├── .gitignore ├── README.md ├── alg ├── insertsort.c ├── selectsort.c └── shellsort.c ├── apue ├── Makefile ├── README.txt ├── Shell │ ├── README.txt │ ├── listing1.c │ ├── listing2.c │ ├── listing3.c │ ├── listing4.c │ ├── listing5.c │ ├── listing5.o │ ├── listing6.c │ ├── listing7.c │ ├── listing8.c │ ├── listing9.c │ ├── test_fill_arg.c │ └── util.h ├── advanceio │ ├── error.c │ ├── error.o │ ├── mmapcp.c │ └── util.h ├── clean2 ├── cleanup2.c ├── daemons │ ├── Makefile │ ├── README.txt │ ├── error.c │ ├── init.c │ ├── lockfile.c │ ├── reread.c │ ├── reread2.c │ ├── single.c │ ├── test_init.c │ └── util.h ├── error.c ├── exit_status ├── exitstatus.c ├── fork_twice.c ├── hello.txt ├── ipc │ ├── error.c │ ├── error.o │ ├── pipe1.c │ ├── pipe_pager.c │ ├── popen_pager.c │ ├── test.sh │ └── util.h ├── pr_exit.c ├── proccontrol │ ├── acctdata │ ├── acctdata.c │ ├── echoall │ ├── echoall.c │ ├── echoarg │ ├── echoarg.c │ ├── error.c │ ├── error.o │ ├── exec1.c │ ├── execinterp.c │ ├── exitcode.c │ ├── fork1.c │ ├── forktwice.c │ ├── getlogin.c │ ├── getlogin2.c │ ├── perm.sh │ ├── prexit.c │ ├── prexit.o │ ├── printacct.c │ ├── printuids │ ├── printuids.c │ ├── setuid │ ├── setuid.c │ ├── setuid2 │ ├── setuid2-1.png │ ├── setuid2-2.png │ ├── setuid2.c │ ├── sys │ ├── system.c │ ├── system.o │ ├── system_arg.c │ ├── systemtest1.c │ ├── systemtest2.c │ ├── tellwait1.c │ ├── tellwait2.c │ ├── tellwait_signal.c │ ├── tellwait_signal.o │ ├── testinterp │ ├── times1.c │ ├── util.h │ ├── vfork1.c │ ├── vfork2.c │ ├── vfork_error.c │ └── zombie.c ├── procenviron │ ├── autovar.c │ ├── cmd1.c │ ├── cmd2.c │ ├── doexit.c │ ├── echoarg.c │ ├── enum2str1.c │ ├── enum2str2.c │ ├── error.c │ ├── getenv_demo.c │ ├── getrlimit.c │ ├── hello1.c │ ├── opendata.c │ ├── setjmp_demo.c │ ├── testjmp.c │ ├── tokenize.c │ ├── tokenize_test.c │ └── util.h ├── procrelation │ └── tcgetpgrp_demo.c ├── ptrace │ ├── README.txt │ ├── attach.c │ ├── breakpoint.c │ ├── breakpoint2.c │ ├── dummy │ ├── dummy1.s │ ├── dummy2.c │ ├── hello │ ├── hello.c │ ├── hello2.c │ ├── ptrace0.c │ ├── ptrace1.c │ ├── ptrace2.c │ ├── ptrace3.c │ ├── ptrace4.c │ └── ptrace5.c ├── signals │ ├── abort.c │ ├── child.c │ ├── critical.c │ ├── ed.c │ ├── error.c │ ├── error.o │ ├── mask.c │ ├── pr_mask.c │ ├── pr_mask.o │ ├── read1.c │ ├── read2.c │ ├── recv │ ├── reenter.c │ ├── send │ ├── signal.c │ ├── signal.md │ ├── signal.o │ ├── signal_recv.c │ ├── signal_send.c │ ├── signalintr.c │ ├── sigsetops.c │ ├── sigsuspend1.c │ ├── sigsuspend2.c │ ├── sigusr.c │ ├── sleep.c │ ├── sleep1.c │ ├── sleep2.c │ ├── sleep2.o │ ├── system.c │ ├── system.o │ ├── system1.c │ ├── system1.o │ ├── tellwait_signal.c │ ├── timer │ │ └── clock.hh │ ├── tsleep2.c │ └── util.h ├── sigsyn.c ├── threadctl │ ├── detach.c │ ├── error.c │ ├── genenv1.c │ ├── getenv2.c │ ├── getenv3.c │ ├── suspend.c │ ├── timeout.c │ └── util.h ├── threads │ ├── Print1234ToFile.c │ ├── README.txt │ ├── ThreeThreadPrintABC.c │ ├── TwoThreadLoop.c │ ├── atomic_vriable.c │ ├── badexit.c │ ├── cleanup.c │ ├── condvar.c │ ├── error.c │ ├── error.o │ ├── exit_status.c │ ├── exitstatus.c │ ├── mutex1.c │ ├── mutex2.c │ ├── mutex3.c │ ├── rwlock.c │ ├── threadid │ ├── threadid.c │ ├── threadid.o │ └── util.h └── util.h ├── csapp ├── README ├── bin │ ├── d2h.lua │ └── h2d.lua ├── chapter7 │ └── hello.c └── code │ └── data │ ├── README.txt │ ├── bool_xor.c │ ├── data.h │ ├── demo22.c │ ├── demo23.c │ ├── demo25.c │ ├── demo26.c │ ├── demo27.c │ ├── demo30.c │ ├── demo31.c │ ├── demo32.c │ ├── demo35.c │ ├── demo36.c │ ├── extend_bits.c │ ├── inplace_swap.c │ ├── reverse_array.c │ ├── show_bytes.c │ ├── show_bytes.o │ └── test_show_bytes.c ├── ctraps ├── 1.5.c ├── 2.1.c ├── 2.3.c ├── 2.ex1.c ├── 3.1.c ├── 5.1.c ├── 5.3.c ├── 5.3a.c ├── 5.3b.c ├── 6.1.c ├── 6.3.c ├── 6.3a.c ├── 6.3b.c ├── 7.1.c ├── 7.11.c ├── 7.11a.c ├── 7.4.c ├── 7.5.c ├── 7.6.c ├── 7.8.c └── testexit.c ├── expertc ├── README.txt ├── arr_ptr.c ├── arr_struct.c ├── chapter4 │ ├── a.cpp │ ├── extern_file1.o │ ├── mango.c │ ├── mango_defined.c │ └── string_iteral.c ├── const.c ├── func.c ├── getch.c ├── hello.c ├── kbhit.c ├── libfruit.so ├── linklist.c ├── map ├── nm_use.sh ├── out.map ├── pid_tag.c ├── self-plus.c ├── stack_addr.c ├── string_literal.c ├── swordoffer │ ├── README.txt │ └── str.c ├── test.c ├── test_nm.c ├── tomato.c ├── tomato.o ├── trick.c ├── trick2.c ├── trick3.c ├── trick4.c ├── trick5.c ├── trick6.c ├── trick7.c ├── trick8.c ├── trick9.c └── union.c ├── hp-server ├── select-use.c └── tcpserverepoll.c ├── kern ├── README.txt ├── fs-vfs │ ├── Makefile │ ├── README.txt │ └── vfs_test.c ├── hello │ ├── Makefile │ └── hello.c ├── high-mem │ ├── Makefile │ └── high_low.c ├── irq-request │ ├── Makefile │ ├── README.txt │ └── kbd.c ├── irq-tasklet │ ├── Makefile │ ├── README.txt │ └── mytasklet.c ├── jiffies │ ├── Makefile │ ├── README.txt │ └── timer.c ├── kthread │ ├── Makefile │ ├── README.txt │ └── kthread_demo.c ├── list │ ├── Makefile │ ├── README.txt │ └── hello.c ├── memory-area │ ├── Makefile │ ├── README.txt │ ├── kern_print_segs.c │ └── print-segments.c ├── mengning │ ├── README.txt │ ├── main.S │ ├── main.c │ ├── main2.S │ └── test.c ├── nelink │ ├── README.txt │ ├── echo-unicast │ │ ├── .built-in.o.cmd │ │ ├── .kern.ko.cmd │ │ ├── .kern.mod.o.cmd │ │ ├── .kern.o.cmd │ │ ├── .tmp_versions │ │ │ └── kern.mod │ │ ├── Makefile │ │ ├── Module.symvers │ │ ├── built-in.o │ │ ├── kern.c │ │ ├── kern.ko │ │ ├── kern.mod.c │ │ ├── kern.mod.o │ │ ├── kern.o │ │ ├── modules.order │ │ └── userspace.c │ ├── echo │ │ ├── Makefile │ │ ├── nl_hello.c │ │ └── user.c │ ├── genl-hello │ │ ├── .built-in.o.cmd │ │ ├── .genl_kern.ko.cmd │ │ ├── .genl_kern.mod.o.cmd │ │ ├── .genl_kern.o.cmd │ │ ├── .tmp_versions │ │ │ └── genl_kern.mod │ │ ├── Makefile │ │ ├── Makefile.old │ │ ├── Module.symvers │ │ ├── built-in.o │ │ ├── genl_kern.c │ │ ├── genl_kern.ko │ │ ├── genl_kern.mod.c │ │ ├── genl_kern.mod.o │ │ ├── genl_kern.o │ │ ├── genl_user.c │ │ └── modules.order │ ├── kern2user │ │ ├── .built-in.o.cmd │ │ ├── .kern.ko.cmd │ │ ├── .kern.mod.o.cmd │ │ ├── .kern.o.cmd │ │ ├── .nl_hello.ko.cmd │ │ ├── .nl_hello.mod.o.cmd │ │ ├── .nl_hello.o.cmd │ │ ├── .tmp_versions │ │ │ └── nl_hello.mod │ │ ├── Makefile │ │ ├── nl_hello.c │ │ └── user.c │ └── route │ │ └── getroute.c ├── netdev-notifier │ ├── Makefile │ ├── README.txt │ ├── hello.c │ └── notifier.c ├── netdev-test │ ├── Makefile │ ├── README.txt │ └── hello.c ├── page │ ├── Makefile │ └── hello.c ├── percpu │ ├── Makefile │ ├── README.txt │ └── hello.c ├── process │ ├── Makefile │ ├── README.txt │ └── current.c ├── schedule │ ├── .built-in.o.cmd │ ├── .schedule_test.ko.cmd │ ├── .schedule_test.mod.o.cmd │ ├── .schedule_test.o.cmd │ ├── .tmp_versions │ │ └── schedule_test.mod │ ├── Makefile │ ├── README.txt │ └── schedule_test.c ├── slab │ ├── Makefile │ └── hello.c ├── syscall │ ├── Makefile │ ├── data.txt │ ├── interceptor.c │ ├── test.c │ └── udpserver.c └── test.c ├── ldd └── completion │ ├── .complete.ko.cmd │ ├── .complete.mod.o.cmd │ ├── .complete.o.cmd │ ├── .tmp_versions │ └── complete.mod │ ├── Makefile │ ├── Module.symvers │ ├── complete.c │ ├── complete.ko │ ├── complete.mod.c │ ├── complete.mod.o │ ├── complete.o │ └── modules.order ├── leetcode ├── hello.c ├── test.c └── test2.c ├── linux-expert └── namespaces │ ├── main-0-template.c │ ├── main-1-uts.c │ └── ns ├── master-alg ├── list.c ├── list.h ├── test-list.c └── vonzhou.txt ├── ovs-learn └── StaticVariabe.c ├── pcap ├── capture ├── capture_pkt.c ├── ip ├── show_ip_mask.c ├── test └── test.c ├── playwithds ├── README.txt ├── sort │ ├── BubbleSort0.c │ ├── InsertSort.c │ └── sort.h └── tree │ └── BiTree.c ├── pong └── play.lua ├── problems └── tecent_2016_11.c ├── py-code ├── byte │ ├── break.py │ ├── cat.py │ ├── continue.py │ ├── expression.py │ ├── finally.py │ ├── for.py │ ├── func1.py │ ├── func_default.py │ ├── func_doc.py │ ├── func_key.py │ ├── func_return.py │ ├── helloworld.py │ ├── if.py │ ├── lambda.py │ ├── local_global.py │ ├── mymodule.py │ ├── mymodule.pyc │ ├── mymodule_demo2.py │ ├── odbchelper.py │ ├── odbchelper.pyc │ ├── pickling.py │ ├── poem.txt │ ├── printMax.py │ ├── raising.py │ ├── shoplist.dat │ ├── try_except.py │ ├── using_dict.py │ ├── using_file.py │ ├── using_list.py │ ├── using_name.py │ ├── using_name.pyc │ ├── using_sys.py │ ├── using_tuple.py │ ├── var.py │ └── while.py └── http_test.py ├── python └── palindrome_v1.py ├── recipe └── ratable.py ├── redis-learn └── demo_isnan.c ├── scull ├── .access.o.cmd ├── .main.o.cmd ├── .pipe.o.cmd ├── .scull.ko.cmd ├── .scull.mod.o.cmd ├── .scull.o.cmd ├── .tmp_versions │ └── scull.mod ├── Makefile ├── Module.symvers ├── access.c ├── access.o ├── main.c ├── main.o ├── modules.order ├── pipe.c ├── pipe.o ├── scull.h ├── scull.init ├── scull.ko ├── scull.mod.c ├── scull.mod.o ├── scull.o ├── scull_load ├── scull_unload └── test_scull.c ├── test1.c ├── test_memcpy.c ├── thinking ├── func1.c ├── func2.c └── func3.c ├── tinyhttpd-0.1.0 ├── Makefile ├── README ├── README~ ├── htdocs │ ├── README │ ├── a.html │ ├── check.cgi │ ├── color.cgi │ ├── index.html │ └── test │ │ ├── a.html │ │ ├── index.html │ │ └── index.html~ ├── httpd ├── httpd.c ├── httpd.c~ └── simpleclient.c ├── tlpi ├── .gitignore ├── 55filelock │ ├── Makefile │ ├── curr_time.c │ ├── curr_time.h │ ├── ename.c.inc │ ├── error_functions.c │ ├── error_functions.h │ ├── get_num.c │ ├── get_num.h │ ├── t_flock.c │ ├── tfile │ └── tlpi_hdr.h └── README.txt ├── tset.c ├── unp ├── .inet_ntop_ipv4.c.swp ├── Makefile ├── README.md ├── _ ├── get_ifi_info.c ├── get_ifi_info.o ├── inet_ntop_ipv4.c ├── inet_ntop_ipv4.o ├── intro │ ├── README.md │ ├── byteorder.c │ ├── cli │ ├── client │ ├── daytimecli.c │ ├── daytimecli.o │ ├── daytimeser.c │ ├── daytimeser.o │ ├── error.c │ ├── error.o │ ├── inet_ntop_pton_v4.c │ ├── ser │ ├── server │ ├── sock_ntop.c │ ├── sock_ntop.o │ ├── test_sock_ntop.c │ └── util.h ├── libevent_learn │ ├── libevent_echo_server.c │ ├── timer │ └── timer_test.c ├── prifinfo.c ├── prifinfo.o ├── prifinfo2.c ├── sock_ntop_host.c ├── sock_ntop_host.o ├── socketpair.c ├── socketpair2.c ├── socketpair_server.c ├── tcpepool │ ├── server │ └── tcpserverepoll.c ├── tcpsercli │ ├── Makefile │ ├── echo_server_event │ ├── error.c │ ├── error.o │ ├── readline.c │ ├── readline.o │ ├── serverepoll │ ├── str_cli.c │ ├── str_cli.o │ ├── str_echo.c │ ├── str_echo.o │ ├── tcpcli01.c │ ├── tcpcli01.o │ ├── tcpclient │ ├── tcpserver │ ├── tcpserver01.c │ ├── tcpserver01.o │ ├── tcpserverepoll │ ├── tcpserverepoll.c │ ├── util.h │ ├── wrapsock.c │ ├── wrapsock.o │ ├── wrapstdio.c │ ├── wrapstdio.o │ ├── wrapunix.c │ ├── wrapunix.o │ ├── writen.c │ └── writen.o ├── tcpsercli02 │ ├── Makefile │ ├── echo_server_event │ ├── error.c │ ├── readline.c │ ├── serverepoll │ ├── signal.c │ ├── str_cli.c │ ├── str_echo.c │ ├── tcpcli01.c │ ├── tcpserver02.c │ ├── tcpserverepoll.c │ ├── util.h │ ├── wrapsock.c │ ├── wrapstdio.c │ ├── wrapunix.c │ └── writen.c ├── udp │ ├── a │ ├── afile.html │ ├── client │ ├── client2 │ ├── file_transfer_client.c │ ├── file_transfer_client2.c │ ├── file_transfer_server.c │ ├── file_transfer_server2.c │ ├── hello │ ├── server │ └── server2 ├── udptest │ ├── README.txt │ ├── global.h │ ├── udp-client.c │ └── udp-server.c ├── unixbin.c ├── unixdomain │ ├── Makefile │ ├── error.c │ ├── error.o │ ├── my_open.c │ ├── my_open.o │ ├── mycat │ ├── mycat.c │ ├── openfile │ ├── openfile.c │ ├── read_fd.c │ ├── read_fd.o │ ├── util.h │ ├── wrapsock.c │ ├── wrapsock.o │ ├── wrapunix.c │ ├── wrapunix.o │ ├── write_fd.c │ └── write_fd.o └── unpifi.h └── unpv2-old ├── Makefile ├── error.c ├── error.o ├── pipe ├── Makefile ├── client.c ├── client.o ├── error.c ├── error.o ├── fduplex ├── fduplex.c ├── fduplex.o ├── mainpipe ├── mainpipe.c ├── mainpipe.o ├── server.c ├── server.o ├── unpipc.h ├── wrapstdio.c ├── wrapstdio.o ├── wrapunix.c └── wrapunix.o ├── px-ipc-name ├── px_ipc_name.c ├── px_ipc_name.o └── unpipc.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | tlpi/tlpi-book 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 这是在平时学习Linux环境编程中写的C程序 2 | --- 3 | 4 | ## [《UNIX网络编程 卷2 - 进程间通信》](unpv2/) 5 | 6 | ## [《UNIX网络编程 卷1》](unpv13e/) 7 | 8 | ## [《Unix高级环境编程》](apue/) 9 | 10 | ## [《C陷阱与缺陷》](ctraps/) 11 | 12 | ## [《深入理解计算机系统结构》]() 13 | 14 | ## [《The Linux Programming interface》](tlpi/) 15 | 16 | 17 | -------------------------------------------------------------------------------- /alg/insertsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int less(int a, int b){ 4 | if(a < b) 5 | return 1; 6 | return 0; 7 | } 8 | 9 | void exch(int *a, int i , int j){ 10 | int temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | 15 | void insert_sort(int *a, int low, int high){ 16 | int i,j; 17 | for(i = low; i < high ; i++){ 18 | for(j = i; j > 0 && less(a[j], a[j-1]); j--) 19 | exch(a, j, j-1); 20 | } 21 | } 22 | void show(int *a, int len){ 23 | int i; 24 | for(i = 0; i< len ;i++) 25 | printf("%d ",a[i]); 26 | printf("\n"); 27 | } 28 | int main(){ 29 | int n = 7; 30 | int a[] = {12,45,-12, 5, 89, 100, 0}; 31 | 32 | insert_sort(a, 0 , n); 33 | show(a,n); 34 | return 0; 35 | } 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /alg/selectsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int less(int a, int b){ 4 | if(a < b) 5 | return 1; 6 | return 0; 7 | } 8 | 9 | void exch(int *a, int i , int j){ 10 | int temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | void slect_sort(int *a, int low, int high){ 15 | int i, j; 16 | for(i = low ; i < high ; i++){ 17 | int min = i; // minimum index 18 | for(j = i + 1; j < high; j++){ 19 | if(less(a[j], a[min])) 20 | min = j; 21 | } 22 | exch(a , i, min); 23 | } 24 | } 25 | 26 | void show(int *a, int len){ 27 | int i; 28 | for(i = 0; i< len ;i++) 29 | printf("%d ",a[i]); 30 | printf("\n"); 31 | } 32 | int main(){ 33 | int n = 7; 34 | int a[] = {12,45,-12, 5, 89, 100, 0}; 35 | 36 | select_sort(a, 0 , n); 37 | show(a,n); 38 | return 0; 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /apue/Makefile: -------------------------------------------------------------------------------- 1 | all: sigsyn exit_status 2 | sigsyn: error.o sigsyn.o 3 | gcc -o sigsyn error.o sigsyn.o 4 | exit_status:error.o exit_status.o 5 | gcc -o exit_status error.o pr_exit.o exit_status.o 6 | 7 | error.o : error.c util.h 8 | gcc -c error.c 9 | pr_mask.o : pr_mask.c util.h 10 | gcc -c pr_mask.c 11 | mask.o : mask.c util.h 12 | gcc -c mask.c 13 | sigsuspend1.o:sigsuspend1.c util.h 14 | gcc -c sigsuspend1.c 15 | 16 | sigsuspend2.o:sigsuspend2.c util.h 17 | gcc -c sigsuspend2.c 18 | 19 | sigsyn.o:sigsyn.c util.h 20 | gcc -c sigsyn.c 21 | 22 | pr_exit.o:pr_exit.c util.h 23 | gcc -c pr_exit.c 24 | exit_status.o:exit_status.c util.h 25 | gcc -c exit_status.c 26 | 27 | 28 | 29 | clean : 30 | -rm test_siglongjmp *.o 31 | -------------------------------------------------------------------------------- /apue/README.txt: -------------------------------------------------------------------------------- 1 | 2 | Linux 环境编程 3 | 4 | 1. APUE阅读 5 | advaceio : 6 | - mmapio 利用内存映射实现文件拷贝 7 | 2. 学习过程中的demo 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /apue/Shell/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 学习实现一个Shell,by Hiran Ramankutty。 3 | 4 | -------------------------------------------------------------------------------- /apue/Shell/listing1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char **argv, char **envp) 6 | { 7 | execve("/bin/ls", argv, envp); 8 | printf("vonzhou\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /apue/Shell/listing2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[], char *envp[]) 6 | { 7 | char c = '\0'; 8 | printf("\n[MY_SHELL ] "); 9 | while(c != EOF) { 10 | c = getchar(); 11 | if(c == '\n') 12 | printf("[MY_SHELL ] "); 13 | } 14 | printf("\n"); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /apue/Shell/listing3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef void (*sighandler_t)(int); 7 | char c = '\0'; 8 | 9 | void handle_signal(int signo){ 10 | printf("\n[MY_SHELL ] "); 11 | fflush(stdout); 12 | } 13 | 14 | int main(int argc, char *argv[], char *envp[]){ 15 | signal(SIGINT, SIG_IGN); 16 | signal(SIGINT, handle_signal); 17 | printf("[MY_SHELL ] "); 18 | while(c != EOF) { 19 | c = getchar(); 20 | if(c == '\n') 21 | printf("[MY_SHELL ] "); 22 | } 23 | printf("\n"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /apue/Shell/listing4.c: -------------------------------------------------------------------------------- 1 | 2 | // 为何不直接读取一整行呢? 3 | 4 | int main(int argc, char *argv[], char *envp[]) 5 | { 6 | /* do some initializations. */ 7 | while(c != EOF) { 8 | c = getchar(); 9 | switch(c) { 10 | case '\n': /* parse and execute. */ 11 | bzero(tmp, sizeof(tmp)); 12 | break; 13 | default: strncat(tmp, &c, 1); 14 | break; 15 | } 16 | } 17 | /* some processing before terminating. */ 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /apue/Shell/listing5.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | void fill_argv(char *tmp_argv) 4 | { 5 | char *foo = tmp_argv; 6 | int index = 0; 7 | char ret[100]; 8 | bzero(ret, 100); 9 | while(*foo != '\0') { 10 | if(index == 10) 11 | break; 12 | 13 | if(*foo == ' ') { 14 | if(my_argv[index] == NULL) 15 | my_argv[index] = (char *)malloc(sizeof(char) * strlen(ret) + 1); 16 | else { 17 | bzero(my_argv[index], strlen(my_argv[index])); 18 | } 19 | strncpy(my_argv[index], ret, strlen(ret)); 20 | strncat(my_argv[index], "\0", 1); 21 | bzero(ret, 100); 22 | index++; 23 | } else { 24 | strncat(ret, foo, 1); 25 | } 26 | foo++; 27 | } 28 | if(ret[0] != '\0') { 29 | my_argv[index] = (char *)malloc(sizeof(char) * strlen(ret) + 1); 30 | strncpy(my_argv[index], ret, strlen(ret)); 31 | strncat(my_argv[index], "\0", 1); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /apue/Shell/listing5.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/Shell/listing5.o -------------------------------------------------------------------------------- /apue/Shell/listing6.c: -------------------------------------------------------------------------------- 1 | // defines a function that checks for the existence of the command 2 | 3 | int attach_path(char *cmd) 4 | { 5 | char ret[100]; 6 | int index; 7 | int fd; 8 | bzero(ret, 100); 9 | for(index=0;search_path[index]!=NULL;index++) { 10 | strcpy(ret, search_path[index]); 11 | strncat(ret, cmd, strlen(cmd)); 12 | if((fd = open(ret, O_RDONLY)) > 0) { 13 | strncpy(cmd, ret, strlen(ret)); 14 | close(fd); 15 | return 0; 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /apue/Shell/listing7.c: -------------------------------------------------------------------------------- 1 | void get_path_string(char **tmp_envp, char *bin_path) 2 | { 3 | int count = 0; 4 | char *tmp; 5 | while(1) { 6 | tmp = strstr(tmp_envp[count], "PATH"); 7 | if(tmp == NULL) { 8 | count++; 9 | } else { 10 | break; 11 | } 12 | } 13 | strncpy(bin_path, tmp, strlen(tmp)); 14 | } 15 | 16 | void insert_path_str_to_search(char *path_str) 17 | { 18 | int index=0; 19 | char *tmp = path_str; 20 | char ret[100]; 21 | 22 | while(*tmp != '=') 23 | tmp++; 24 | tmp++; 25 | 26 | while(*tmp != '\0') { 27 | if(*tmp == ':') { 28 | strncat(ret, "/", 1); 29 | search_path[index] = (char *) malloc(sizeof(char) * (strlen(ret) + 1)); 30 | strncat(search_path[index], ret, strlen(ret)); 31 | strncat(search_path[index], "\0", 1); 32 | index++; 33 | bzero(ret, 100); 34 | } else { 35 | strncat(ret, tmp, 1); 36 | } 37 | tmp++; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /apue/Shell/listing8.c: -------------------------------------------------------------------------------- 1 | void call_execve(char *cmd) 2 | { 3 | int i; 4 | if(fork() == 0) { 5 | i = execlp(cmd, my_argv, my_envp); 6 | if(i < 0) { 7 | printf("%s: %s\n", cmd, "command not found"); 8 | exit(1); 9 | } 10 | } else { 11 | wait(NULL); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /apue/Shell/test_fill_arg.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | char *my_argv[100]; 7 | 8 | 9 | int main(){ 10 | 11 | char *s = "gcc -o hello hello.c"; 12 | fill_argv(s); 13 | printf("%s\n", s); 14 | 15 | return 0; 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /apue/Shell/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | extern char *my_argv; 6 | -------------------------------------------------------------------------------- /apue/advanceio/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/advanceio/error.o -------------------------------------------------------------------------------- /apue/advanceio/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/clean2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/clean2 -------------------------------------------------------------------------------- /apue/daemons/Makefile: -------------------------------------------------------------------------------- 1 | all: reread test_init 2 | reread : reread.o single.o error.o lockfile.o init.o 3 | gcc -o reread reread.o single.o error.o lockfile.o init.o -lpthread 4 | test_init: test_init.o init.o error.o 5 | gcc -o test_init test_init.o init.o error.o 6 | 7 | test_init.o: test_init.c util.h 8 | gcc -c test_init.c 9 | init.o: init.c util.h 10 | gcc -c init.c 11 | error.o : error.c util.h 12 | gcc -c error.c 13 | single.o : single.c 14 | gcc -c single.c 15 | lockfile.o : lockfile.c 16 | gcc -c lockfile.c 17 | reread.o:reread.c util.h 18 | gcc -c reread.c 19 | 20 | 21 | 22 | clean : 23 | -rm reread test_init *.o 24 | -------------------------------------------------------------------------------- /apue/daemons/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 学习APUE守护进程,再次阅读,理解更加深入了。 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /apue/daemons/lockfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | lockfile(int fd) 6 | { 7 | struct flock fl; 8 | 9 | fl.l_type = F_WRLCK; 10 | fl.l_start = 0; 11 | fl.l_whence = SEEK_SET; 12 | fl.l_len = 0; 13 | return(fcntl(fd, F_SETLK, &fl)); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /apue/daemons/single.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define LOCKFILE "/var/run/daemon.pid" 11 | #define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 12 | 13 | extern int lockfile(int); 14 | 15 | int 16 | already_running(void) 17 | { 18 | int fd; 19 | char buf[16]; 20 | 21 | fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE); 22 | if (fd < 0) { 23 | syslog(LOG_ERR, "can't open %s: %s", LOCKFILE, strerror(errno)); 24 | exit(1); 25 | } 26 | if (lockfile(fd) < 0) { 27 | if (errno == EACCES || errno == EAGAIN) { 28 | close(fd); 29 | return(1); 30 | } 31 | syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno)); 32 | exit(1); 33 | } 34 | ftruncate(fd, 0); 35 | sprintf(buf, "%ld", (long)getpid()); 36 | write(fd, buf, strlen(buf)+1); 37 | return(0); 38 | } 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /apue/daemons/test_init.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int main(){ 4 | 5 | daemonize("main--"); 6 | pause(); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /apue/daemons/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/exit_status: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/exit_status -------------------------------------------------------------------------------- /apue/fork_twice.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | 4 | int main(void){ 5 | pid_t pid; 6 | 7 | if((pid = fork()) < 0) 8 | err_sys("fork error"); 9 | else if(pid == 0){ 10 | //first child 11 | if((pid = fork()) < 0) 12 | err_sys("fork error"); 13 | else if(pid > 0) 14 | exit(0); // parent from second fork == first child ,first child exit; 15 | //we are the second child,our parent become init as soon as 16 | //our real parent calls exit() int the statement above;] 17 | //continue executing,when we're done,init will reap our status 18 | sleep(2); //so that the first child been exited; 19 | printf("second child,parent pid = %d\n",getppid()); 20 | exit(0); 21 | } 22 | 23 | if(waitpid(pid,NULL,0) != pid) 24 | err_sys("waitpid error"); 25 | 26 | //we're the original process,continue executing, 27 | //knowing that we're not the parent of the second child 28 | exit(0); 29 | } 30 | -------------------------------------------------------------------------------- /apue/ipc/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/ipc/error.o -------------------------------------------------------------------------------- /apue/ipc/pipe1.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int 4 | main(void){ 5 | int fd[2]; 6 | int n; 7 | pid_t pid; 8 | char line[MAXLINE]; 9 | 10 | if(pipe(fd) < 0) 11 | err_sys("pipe error"); 12 | if((pid = fork()) < 0) 13 | err_sys("fork error"); 14 | else if(pid == 0){ 15 | //child 16 | close(fd[1]); 17 | n = read(fd[0],line,MAXLINE); 18 | write(STDOUT_FILENO,line,n); 19 | }else{ 20 | //parent 21 | close(fd[0]); 22 | write(fd[1],"hello vonzhou\n",14); 23 | } 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /apue/ipc/popen_pager.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | #define PAGER "${PAGER:-more}" // environment var or default 4 | 5 | int main(int argc, char *argv[]){ 6 | char line[MAXLINE]; 7 | FILE *fpin, *fpout; 8 | 9 | if(argc != 2) 10 | err_quit("usage:a.out "); 11 | 12 | if((fpin = fopen(argv[1], "r")) == NULL) 13 | err_sys("cannot open %s", argv[1]); 14 | 15 | if((fpout = popen(PAGER, "w")) == NULL) 16 | err_sys("popen error"); 17 | // copy the file to pager 18 | while(fgets(line, MAXLINE, fpin) != NULL){ 19 | if(fputs(line, fpout) == EOF) 20 | err_sys("fputs error to pipe"); 21 | } 22 | 23 | if(ferror(fpin)) 24 | err_sys("fgets error"); 25 | pclose(fpout); 26 | 27 | exit(0); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /apue/ipc/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "hello$$" 4 | -------------------------------------------------------------------------------- /apue/ipc/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/pr_exit.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | 4 | void 5 | pr_exit(int status) 6 | { 7 | if (WIFEXITED(status)) 8 | printf("normal termination, exit status = %d\n",WEXITSTATUS(status)); 9 | else if (WIFSIGNALED(status)) 10 | printf("abnormal termination, signal number = %d%s\n",WTERMSIG(status), 11 | #ifdef WCOREDUMP 12 | WCOREDUMP(status) ? " (core file generated)" : ""); 13 | #else 14 | ""); 15 | #endif 16 | else if (WIFSTOPPED(status)) 17 | printf("child stopped, signal number = %d\n",WSTOPSIG(status)); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /apue/proccontrol/acctdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/acctdata -------------------------------------------------------------------------------- /apue/proccontrol/echoall: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/echoall -------------------------------------------------------------------------------- /apue/proccontrol/echoall.c: -------------------------------------------------------------------------------- 1 | 2 | // prag : 8-9 3 | #include "util.h" 4 | 5 | int 6 | main(int argc, char *argv[]) 7 | { 8 | int i; 9 | char **ptr; 10 | extern char **environ; 11 | 12 | for (i = 0; i < argc; i++) /* echo all command-line args */ 13 | printf("argv[%d]: %s\n", i, argv[i]); 14 | 15 | for (ptr = environ; *ptr != 0; ptr++) /* and all env strings */ 16 | printf("%s\n", *ptr); 17 | 18 | exit(0); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /apue/proccontrol/echoarg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/echoarg -------------------------------------------------------------------------------- /apue/proccontrol/echoarg.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int 4 | main(int argc, char *argv[]) 5 | { 6 | int i; 7 | 8 | for (i = 0; i < argc; i++) /* echo all command-line args */ 9 | printf("argv[%d]: %s\n", i, argv[i]); 10 | exit(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /apue/proccontrol/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/error.o -------------------------------------------------------------------------------- /apue/proccontrol/exec1.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | 4 | char *env_init[] = { "USER=unknown", "PATH=/tmp", NULL }; 5 | 6 | int 7 | main(void) 8 | { 9 | pid_t pid; 10 | 11 | if ((pid = fork()) < 0) { 12 | err_sys("fork error"); 13 | } else if (pid == 0) { /* specify pathname, specify environment */ 14 | if (execle("/usr/local/bin/echoall", "echoall", "myarg1", 15 | "MY ARG2", (char *)0, env_init) < 0) 16 | err_sys("execle error"); 17 | } 18 | 19 | if (waitpid(pid, NULL, 0) < 0) 20 | err_sys("wait error"); 21 | 22 | if ((pid = fork()) < 0) { 23 | err_sys("fork error"); 24 | } else if (pid == 0) { /* specify filename, inherit environment */ 25 | if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0) 26 | err_sys("execlp error"); 27 | } 28 | 29 | exit(0); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /apue/proccontrol/execinterp.c: -------------------------------------------------------------------------------- 1 | // prag: 8 -10 2 | 3 | #include "util.h" 4 | #include 5 | 6 | int 7 | main(void) 8 | { 9 | pid_t pid; 10 | 11 | if ((pid = fork()) < 0) { 12 | err_sys("fork error"); 13 | } else if (pid == 0) { /* child */ 14 | if (execl("/home/vonzhou/Coding/apue/proccontrol/testinterp", "testinterp", "myarg1", "MY ARG2", (char *)0) < 0) 15 | err_sys("execl error"); 16 | } 17 | if (waitpid(pid, NULL, 0) < 0) /* parent */ 18 | err_sys("waitpid error"); 19 | exit(0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /apue/proccontrol/fork1.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int glob = 6; /* external variable in initialized data */ 4 | char buf[] = "a write to stdout\n"; 5 | 6 | int 7 | main(void) 8 | { 9 | int var; /* automatic variable on the stack */ 10 | pid_t pid; 11 | 12 | var = 88; 13 | if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1) 14 | err_sys("write error"); 15 | printf("before fork\n"); /* we don't flush stdout */ 16 | 17 | if ((pid = fork()) < 0) { 18 | err_sys("fork error"); 19 | } else if (pid == 0) { /* child */ 20 | glob++; /* modify variables */ 21 | var++; 22 | } else { 23 | sleep(2); /* parent */ 24 | } 25 | 26 | printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var); 27 | exit(0); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /apue/proccontrol/getlogin.c: -------------------------------------------------------------------------------- 1 | 2 | // APUE p208 3 | 4 | #include 5 | #include 6 | #include 7 | #include // getlogin() 8 | 9 | int main() 10 | { 11 | struct passwd *my_info; 12 | char *s = getlogin(); 13 | my_info = getpwnam(s); 14 | //my_info = getpwnam( "fflg" ); 15 | printf( "my name = [%s]\n", my_info->pw_name ); 16 | printf( "my passwd = [%s]\n", my_info->pw_passwd ); 17 | printf( "my uid = [%d]\n", my_info->pw_uid ); 18 | printf( "my gid = [%d]\n", my_info->pw_gid ); 19 | printf( "my gecos = [%s]\n", my_info->pw_gecos ); 20 | printf( "my dir = [%s]\n", my_info->pw_dir ); 21 | printf( "my shell = [%s]\n", my_info->pw_shell ); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /apue/proccontrol/getlogin2.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | // 通过上一个程序可以得到用户UID 4 | 5 | #include 6 | #include 7 | #include 8 | main() 9 | { 10 | uid_t my_uid; 11 | my_uid = 1001; 12 | struct passwd *my_info; 13 | my_info = getpwuid( my_uid ); 14 | //my_info = getpwnam( "fflg" ); 15 | printf( "my name = [%s]\n", my_info->pw_name ); 16 | printf( "my passwd = [%s]\n", my_info->pw_passwd ); 17 | printf( "my uid = [%d]\n", my_info->pw_uid ); 18 | printf( "my gid = [%d]\n", my_info->pw_gid ); 19 | printf( "my gecos = [%s]\n", my_info->pw_gecos ); 20 | printf( "my dir = [%s]\n", my_info->pw_dir ); 21 | printf( "my shell = [%s]\n", my_info->pw_shell ); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /apue/proccontrol/perm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cat /etc/shadow 4 | -------------------------------------------------------------------------------- /apue/proccontrol/prexit.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | 4 | void 5 | pr_exit(int status) 6 | { 7 | if (WIFEXITED(status)) 8 | printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); 9 | else if (WIFSIGNALED(status)) 10 | printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status), 11 | #ifdef WCOREDUMP 12 | WCOREDUMP(status) ? " (core file generated)" : ""); 13 | #else 14 | ""); 15 | #endif 16 | else if (WIFSTOPPED(status)) 17 | printf("child stopped, signal number = %d\n", 18 | WSTOPSIG(status)); 19 | } 20 | -------------------------------------------------------------------------------- /apue/proccontrol/prexit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/prexit.o -------------------------------------------------------------------------------- /apue/proccontrol/printuids: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/printuids -------------------------------------------------------------------------------- /apue/proccontrol/printuids.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | // prag : 8-15 4 | #include "util.h" 5 | 6 | int 7 | main(void) 8 | { 9 | printf("real uid = %d, effective uid = %d\n", getuid(), geteuid()); 10 | exit(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /apue/proccontrol/setuid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/setuid -------------------------------------------------------------------------------- /apue/proccontrol/setuid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | setuid(0); // SETUID to root 6 | system("./perm.sh"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /apue/proccontrol/setuid2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/setuid2 -------------------------------------------------------------------------------- /apue/proccontrol/setuid2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/setuid2-1.png -------------------------------------------------------------------------------- /apue/proccontrol/setuid2-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/setuid2-2.png -------------------------------------------------------------------------------- /apue/proccontrol/setuid2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include // open() 8 | 9 | #include 10 | #include // strerror() 11 | 12 | 13 | int main(){ 14 | int fd; 15 | setuid(0); // become the superuser to open the master file 16 | fd = open("/etc/shadow", O_RDONLY); 17 | setuid(-1); // give up the privelige 18 | if(fd < 0){ 19 | printf("open error! %s(errno=%d)\n", strerror(errno), errno); 20 | exit(-1); 21 | } 22 | 23 | // do something with fd 24 | 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /apue/proccontrol/sys: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/sys -------------------------------------------------------------------------------- /apue/proccontrol/system.c: -------------------------------------------------------------------------------- 1 | // prag : 8-12 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int 8 | system(const char *cmdstring) /* version without signal handling */ 9 | { 10 | pid_t pid; 11 | int status; 12 | 13 | if (cmdstring == NULL) 14 | return(1); /* always a command processor with UNIX */ 15 | 16 | if ((pid = fork()) < 0) { 17 | status = -1; /* probably out of processes */ 18 | } else if (pid == 0) { /* child */ 19 | execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); 20 | printf("execl error\n"); // 不会执行到这里 21 | _exit(127); /* execl error */ 22 | } else { /* parent */ 23 | while (waitpid(pid, &status, 0) < 0) { 24 | if (errno != EINTR) { 25 | status = -1; /* error other than EINTR from waitpid() */ 26 | break; 27 | } 28 | } 29 | } 30 | 31 | return(status); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /apue/proccontrol/system.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/system.o -------------------------------------------------------------------------------- /apue/proccontrol/system_arg.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | // prag : 8-14 4 | #include "util.h" 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | int status; 9 | 10 | if (argc < 2) 11 | err_quit("command-line argument required"); 12 | 13 | if ((status = system(argv[1])) < 0) 14 | err_sys("system() error"); 15 | pr_exit(status); 16 | 17 | exit(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /apue/proccontrol/systemtest1.c: -------------------------------------------------------------------------------- 1 | 2 | // prag : 8-13 3 | #include "util.h" 4 | #include 5 | 6 | int 7 | main(void) 8 | { 9 | int status; 10 | 11 | if ((status = system("date")) < 0) 12 | err_sys("system() error"); 13 | pr_exit(status); 14 | 15 | if ((status = system("nosuchcommand")) < 0) 16 | err_sys("system() error"); 17 | pr_exit(status); 18 | 19 | if ((status = system("who; exit 44")) < 0) 20 | err_sys("system() error"); 21 | pr_exit(status); 22 | 23 | exit(0); 24 | } 25 | // gcc systemtest1.c system.o prexit.o error.o 26 | -------------------------------------------------------------------------------- /apue/proccontrol/systemtest2.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | static void 4 | sig_int(int signo) 5 | { 6 | printf("caught SIGINT\n"); 7 | } 8 | 9 | static void 10 | sig_chld(int signo) 11 | { 12 | printf("caught SIGCHLD\n"); 13 | } 14 | 15 | int 16 | main(void) 17 | { 18 | if (signal(SIGINT, sig_int) == SIG_ERR) 19 | err_sys("signal(SIGINT) error"); 20 | if (signal(SIGCHLD, sig_chld) == SIG_ERR) 21 | err_sys("signal(SIGCHLD) error"); 22 | if (system("/bin/ed") < 0) 23 | err_sys("system() error"); 24 | exit(0); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /apue/proccontrol/tellwait1.c: -------------------------------------------------------------------------------- 1 | // prag: 8-6 2 | #include "util.h" 3 | 4 | static void charatatime(char *); 5 | 6 | int main(void){ 7 | pid_t pid; 8 | 9 | if ((pid = fork()) < 0) { 10 | err_sys("fork error"); 11 | } else if (pid == 0) { 12 | charatatime("output from child\n"); 13 | } else { 14 | charatatime("output from parent\n"); 15 | } 16 | exit(0); 17 | } 18 | 19 | static void charatatime(char *str){ 20 | char *ptr; 21 | int c; 22 | 23 | // 标准输出设置为不带缓冲,每个字符输出都会调用一次write 24 | setbuf(stdout, NULL); /* set unbuffered */ 25 | for (ptr = str; (c = *ptr++) != 0; ) 26 | putc(c, stdout); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /apue/proccontrol/tellwait2.c: -------------------------------------------------------------------------------- 1 | // prag: 8-7 use tellwait.c 2 | 3 | #include "util.h" 4 | 5 | static void charatatime(char *); 6 | 7 | int 8 | main(void) 9 | { 10 | pid_t pid; 11 | 12 | TELL_WAIT(); 13 | 14 | if ((pid = fork()) < 0) { 15 | err_sys("fork error"); 16 | } else if (pid == 0) { 17 | WAIT_PARENT(); /* parent goes first */ 18 | charatatime("output from child\n"); 19 | } else { 20 | // 父进程先运行 21 | charatatime("output from parent\n"); 22 | TELL_CHILD(pid); 23 | } 24 | exit(0); 25 | } 26 | 27 | static void charatatime(char *str){ 28 | char *ptr; 29 | int c; 30 | 31 | setbuf(stdout, NULL); /* set unbuffered */ 32 | for (ptr = str; (c = *ptr++) != 0; ) 33 | putc(c, stdout); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /apue/proccontrol/tellwait_signal.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/proccontrol/tellwait_signal.o -------------------------------------------------------------------------------- /apue/proccontrol/testinterp: -------------------------------------------------------------------------------- 1 | #! /home/vonzhou/Coding/apue/proccontrol/echoarg foo 2 | -------------------------------------------------------------------------------- /apue/proccontrol/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/proccontrol/vfork1.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int glob = 6; /* external variable in initialized data */ 4 | 5 | int 6 | main(void) 7 | { 8 | int var; /* automatic variable on the stack */ 9 | pid_t pid; 10 | 11 | var = 88; 12 | printf("before vfork\n"); /* we don't flush stdio */ 13 | if ((pid = vfork()) < 0) { 14 | err_sys("vfork error"); 15 | } else if (pid == 0) { /* child */ 16 | glob++; /* modify parent's variables */ 17 | var++; 18 | _exit(0); /* child terminates */ 19 | } 20 | 21 | /* 22 | * Parent continues here. 23 | */ 24 | printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var); 25 | exit(0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /apue/proccontrol/vfork2.c: -------------------------------------------------------------------------------- 1 | 2 | // 习题8.1 3 | #include "util.h" 4 | 5 | int glob = 6; /* external variable in initialized data */ 6 | 7 | int 8 | main(void) 9 | { 10 | int var; /* automatic variable on the stack */ 11 | pid_t pid; 12 | int i; 13 | char buf[20]; 14 | 15 | var = 88; 16 | printf("before vfork\n"); /* we don't flush stdio */ 17 | if ((pid = vfork()) < 0) { 18 | err_sys("vfork error"); 19 | } else if (pid == 0) { /* child */ 20 | glob++; /* modify parent's variables */ 21 | var++; 22 | 23 | fclose(stdout); 24 | i = printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var); 25 | sprintf(buf, "%d\n", i); 26 | write(STDOUT_FILENO, buf, strlen(buf)); 27 | 28 | exit(0); 29 | } 30 | 31 | /* 32 | * Parent continues here. 33 | */ 34 | printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var); 35 | exit(0); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /apue/proccontrol/vfork_error.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | //习题8.2,没有理解原理 4 | #include "util.h" 5 | 6 | static void f1(void), f2(void); 7 | 8 | int 9 | main(void) 10 | { 11 | f1(); 12 | f2(); 13 | _exit(0); 14 | } 15 | 16 | static void 17 | f1(void) 18 | { 19 | pid_t pid; 20 | 21 | if ((pid = vfork()) < 0) 22 | err_sys("vfork error"); 23 | /* child and parent both return */ 24 | } 25 | 26 | static void 27 | f2(void) 28 | { 29 | char buf[1000]; /* automatic variables */ 30 | int i; 31 | 32 | for (i = 0; i < sizeof(buf); i++) 33 | buf[i] = 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /apue/proccontrol/zombie.c: -------------------------------------------------------------------------------- 1 | 2 | // 3 | 4 | #include "util.h" 5 | 6 | #ifdef SOLARIS 7 | #define PSCMD "ps -a -o pid,ppid,s,tty,comm" 8 | #else 9 | #define PSCMD "ps -o pid,ppid,state,tty,command" 10 | #endif 11 | 12 | int 13 | main(void) 14 | { 15 | pid_t pid; 16 | 17 | if ((pid = fork()) < 0) 18 | err_sys("fork error"); 19 | else if (pid == 0) /* child */ 20 | exit(0); 21 | 22 | /* parent */ 23 | sleep(4); 24 | system(PSCMD); 25 | 26 | exit(0); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /apue/procenviron/autovar.c: -------------------------------------------------------------------------------- 1 | 2 | // - 7.10 3 | // 没有看到出错的结果!!! 4 | #include 5 | #include 6 | 7 | int f(int val){ 8 | int *ptr; 9 | if(val ==0){ 10 | int val ; 11 | val = 5; 12 | ptr = &val; 13 | } 14 | return (*ptr + 1); 15 | } 16 | 17 | int f2(int x){ 18 | x = x * x; 19 | printf("in f2 : %d\n", x); 20 | return x; 21 | } 22 | 23 | 24 | int main(){ 25 | int val = f(0); 26 | int i = 10; 27 | for(i=0 ; i < 10; i++){ 28 | int x = i *i; 29 | } 30 | f2(10); 31 | printf("%d\n", val); 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /apue/procenviron/cmd2.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | 4 | #define TOK_ADD 5 5 | 6 | jmp_buf jmpbuffer; 7 | 8 | int 9 | main(void) 10 | { 11 | char line[MAXLINE]; 12 | 13 | if (setjmp(jmpbuffer) != 0) 14 | printf("error"); 15 | while (fgets(line, MAXLINE, stdin) != NULL) 16 | do_line(line); 17 | exit(0); 18 | } 19 | 20 | // . . . 21 | 22 | void 23 | cmd_add(void) 24 | { 25 | int token; 26 | 27 | token = get_token(); 28 | if (token < 0) /* an error has occurred */ 29 | longjmp(jmpbuffer, 1); 30 | /* rest of processing for this command */ 31 | } 32 | 33 | -------------------------------------------------------------------------------- /apue/procenviron/doexit.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | static void my_exit1(void); 4 | static void my_exit2(void); 5 | 6 | int 7 | main(void) 8 | { 9 | if (atexit(my_exit2) != 0) 10 | err_sys("can't register my_exit2"); 11 | 12 | if (atexit(my_exit1) != 0) 13 | err_sys("can't register my_exit1"); 14 | if (atexit(my_exit1) != 0) 15 | err_sys("can't register my_exit1"); 16 | 17 | printf("main is done\n"); 18 | return(0); // = exit 19 | } 20 | 21 | static void 22 | my_exit1(void) 23 | { 24 | printf("first exit handler\n"); 25 | } 26 | 27 | static void 28 | my_exit2(void) 29 | { 30 | printf("second exit handler\n"); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /apue/procenviron/echoarg.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int main(int argc, char **argv){ 4 | int i; 5 | for(i = 0; argv[i]!= NULL; i++) 6 | printf("argv[%d] : %s\n", i, argv[i]); 7 | exit(0); 8 | } 9 | -------------------------------------------------------------------------------- /apue/procenviron/enum2str1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | enum cmd{ 6 | CMD_UNSPEC = 0, 7 | CMD_ADD, 8 | CMD_SUB, 9 | CMD_MULTI, 10 | CMD_DIV, 11 | }; 12 | static inline char * cmd_str(enum cmd c){ 13 | static const char *strings[] = {"CMD_UNSPEC", "CMD_ADD", "CMD_SUB", "CMD_MULTI", "CMD_DIV", }; 14 | return strings[c]; 15 | } 16 | 17 | int main(void){ 18 | 19 | printf("%s\n", cmd_str(CMD_ADD)); 20 | 21 | exit(0); 22 | } 23 | -------------------------------------------------------------------------------- /apue/procenviron/enum2str2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | /*将相应的枚举类型转为字符串(这种方法很重要)*/ 6 | #define FOREACH_CMD(CMD) \ 7 | CMD(CMD_UNSPEC) \ 8 | CMD(CMD_ADD) \ 9 | CMD(CMD_SUB) \ 10 | CMD(CMD_MULTI) \ 11 | 12 | #define GENERATE_ENUM(ENUM) ENUM, 13 | #define GENERATE_STRING(STRING) #STRING, 14 | 15 | enum CMD_ENUM { 16 | FOREACH_CMD(GENERATE_ENUM) 17 | }; 18 | 19 | static const char *CMD_STRING[] = { 20 | FOREACH_CMD(GENERATE_STRING) 21 | }; 22 | 23 | int main(void){ 24 | printf("%s\n", CMD_STRING[CMD_ADD]); 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /apue/procenviron/getenv_demo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | 6 | printf("HOME - %s\n", getenv("HOME")); 7 | printf("PATH - %s\n", getenv("PATH")); 8 | printf("LOGNAME - %s\n", getenv("LOGNAME")); 9 | printf("PWD - %s\n", getenv("PWD")); 10 | printf("TERM - %s\n", getenv("TERM")); 11 | 12 | return 0; 13 | } 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /apue/procenviron/hello1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | printf("hello, world\n"); 6 | } 7 | 8 | -------------------------------------------------------------------------------- /apue/procenviron/opendata.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DATAFILE "datafile" 4 | 5 | FILE * 6 | open_data(void) 7 | { 8 | FILE *fp; 9 | char databuf[BUFSIZ]; /* setvbuf makes this the stdio buffer */ 10 | 11 | if ((fp = fopen(DATAFILE, "r")) == NULL) 12 | return(NULL); 13 | if (setvbuf(fp, databuf, _IOLBF, BUFSIZ) != 0) 14 | return(NULL); 15 | return(fp); /* error */ 16 | } 17 | 18 | void func(int x){ 19 | int a = 9; 20 | printf("%d\n", x + 9); 21 | } 22 | 23 | int main(){ 24 | 25 | FILE *file = open_data(); 26 | printf("==============\n"); 27 | func(10); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /apue/procenviron/tokenize.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char** tokenize(const char* input, int *countp) 6 | { 7 | char *str = strdup(input);// 复制一份进行操作 8 | int count = 0; 9 | int capacity = 10; //初始容量是10个单词,满后倍增 10 | char **result = malloc(capacity*sizeof(*result)); 11 | //printf("%d\n", sizeof(*result)); 12 | char *tok=strtok(str," "); 13 | 14 | while(1) 15 | { 16 | if (count >= capacity) 17 | result = realloc(result, (capacity *= 2) * sizeof(*result)); 18 | 19 | result[count++] = tok ? strdup(tok) : tok; // 数组最后一项为NULL 20 | 21 | if (!tok) break; 22 | 23 | tok=strtok(NULL," "); 24 | } 25 | *countp = count - 1; 26 | free(str); 27 | return result; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /apue/procenviron/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/ptrace/README.txt: -------------------------------------------------------------------------------- 1 | 2 | ptrace0 - 在tracer收到由于系统调用而来的控制时,设置单步执行 从而简单对指令计数 3 | ptrace1 - 得到系统调用号 4 | ptrace2 - 获得系统调用的参数 和 返回值 PTRACE_SYSCALL 设置的是在系统调用的入口和出口 5 | 都会停止子进程的执行,这样可以在write退出的时候从eax寄存器得到它的返回值 6 | ptrace3 - 利用PTRACE_GETREGS获得系统调用时寄存器值 7 | ptrace4 - 改变tracee中的内存单元内容 每次读取的是一个word 8 | ptrace5 - 在系统调用之后单步执行,获得正在执行的指令 9 | attach.c dummy2.c - 探测非孩子进程 用PTRACE_ATTACH 10 | breakpoint.c - 插入断点INT3 11 | breakpoint2.c - 向另一个进程空间中插入代码片段 12 | -------------------------------------------------------------------------------- /apue/ptrace/dummy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/ptrace/dummy -------------------------------------------------------------------------------- /apue/ptrace/dummy1.s: -------------------------------------------------------------------------------- 1 | .data 2 | hello: 3 | .string "hello vonzhou\n" 4 | .global main 5 | main: 6 | movl $4,%eax 7 | movl $2,%ebx 8 | movl $hello, %ecx 9 | movl $14, %edx 10 | int $0x80 11 | movl $1, %eax 12 | xorl %ebx,%ebx 13 | int $0x80 14 | ret 15 | -------------------------------------------------------------------------------- /apue/ptrace/dummy2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i; 5 | for(i=0; i < 10;i ++){ 6 | //printf("My counter : %d\n", i); 7 | sleep(2); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /apue/ptrace/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/ptrace/hello -------------------------------------------------------------------------------- /apue/ptrace/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | printf("Hello Vonzhou.\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /apue/ptrace/hello2.c: -------------------------------------------------------------------------------- 1 | 2 | int main(){ 3 | __asm__( 4 | "jmp forward;" 5 | "backward:popl %esi;" 6 | "movl $4, %eax;" 7 | "movl $2, %ebx;" 8 | "movl %esi, %ecx;" 9 | "movl $12, %edx;" 10 | "int $0x80;" 11 | "int3;" 12 | "forward:call backward;" 13 | ".string \"Hello World\\n\";" 14 | ); 15 | } 16 | -------------------------------------------------------------------------------- /apue/ptrace/ptrace1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include // ORIG_EAX 8 | int main(){ 9 | int val; 10 | pid_t pid; 11 | long orig_eax; 12 | switch(pid = fork()){ 13 | case -1: 14 | perror("fork"); 15 | break; 16 | case 0: 17 | // child process starts here 18 | ptrace(PTRACE_TRACEME, 0, 0, 0); 19 | execl("/bin/ls", "ls", NULL);\ 20 | break; 21 | default: 22 | // parent process 23 | wait(&val); 24 | orig_eax = ptrace(PTRACE_PEEKUSER, pid, 4 * ORIG_EAX, NULL); 25 | printf("The child made a syscall %ld\n", orig_eax); 26 | ptrace(PTRACE_CONT, pid, NULL, NULL); 27 | } 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /apue/signals/child.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include"util.h" 4 | #include 5 | 6 | static void sig_cld(int); 7 | 8 | int 9 | main() 10 | { 11 | pid_t pid; 12 | if (signal(SIGCLD, sig_cld) == SIG_ERR) 13 | perror("signal error"); 14 | if ((pid = fork()) < 0) { 15 | perror("fork error"); 16 | } else if (pid == 0) {/* child */ 17 | sleep(2); 18 | _exit(0); 19 | } 20 | 21 | pause();/* parent */ 22 | exit(0); 23 | } 24 | 25 | static void 26 | sig_cld(int signo)/* interrupts pause() */ 27 | { 28 | pid_t pid; 29 | int status; 30 | 31 | printf("SIGCLD received\n"); 32 | if (signal(SIGCLD, sig_cld) == SIG_ERR)/* reestablish handler */ 33 | perror("signal error" ); 34 | if ((pid = wait(&status)) < 0)/* fetch child status */ 35 | perror("wait error"); 36 | printf("pid = %d\n", pid); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /apue/signals/ed.c: -------------------------------------------------------------------------------- 1 | 2 | #include "util.h" 3 | 4 | static void sig_int(int signo){ 5 | printf("Caught SIGINT \n"); 6 | } 7 | static void sig_chld(int signo){ 8 | printf("Caught SIGCHLD \n"); 9 | } 10 | int main(){ 11 | if(signal(SIGINT, sig_int) == SIG_ERR) 12 | err_sys("signal(SIGINT) error"); 13 | if(signal(SIGCHLD, sig_chld) == SIG_ERR) 14 | err_sys("signal(SIGCHLD) error"); 15 | 16 | if(system("/bin/ed") < 0) 17 | err_sys("system(ed) error"); 18 | 19 | exit(0); 20 | } 21 | -------------------------------------------------------------------------------- /apue/signals/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/error.o -------------------------------------------------------------------------------- /apue/signals/pr_mask.c: -------------------------------------------------------------------------------- 1 | 2 | #include "util.h" 3 | #include 4 | 5 | void 6 | pr_mask(const char *str){ 7 | sigset_t sigset; 8 | int errno_save; 9 | 10 | errno_save = errno; 11 | if(sigprocmask(0, NULL, &sigset) < 0) 12 | err_sys("sigpromask failed."); 13 | printf("%s",str); 14 | if(sigismember(&sigset, SIGINT)) 15 | printf("SIGINT "); 16 | if(sigismember(&sigset, SIGQUIT)) 17 | printf("SIGQUIT "); 18 | if(sigismember(&sigset, SIGUSR1)) 19 | printf("SIGUSR1 "); 20 | if(sigismember(&sigset, SIGALRM)) 21 | printf("SIGARM "); 22 | 23 | //remaining signals can get here. 24 | printf("\n"); 25 | errno = errno_save; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /apue/signals/pr_mask.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/pr_mask.o -------------------------------------------------------------------------------- /apue/signals/read1.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "util.h" 4 | 5 | static void sig_alrm(int); 6 | 7 | int 8 | main(void) 9 | { 10 | int n; 11 | char line[MAXLINE]; 12 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 13 | err_sys("signal(SIGALRM) error"); 14 | alarm(10); 15 | if ((n = read(STDIN_FILENO, line, MAXLINE)) < 0) 16 | err_sys("read error"); 17 | alarm(0); 18 | write(STDOUT_FILENO, line, n); 19 | exit(0); 20 | } 21 | 22 | static void 23 | sig_alrm(int signo) 24 | { 25 | /* nothing to do, just return to interrupt the read */ 26 | } 27 | /* 28 | vonzhou@de15:~/Coding/apue/signals$ ./a.out 29 | read error: Interrupted system call (如果长时间不输入,则发现系统调用被中断) 30 | vonzhou@de15:~/Coding/apue/signals$ ./a.out 31 | oewutjowerjtlewrt 32 | oewutjowerjtlewrt 33 | vonzhou@de15:~/Coding/apue/signals$ 34 | */ 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /apue/signals/read2.c: -------------------------------------------------------------------------------- 1 | 2 | #include "util.h" 3 | #include 4 | 5 | static void sig_alrm(int); 6 | static jmp_buf env_alrm; 7 | 8 | int 9 | main(void) 10 | { 11 | int n; 12 | char line[MAXLINE]; 13 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 14 | err_sys("signal(SIGALRM) error"); 15 | if (setjmp(env_alrm) != 0) 16 | err_quit("read timeout"); 17 | 18 | alarm(10); 19 | if ((n = read(STDIN_FILENO, line, MAXLINE)) < 0) 20 | err_sys("read error"); 21 | alarm(0); 22 | 23 | write(STDOUT_FILENO, line, n); 24 | exit(0); 25 | } 26 | 27 | static void 28 | sig_alrm(int signo) 29 | { 30 | longjmp(env_alrm, 1); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /apue/signals/recv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/recv -------------------------------------------------------------------------------- /apue/signals/reenter.c: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 在信号处理函数中调用不可重入函数 4 | */ 5 | #include "util.h" 6 | #include 7 | 8 | static void 9 | my_alarm(int signo){ 10 | struct passwd*rootptr; 11 | printf("in signal handler\n"); 12 | if ((rootptr = getpwnam("de15")) == NULL) 13 | err_sys("getpwnam(de15) error"); 14 | alarm(1); 15 | } 16 | 17 | int 18 | main(void){ 19 | struct passwd*ptr; 20 | 21 | signal(SIGALRM, my_alarm); 22 | alarm(1); 23 | for ( ; ; ) { 24 | if ((ptr = getpwnam("vonzhou")) == NULL) 25 | err_sys("getpwnam error"); 26 | if (strcmp(ptr->pw_name, "vonzhou") != 0) 27 | printf("return value corrupted!, pw_name = %s\n", ptr->pw_name); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /apue/signals/send: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/send -------------------------------------------------------------------------------- /apue/signals/signal.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | // Reliable version of signal(),using POSIX sigaction; 3 | Sigfunc *signal(int signo,Sigfunc *func){ 4 | struct sigaction act,oact; 5 | 6 | act.sa_handler = func; 7 | sigemptyset(&act.sa_mask); 8 | act.sa_flags = 0; 9 | if(signo == SIGALRM){ 10 | #ifdef SA_INTERRUPT 11 | act.sa_flags |= SA_INTERRUPT; 12 | #endif 13 | }else{ 14 | #ifdef SA_RESTART 15 | act.sa_flags |= SA_RESTART; 16 | #endif 17 | } 18 | if(sigaction(signo,&act,&oact)<0) 19 | return(SIG_ERR); 20 | return(oact.sa_handler); 21 | } 22 | -------------------------------------------------------------------------------- /apue/signals/signal.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/signal.md -------------------------------------------------------------------------------- /apue/signals/signal.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/signal.o -------------------------------------------------------------------------------- /apue/signals/signal_recv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static void sig_usr(int); 6 | 7 | int 8 | main(void){ 9 | if(signal(SIGUSR1,sig_usr) == SIG_ERR){ 10 | printf("cannot catch SIGUSR1!"); 11 | exit(-1); 12 | } 13 | 14 | for(;;) 15 | pause(); 16 | } 17 | 18 | static void sig_usr(int signo){ 19 | if(signo == SIGUSR1) 20 | printf("received SIGUSR1\n"); 21 | else printf("other signals\n"); 22 | } 23 | -------------------------------------------------------------------------------- /apue/signals/signal_send.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]){ 8 | if(argc != 2){ 9 | printf("Usage: signal_sender \n"); 10 | exit(0); 11 | } 12 | pid_t pid = (pid_t)atoi(argv[1]); 13 | kill(pid, SIGUSR1); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /apue/signals/signalintr.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | // will stop all the inttrupted signal,that is sigaction's 4 | //default behavior ,so deal with the early aspects; 5 | Sigfunc * 6 | signal_intr(int signo, Sigfunc *func) 7 | { 8 | struct sigaction act, oact; 9 | act.sa_handler = func; 10 | sigemptyset(&act.sa_mask); 11 | act.sa_flags = 0; 12 | 13 | #ifdef SA_INTERRUPT 14 | act.sa_flags |= SA_INTERRUPT; 15 | #endif 16 | if (sigaction(signo, &act, &oact) < 0) 17 | return(SIG_ERR); 18 | return(oact.sa_handler); 19 | } 20 | -------------------------------------------------------------------------------- /apue/signals/sigsetops.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | /** 5 | * 假设把 sigset_t 视为整数,各种位操作 6 | */ 7 | /* usually defines NSIG to include signa l number 0 */ 8 | #define SIGBAD(signo)((signo) <= 0 || (signo) >= NSIG) 9 | 10 | int sigaddset(sigset_t *set, int signo) 11 | { 12 | if (SIGBAD(signo)) { errno = EINVAL; return(-1); } 13 | 14 | *set |= 1 << (signo - 1);/* turn bit on */ 15 | return(0); 16 | } 17 | 18 | int 19 | sigdelset(sigset_t *set, int signo) 20 | { 21 | if (SIGBAD(signo)) { errno = EINVAL; return(-1); } 22 | *set &= ~(1 << (signo - 1));/* turn bit off */ 23 | retur n(0); 24 | } 25 | 26 | int 27 | sigismember(const sigset_t *set, int signo) 28 | { 29 | if (SIGBAD(signo)) { errno = EINVAL; return(-1); } 30 | return((*set & (1 << (signo - 1))) != 0); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /apue/signals/sigusr.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | static void sig_usr(int); 4 | 5 | int 6 | main(void){ 7 | if(signal(SIGUSR1,sig_usr) == SIG_ERR) 8 | err_sys("cannot catch SIGUSR1!"); 9 | if(signal(SIGUSR2,sig_usr) == SIG_ERR) 10 | err_sys("cannot catch SIGUSR2!"); 11 | for(;;) 12 | pause(); 13 | } 14 | 15 | static void sig_usr(int signo){ 16 | if(signo == SIGUSR1) 17 | printf("received SIGUSR1\n"); 18 | else if(signo == SIGUSR2) 19 | printf("received SIGUSR2\n"); 20 | else err_dump("received signal %d\n",signo); 21 | } 22 | -------------------------------------------------------------------------------- /apue/signals/sleep1.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | 6 | /**存在的问题是,可能在一个繁忙的系统中调用pause之前超时, 7 | * 直接调用信号处理程序,pause后如果没有捕捉到其他信号, 8 | * 该程序将永远被挂起 9 | */ 10 | static void 11 | sig_alrm(int signo) 12 | { 13 | /* nothing to d o, just return to wake up the pause */ 14 | } 15 | 16 | unsigned int 17 | sleep1(unsigned int nsecs) 18 | { 19 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 20 | return(nsecs); 21 | alarm(nsecs);/* start the timer */ 22 | pause();/* next caught signal wakes us up */ 23 | return(alarm(0));/* turn off timer, return unslept time */ 24 | } 25 | 26 | -------------------------------------------------------------------------------- /apue/signals/sleep2.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | static jmp_buf env_alrm; 7 | 8 | static void 9 | sig_alrm(int signo) 10 | { 11 | longjmp(env_alrm, 1); 12 | } 13 | 14 | unsigned int 15 | sleep2(unsigned int nsecs) 16 | { 17 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 18 | return(nsecs); 19 | if(setjmp(env_alrm) == 0){ 20 | alarm(nsecs);/* start the timer */ 21 | pause();/* next caught signal wakes us up */ 22 | } 23 | 24 | return(alarm(0));/* turn off timer, return unslept time */ 25 | } 26 | 27 | -------------------------------------------------------------------------------- /apue/signals/sleep2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/sleep2.o -------------------------------------------------------------------------------- /apue/signals/system.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/system.o -------------------------------------------------------------------------------- /apue/signals/system1.c: -------------------------------------------------------------------------------- 1 | // prag : 8-12 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | int 8 | system(const char *cmdstring) /* version without signal handling */ 9 | { 10 | pid_t pid; 11 | int status; 12 | 13 | if (cmdstring == NULL) 14 | return(1); /* always a command processor with UNIX */ 15 | 16 | if ((pid = fork()) < 0) { 17 | status = -1; /* probably out of processes */ 18 | } else if (pid == 0) { /* child */ 19 | execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); 20 | printf("execl error\n"); // 不会执行到这里 21 | _exit(127); /* execl error */ 22 | } else { /* parent */ 23 | while (waitpid(pid, &status, 0) < 0) { 24 | if (errno != EINTR) { 25 | status = -1; /* error other than EINTR from waitpid() */ 26 | break; 27 | } 28 | } 29 | } 30 | 31 | return(status); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /apue/signals/system1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/signals/system1.o -------------------------------------------------------------------------------- /apue/signals/timer/clock.hh: -------------------------------------------------------------------------------- 1 | 2 | uint64_t clock(); //microseconds 3 | 4 | static constexpr uint64_t CLOCK_PRECISION = 1000; 5 | -------------------------------------------------------------------------------- /apue/signals/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/threadctl/detach.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 程序清单 12-1 以分离状态创建的线程 3 | */ 4 | 5 | #include "util.h" 6 | #include 7 | 8 | int 9 | makethread(void *(*fn)(void *), void *arg) 10 | { 11 | int err; 12 | pthread_t tid; 13 | pthread_attr_t attr; 14 | 15 | err = pthread_attr_init(&attr); 16 | if (err != 0) 17 | return err; 18 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 19 | if (err == 0) 20 | err = pthread_create(&tid, &attr, fn, arg); 21 | pthread_attr_destroy(&attr); 22 | return err; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /apue/threadctl/genenv1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 程序清单 12-3 getenv 的非可重入版本 3 | * 4 | * 因为所有调用 getenv() 的线程返回的字符串都存放在一个静态缓冲区中,所以这个 5 | * 版本不是可重入的。如果两个线程同时调用这个函数,就会看到不一致的结果。 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | static char envbuf[ARG_MAX]; 12 | 13 | extern char **environ; 14 | 15 | char * 16 | getenv(const char *name) 17 | { 18 | int i, len; 19 | 20 | len = strlen(name); 21 | for (i = 0; environ[i] != NULL; i++) { 22 | if ((strcmp(name, environ[i], len) == 0) && 23 | (environ[i][len] == '=')) { 24 | strcpy(envbuf, &environ[i][len+1]); 25 | return envbuf; 26 | } 27 | } 28 | return NULL; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /apue/threadctl/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/threads/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 1. 打印线程ID --threadids; 3 | 2. 获得线程的退出状态 -- exitstatus ; 4 | 3. 在把自动变量作为pthread_exit的参数时发生的问题 --badexit ; 5 | 4. 线程清理处理程序 -- cleanup; 6 | 4. 使用互斥量保护数据结构 mutex1; 7 | 5. 两个互斥量的使用方法 mutex2; 8 | 8. 使用读写锁 -- rwlock ; 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | (1) 子线程循环10次 主线程循环100次 接着子线程10次 主线程100次 如此循环50次 使用条件变量实现 TwoThreadLoop ; 19 | (2) 编写一个程序 开启3个线程ID分别为A,B,C 每个线程将自己的ID打印到屏幕上10遍 输出的结果按照ABC的顺序 如ABCABC...一次类推 ThreeThreadPrintABC; 20 | (3) 有四个线程1 2 3 4,线程1输出1 线程2输出2以此类推 现有四个文件ABCD 初始为空 现在让四个文件呈现如下格式:A-123412.. B-23412341.. C-34123412.. D-41234123.. Print1234ToFiles ; 21 | (4) 生产者消费者问题: 22 | 用条件变量实现 -- ProducerConsumer1 ; 23 | 用信号量实现 -- ProducerCusumer2 ; 24 | 25 | (5) 读者写者问题:有一个写者很多读者 多个读者可以同时读文件 写者在写文件时不许读 同样有读者时不能写 ReaderWriter ; 26 | 27 | 28 | -------------------------------------------------------------------------------- /apue/threads/condvar.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 程序清单 11-9 使用条件变量 3 | */ 4 | 5 | #include 6 | 7 | struct msg { 8 | struct msg *m_next; 9 | /* ... more stuff ... */ 10 | }; 11 | 12 | struct msg *workq; 13 | pthread_cond_t qready = PTHREAD_COND_INITIALIZER; 14 | pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; 15 | 16 | void 17 | process_msg(void) 18 | { 19 | struct msg *mp; 20 | 21 | for (;;) { 22 | pthread_mutex_lock(&qlock); 23 | while (workq == NULL) 24 | pthread_cond_wait(&qready, &qlock); 25 | mp = workq; 26 | workq = mp->m_next; 27 | pthread_mutex_unlock(&qlock); 28 | /* now process the message mp */ 29 | } 30 | } 31 | 32 | void 33 | enqueue_msg(struct msg *mp) 34 | { 35 | pthread_mutex_lock(&qlock); 36 | mp->m_next = workq; 37 | workq = mp; 38 | pthread_mutex_unlock(&qlock); 39 | pthread_cond_signal(&qready); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /apue/threads/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/threads/error.o -------------------------------------------------------------------------------- /apue/threads/exit_status.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | 4 | int 5 | main(void){ 6 | pid_t pid; 7 | int status; 8 | 9 | if((pid=fork()) < 0) 10 | err_sys("fork error"); 11 | else if(pid == 0) 12 | exit(7);// child process exit 13 | //wait child 14 | if(wait(&status) != pid) 15 | err_sys("wait child error"); 16 | pr_exit(status); 17 | 18 | if((pid=fork()) < 0) 19 | err_sys("fork error"); 20 | else if(pid == 0) 21 | abort(); // generate SIGABRT; 22 | 23 | if(wait(&status) != pid) 24 | err_sys("wait child error"); 25 | pr_exit(status); 26 | 27 | if((pid=fork()) < 0) 28 | err_sys("fork error"); 29 | else if(pid == 0) 30 | status /= 0; // divide by 0 generate SIGFPE; 31 | 32 | if(wait(&status) != pid) 33 | err_sys("wait child error"); 34 | pr_exit(status); 35 | 36 | exit(0); 37 | } 38 | -------------------------------------------------------------------------------- /apue/threads/threadid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/threads/threadid -------------------------------------------------------------------------------- /apue/threads/threadid.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | 4 | pthread_t ntid; 5 | 6 | void 7 | printids(const char *s) 8 | { 9 | pid_t pid; 10 | pthread_t tid; 11 | 12 | pid = getpid(); 13 | tid = pthread_self(); 14 | //之所以打印16进制,便于pthread_t是结构体的话看地址; 15 | printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid, 16 | (unsigned int)tid, (unsigned int)tid); 17 | } 18 | 19 | void * 20 | thr_fn(void *arg) 21 | { 22 | printids("new thread: "); 23 | return((void *)0); 24 | } 25 | 26 | int 27 | main(void) 28 | { 29 | int err; 30 | 31 | err = pthread_create(&ntid, NULL, thr_fn, NULL); 32 | if (err != 0) 33 | err_quit("can't create thread: %s\n", strerror(err)); 34 | printids("main thread:"); 35 | sleep(1);//确保会执行新线程 36 | exit(0); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /apue/threads/threadid.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/apue/threads/threadid.o -------------------------------------------------------------------------------- /apue/threads/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /apue/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include // pipe(), 6 | 7 | 8 | #define MAXLINE 50 9 | 10 | 11 | void err_exit(int errno,const char *fmt,...); 12 | void err_quit(const char *fmt,...); 13 | void err_sys(const char *fmt,...); 14 | void err_dump(const char *fmt,...); 15 | 16 | 17 | 18 | typedef void Sigfunc(int); /*for signal handlers*/ 19 | Sigfunc *signal(int signo,Sigfunc *func); 20 | void pr_mask(const char *str); 21 | void pr_exit(int status); 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /csapp/README: -------------------------------------------------------------------------------- 1 | 2 | 3 | 深入理解计算机系统结构 4 | 5 | bin 书中脚本(使用lua) 6 | code 书中源码,包括习题 7 | -------------------------------------------------------------------------------- /csapp/bin/d2h.lua: -------------------------------------------------------------------------------- 1 | 2 | -- convert list of decimal numbers to hex 3 | require("string") 4 | 5 | local a =1 6 | while arg[a] do 7 | --print(arg[a]) 8 | print(string.format("%d = 0x%x \n", arg[a], arg[a])) 9 | a =a + 1 10 | end 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /csapp/bin/h2d.lua: -------------------------------------------------------------------------------- 1 | 2 | -- convert list of hex numbers(headed with 0x..) to decamal 3 | require("string") 4 | 5 | local a =1 6 | while arg[a] do 7 | -- turn 0x.. to decimal 8 | de = tonumber(arg[a]) 9 | print(string.format("0x%x = %d \n", de, de)) 10 | a =a + 1 11 | end 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /csapp/chapter7/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int M = 100; 5 | 6 | int main(){ 7 | printf("Hello World!\n"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /csapp/code/data/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 第二章 信息的表示和处理 4 | -------------------------------------------------------------------------------- /csapp/code/data/data.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _DATA_H 3 | #define _DATA_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | 11 | 12 | 13 | /*show bytes of memory*/ 14 | typedef unsigned char *byte_pointer; 15 | void show_bytes(byte_pointer, int len); 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /csapp/code/data/demo22.c: -------------------------------------------------------------------------------- 1 | 2 | #include "data.h" 3 | 4 | int main(){ 5 | short sx = -12345; 6 | unsigned uy = sx; /*NB*/ 7 | 8 | printf("uy = %u:\t", uy); 9 | show_bytes((byte_pointer)&uy, sizeof(unsigned)); 10 | 11 | printf("------------\n"); 12 | 13 | int z = sx; 14 | printf("z = %d:\t", z); 15 | show_bytes((byte_pointer)&z, sizeof(int)); 16 | 17 | return 0; 18 | } 19 | 20 | /* 21 | vonzhou@de15:~/Coding/csapp/code/data$ gcc demo22.c show_bytes.o 22 | vonzhou@de15:~/Coding/csapp/code/data$ ./a.out 23 | uy = 4294954951:c7cfffff 24 | ------------ 25 | z = -12345:c7cfffff 26 | * */ 27 | -------------------------------------------------------------------------------- /csapp/code/data/demo23.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int fun1(unsigned word){ 6 | return (int)((word << 24) >> 24); 7 | } 8 | 9 | int fun2(unsigned word){ 10 | return (((int)word << 24) >> 24); 11 | } 12 | 13 | int main(){ 14 | unsigned x = 0x87654321; 15 | printf("%x\n", fun1(x)); 16 | printf("%x\n", fun2(x)); 17 | 18 | x = 0xedcba987; 19 | printf("%x\n", fun1(x)); 20 | printf("%x\n", fun2(x)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /csapp/code/data/demo25.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | float sum_elements(float a[], unsigned length){ 5 | int i; 6 | float res = 0; 7 | for(i = 0; i <= length -1; i++) 8 | res += a[i]; 9 | return res; 10 | } 11 | 12 | float sum_elements2(float a[], unsigned length){ 13 | int i; 14 | float res = 0; 15 | // or make length to int type 16 | for(i = 0; i < length ; i++) 17 | res += a[i]; 18 | return res; 19 | } 20 | 21 | int main(){ 22 | float a[] = {3,14, 5678, 1,3,7}; 23 | //float res1 = sum_elements(a, 0);/* coredump !!*/ 24 | float res1 = sum_elements2(a, 0); 25 | printf("result is %f\n", res1); 26 | 27 | return 0; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /csapp/code/data/demo26.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int strlonger(char *s, char *t){ 6 | return strlen(s)-strlen(t) > 0; 7 | } 8 | 9 | int strlonger2(char *s, char *t){ 10 | return strlen(s) > strlen(t); // 直接关系运算 11 | } 12 | 13 | void test1(){ 14 | char *s = "abcd"; 15 | char *t = "a"; 16 | printf("test1 : %d\n", strlonger(s, t)); 17 | printf("test1 : %d\n", strlonger2(s, t)); 18 | } 19 | 20 | void test2(){ 21 | char *s = "abcd"; 22 | char *t = "vonzhou"; 23 | printf("test2 : %d\n", strlonger(s, t)); 24 | printf("test2 : %d\n", strlonger2(s, t)); 25 | } 26 | 27 | void test3(){ 28 | char *s = ""; 29 | char *t = ""; 30 | printf("test1 : %d\n", strlonger(s, t)); 31 | printf("test2 : %d\n", strlonger2(s, t)); 32 | } 33 | 34 | int main(){ 35 | test1(); 36 | test2(); 37 | test3(); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /csapp/code/data/demo27.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /*Determine wether arguments can be added without overflow*/ 4 | int uadd_ok(unsigned x, unsigned y){ 5 | unsigned sum = x + y; 6 | return sum >= x; 7 | } 8 | 9 | int main(){ 10 | unsigned x = 0xffffffff, y = 3; 11 | printf("uadd_ok : %d\n", uadd_ok(x, y)); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /csapp/code/data/demo30.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | /*Whether arguments can be added without overflow*/ 5 | int tadd_ok(int x, int y){ 6 | int sum = x + y; 7 | int neg_overflow = x < 0 && y < 0 && sum >= 0; 8 | int pos_overflow = x >= 0 && y>= 0 && sum < 0; 9 | if(neg_overflow) 10 | printf("negative overflow....\n"); 11 | if(pos_overflow) 12 | printf("positive overflow...\n"); 13 | return !neg_overflow && !pos_overflow; 14 | } 15 | 16 | int main(){ 17 | int x = 0x80000000; 18 | int y = 0x87654321; 19 | printf("%d\n", tadd_ok(x, y)); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /csapp/code/data/demo31.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | /*Not work !!!*/ 5 | int tadd_ok(int x, int y){ 6 | int sum = x + y; 7 | return (sum-x == y) && (sum-y == x); 8 | } 9 | 10 | int main(){ 11 | int x = 0x80000000; 12 | int y = 0x87654321; 13 | printf("overflow but return %d\n", tadd_ok(x, y)); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /csapp/code/data/demo35.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 习题 2.35*/ 4 | 5 | int tmult_ok(int x, int y){ 6 | int p = x * y; 7 | return !x || p/x == y; 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /csapp/code/data/demo36.c: -------------------------------------------------------------------------------- 1 | 2 | /* 习题 2.36*/ 3 | 4 | #include 5 | #include 6 | 7 | /*using long long */ 8 | int tmult_ok(int x, int y){ 9 | long long pll = (long long )x * y; // must cast 10 | return pll == (int)pll; 11 | } 12 | 13 | 14 | int main(){ 15 | int ele_cnt = 1048577; 16 | int ele_size = 4096; 17 | printf("sizeof long long :%d\n", sizeof(long long)); 18 | printf("sizeof long :%d\n", sizeof(long)); 19 | printf("mult test : %d\n", tmult_ok(ele_cnt, ele_size)); 20 | exit(0); 21 | } 22 | /* 23 | sizeof long long :8 24 | sizeof long :8 25 | mult test : 0 26 | * */ 27 | 28 | -------------------------------------------------------------------------------- /csapp/code/data/extend_bits.c: -------------------------------------------------------------------------------- 1 | 2 | #include "data.h" 3 | 4 | /*P49*/ 5 | void test(){ 6 | short sx = -12345; 7 | unsigned short usx = sx; 8 | int x = sx; 9 | unsigned ux = usx; 10 | 11 | printf("sx = %d:\t", sx); 12 | show_bytes((byte_pointer)&sx, sizeof(short)); 13 | printf("usx = %u:\t", usx); 14 | show_bytes((byte_pointer)&usx, sizeof(unsigned short)); 15 | printf("x = %d:\t", x); 16 | show_bytes((byte_pointer)&x, sizeof(int)); 17 | printf("ux = %u:\t", ux); 18 | show_bytes((byte_pointer)&ux, sizeof(unsigned int)); 19 | } 20 | 21 | int main(){ 22 | test(); 23 | return 0; 24 | } 25 | 26 | 27 | /* 28 | vonzhou@de15:~/Coding/csapp/code/data$ gcc extend_bits.c 29 | vonzhou@de15:~/Coding/csapp/code/data$ ./a.out 30 | sx = -12345:c7c f 31 | usx = 53191:c7cf 32 | x = -12345:c7cfffff 33 | ux = 53191:c7cf0000 34 | */ 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /csapp/code/data/inplace_swap.c: -------------------------------------------------------------------------------- 1 | 2 | /*习题 2.10*/ 3 | 4 | #include 5 | 6 | void inplace_swap(int *x, int *y){ 7 | *y = *x ^ *y; 8 | *x = *x ^ *y; 9 | *y = *x ^ *y; 10 | } 11 | 12 | int main(){ 13 | int x = 100; 14 | int y = 9999; 15 | printf("x= %d, y = %d\n", x, y); 16 | inplace_swap(&x, &y); 17 | printf("x= %d, y = %d\n", x, y); 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /csapp/code/data/reverse_array.c: -------------------------------------------------------------------------------- 1 | 2 | /*习题 2.11*/ 3 | 4 | #include 5 | 6 | void inplace_swap(int *x, int *y){ 7 | *y = *x ^ *y; 8 | *x = *x ^ *y; 9 | *y = *x ^ *y; 10 | } 11 | 12 | void reverse_array(int a[], int cnt){ 13 | int first = 0, last = cnt - 1; 14 | for(; first < last; first++, last --) 15 | inplace_swap(&a[first], &a[last]); 16 | } 17 | 18 | void show_array(int a[], int cnt){ 19 | int i=0; 20 | for(; i < cnt; i++) 21 | printf("%d ", a[i]); 22 | printf("\n"); 23 | } 24 | 25 | void test_odd(){ 26 | int a[] = {1, 3, 5, 7, 9}; 27 | reverse_array(a, 5); 28 | show_array(a, 5); 29 | } 30 | 31 | void test_even(){ 32 | int a[] = {1, 3, 5, 7, 9, 11}; 33 | reverse_array(a, 6); 34 | show_array(a, 6); 35 | } 36 | 37 | int main(){ 38 | test_odd(); 39 | test_even(); 40 | return 0; 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /csapp/code/data/show_bytes.c: -------------------------------------------------------------------------------- 1 | 2 | #include "data.h" 3 | 4 | void show_bytes(byte_pointer start, int len){ 5 | int i =0; 6 | for(i = 0; i < len ;i++) 7 | printf("%.2x", start[i]); 8 | printf("\n"); 9 | } 10 | 11 | void show_int(int x){ 12 | show_bytes((byte_pointer)&x, sizeof(int)); 13 | } 14 | 15 | void show_float(float f){ 16 | show_bytes((byte_pointer)&f, sizeof(f)); 17 | } 18 | 19 | void show_pointer(void *x){ 20 | show_bytes((byte_pointer)&x, sizeof(void *)); 21 | } 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /csapp/code/data/show_bytes.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/csapp/code/data/show_bytes.o -------------------------------------------------------------------------------- /ctraps/1.5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | char x = 'yes'; 5 | printf("%d\n", x); 6 | printf("%d\n", 's'); 7 | } 8 | -------------------------------------------------------------------------------- /ctraps/2.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef void (*funcptr)(); // typedef makes it clear 4 | 5 | void func(){ 6 | printf("entering func.....\n"); 7 | } 8 | 9 | int main(){ 10 | void *vp = (void *)func; 11 | (*(void (*)())vp)(); 12 | ((void (*)())vp)(); // NB: an abbreviated form 13 | (*(funcptr)vp)(); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /ctraps/2.3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct logrec{ 4 | int data; 5 | int time; 6 | int code; 7 | } 8 | 9 | 10 | main(){ 11 | 12 | printf("hello vonzhou ...\n"); 13 | return 0;// error 14 | } 15 | 16 | -------------------------------------------------------------------------------- /ctraps/2.ex1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int days1[] = {31, 28, 31, 30, 31, 30, 4 | 31, 31, 30, 31, 30, 31}; 5 | int days[] = {31, 28, 31, 30, 31, 30, 6 | 31, 31, 30, 31, 30, 31,}; 7 | 8 | 9 | int main(){ 10 | int i, sum=0; 11 | for(i = 0; i< 12; i++) 12 | sum += days[i]; 13 | days[30] = 999; 14 | days1[12] = 888; 15 | printf("%d\n", sum); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /ctraps/3.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void show(int c[12][31]){ 4 | 5 | 6 | } 7 | 8 | 9 | int main(){ 10 | int calendar[12][31]; 11 | int *p; 12 | int i; 13 | 14 | p = calendar[4]; 15 | printf("%d\n", sizeof(p)); // 8 16 | printf("%d\n", sizeof(calendar[4])); // 31*4 = 124 17 | 18 | *(p+7) = 90; 19 | int (*monthp)[31]; // pointer to array 20 | monthp = calendar; 21 | printf("%d\n",*( (int *)(monthp + 4) + 7)); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ctraps/5.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | char c; 6 | while((c = getchar()) != EOF) 7 | putchar(c); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /ctraps/5.3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int c; 6 | 7 | //static char buf[BUFSIZ]; 8 | setbuf(stdout, malloc(BUFSIZ)); 9 | printf("BUFSIZ : %d\n", BUFSIZ); 10 | while((c = getchar()) != EOF) 11 | putchar(c); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /ctraps/5.3a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func(){ 5 | int c; 6 | 7 | //static char buf[BUFSIZ]; 8 | setbuf(stdout, malloc(BUFSIZ)); 9 | printf("BUFSIZ : %d\n", BUFSIZ); 10 | while((c = getchar()) != EOF) 11 | putchar(c); 12 | 13 | } 14 | 15 | 16 | int main(){ 17 | func(); 18 | printf("---------in main---------\n"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ctraps/5.3b.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | 6 | putchar('a'); 7 | putchar('b'); 8 | 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /ctraps/6.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #define f(x) ((x) - 1) 5 | 6 | 7 | int main(){ 8 | printf("%d\n", f(3)); 9 | 10 | printf("%d\n", f (3)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /ctraps/6.3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define assert(e) \ 5 | { if(!(e)) assert_error(__FILE__, __LINE__); } 6 | 7 | void assert_error(char *filename, int line){ 8 | printf("Error ouccurred in %s at line:%d\n", filename, line); 9 | } 10 | 11 | 12 | int main(){ 13 | int flag = 1; 14 | int y = 8; 15 | if(flag) 16 | assert(y > 0) 17 | else printf("hhhhhhhhh\n"); 18 | printf("%f\n", sqrt(y)); 19 | // printf("test, %s--%d\n", __FILE__, __LINE__); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ctraps/6.3a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define assert(e) \ 6 | ((void)((e) || _assert_error(__FILE__, __LINE__))) 7 | // { if(!(e)) assert_error(__FILE__, __LINE__); } 8 | 9 | int _assert_error(char *filename, int line){ 10 | printf("Error ouccurred in %s at line:%d\n", filename, line); 11 | exit(-1); 12 | } 13 | 14 | 15 | int main(){ 16 | int flag = 1; 17 | int y = -8; 18 | if(flag) 19 | assert(y > 0); 20 | else printf("hhhhhhhhh\n"); 21 | printf("%f\n", sqrt(y)); 22 | // printf("test, %s--%d\n", __FILE__, __LINE__); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ctraps/6.3b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define assert(e) \ 6 | ((void)((e) || _assert_error(__FILE__, __LINE__))) 7 | // { if(!(e)) assert_error(__FILE__, __LINE__); } 8 | 9 | void _assert_error(char *filename, int line){ 10 | printf("Error ouccurred in %s at line:%d\n", filename, line); 11 | exit(0); 12 | } 13 | 14 | 15 | int main(){ 16 | int flag = 1; 17 | int y = -8; 18 | if(flag) 19 | assert(y > 0); 20 | else printf("hhhhhhhhh\n"); 21 | printf("%f\n", sqrt(y)); 22 | // printf("test, %s--%d\n", __FILE__, __LINE__); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ctraps/7.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a=99; 4 | 5 | int A =9; 6 | 7 | 8 | double square(double x){ 9 | return x*x; 10 | } 11 | 12 | int main(){ 13 | 14 | printf("%g\n", square(3)); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ctraps/7.11.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void printnum(long n, void (*p)()){ 6 | if(n < 0){ 7 | (*p)('-'); 8 | n = -n; 9 | } 10 | 11 | if(n >= 10) 12 | printnum(n/10, p); 13 | 14 | (*p)((int)(n % 10) + '0'); 15 | 16 | } 17 | 18 | void func(char c){ 19 | printf("%c\n",c); 20 | } 21 | 22 | int main(){ 23 | long x = 145; 24 | printnum(145, (void (*)())func); // clever !!!!! 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ctraps/7.11a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void printnum(long n, void (*p)()){ 6 | if(n < 0){ 7 | printf("n is negative .\n"); 8 | (*p)('-'); 9 | n = -n; 10 | } 11 | 12 | if(n >= 10) 13 | printnum(n/10, p); 14 | 15 | (*p)("0123456789"[n % 10]); 16 | 17 | } 18 | 19 | void func(char c){ 20 | printf("%c\n",c); 21 | } 22 | 23 | int main(){ 24 | long x = -145l; 25 | printnum(x, (void (*)())func); // clever !!!!! 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ctraps/7.4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a=99; 4 | 5 | int A =9; 6 | 7 | 8 | double square(double x){ 9 | return x*x; 10 | } 11 | 12 | int main(){ 13 | char x = (char)130; 14 | unsigned y = (unsigned)x; 15 | unsigned char z = (unsigned char )x; 16 | printf("%x %x %x\n",x,y,z);// ffffff82 ffffff82 82 17 | printf("sizeof char = %d\n", sizeof(char)); 18 | printf("%c\n", x); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /ctraps/7.5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a=99; 4 | 5 | int A =9; 6 | 7 | 8 | double square(double x){ 9 | return x*x; 10 | } 11 | 12 | int main(){ 13 | int x = 100; 14 | //unsigned y = (unsigned)x; 15 | printf("x= %x\n",x);// 16 | x >> 2; 17 | printf("shift x = %x\n", x); 18 | //printf("%c\n", x); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /ctraps/7.6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | char *p; 6 | p = NULL; 7 | printf("Location 0 contains %d\n", *p); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /ctraps/7.8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | char *p; 6 | //p = NULL; 7 | 8 | printf("Location 0 contains %d\n", RAND_MAX); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /ctraps/testexit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void func(){ 6 | 7 | exit(-1); 8 | } 9 | 10 | 11 | int main(){ 12 | func(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /expertc/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 阅读与实践 3 | 4 | 1. P57 - arr_ptr.c 测试数组指针 5 | 2. P57 - const.c const关键字的含义 6 | 3. P60 - pid_tag.c 结构体中可以存在位字段和填充字段 7 | 4. P61 - arr_struct.c 用结构体包装数组,把数组当做firt-class type 8 | 5. P63 - union.c 把同一个数据解释成两种不同的东西 9 | 6. P82 - extern_file1.c extern_file2.c 对于extern关键字的正确使用,数组名和指针的区别 10 | 7. P87 - string_literal.c 字符串常量存储的位置决定了它不能改变 11 | 8. P93, P107, P97 了解gcc的常见选项的使用 12 | 9. P97 - tomato.c , test.c 创建动态链接库 13 | 10.P100 - nm_use.sh 利用nm命令找一个符号的shell脚本 14 | 15 | -------------------------------------------------------------------------------- /expertc/arr_ptr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | int main(){ 6 | int i = 0; 7 | char cs[] = "12345678"; 8 | char (*j)[4] = cs; 9 | 10 | *(int *)(j+1) = 0; 11 | for(i=0; i<8; i++) 12 | printf("%d\t", cs[i]); 13 | 14 | printf("\n"); 15 | return 0; 16 | } 17 | /* 18 | vonzhou@de15:~/Coding/expertc$ gcc arr_ptr.c 19 | arr_ptr.c: In function ‘main’: 20 | arr_ptr.c:8:20: warning: initialization from incompatible pointer type [enabled by default] 21 | char (*j)[4] = cs; 22 | ^ 23 | vonzhou@de15:~/Coding/expertc$ ./a.out 24 | 49 50 51 52 0 0 0 0 25 | 26 | */ 27 | 28 | 29 | -------------------------------------------------------------------------------- /expertc/arr_struct.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | struct s_tag{int a[100];}; 5 | 6 | struct s_tag twofold(struct s_tag s){ 7 | int j = 0; 8 | for(j=0;j<100;j++) 9 | s.a[j] *= 2; 10 | return s; 11 | } 12 | 13 | int main(){ 14 | int i; 15 | struct s_tag orange, lime, lemon; 16 | for(i = 0; i<100; i++) 17 | lime.a[i] = i; 18 | lemon = twofold(lime); 19 | orange = lemon; 20 | 21 | for(i=0; i<100; i++) 22 | printf("%d ", lemon.a[i]); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /expertc/chapter4/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | const int a =10; 8 | int *p = (int *) (&a); 9 | *p =20; 10 | 11 | cout << a<< endl << *p < 2 | 3 | //extern int mango[]; 4 | extern int *mango; 5 | 6 | void func2(){ 7 | printf("mango declared and used here\n"); 8 | printf("mango[0] : %d\n", mango[0]); 9 | } 10 | 11 | int main(){ 12 | func(); 13 | func2(); 14 | return 0; 15 | } 16 | 17 | /* 18 | mango defined here 19 | mango declared and used here 20 | Segmentation fault (core dumped) 21 | */ 22 | -------------------------------------------------------------------------------- /expertc/chapter4/mango_defined.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int mango[100]; 5 | 6 | 7 | void func(){ 8 | printf("mango defined here\n"); 9 | int i = 0; 10 | for(i=0; i<100;i++) 11 | mango[i] = 100-i; 12 | } 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /expertc/chapter4/string_iteral.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | char *p = "vonzhouabc"; 5 | char a[] = "hello world"; 6 | 7 | p[0] = 'A'; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /expertc/const.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a = 100; 5 | int b = 10; 6 | int c = 4; 7 | int d = 1000; 8 | const int * grape = &a; 9 | int const * grape2 = &b; 10 | 11 | int * const grape_jelly = &c; 12 | 13 | const int * const grape_jam = &d; 14 | printf("a = %d, b = %d\n",a, b); 15 | //some actions 16 | a = 10; 17 | 18 | //*grape = 10;// error: assignment of read-only location ‘*grape’ 19 | //可以直接修改a 但是不能通过指针来修改 相当于访问控制 ?\ 20 | //对于b同样如此 21 | b = 100; 22 | // *grape2 = 0; // cannot 23 | 24 | grape = grape2; 25 | grape2 = &a; 26 | printf("a = %d, b = %d\n",a, b); 27 | 28 | c = 1000; 29 | // grape_jelly = grape;//error: assignment of read-only variable ‘grape_jelly’ 30 | // *grape_jelly = 0; // cannot 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /expertc/func.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char *echo(){ 4 | char str[] = "vonzhou"; // This is wrong, after teh func returned ,it dead 5 | //char *str = "vonzhou"; 6 | static str2[] = "hello"; // can work 7 | printf("-----\n"); 8 | return str; 9 | } 10 | 11 | main(){ 12 | char *p; 13 | p = echo(); 14 | printf("return: %s\n",p); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /expertc/getch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(){ 4 | int c; 5 | system("stty raw"); 6 | c = getchar(); 7 | system("stty cooked"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /expertc/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int x[1000] = {1}; 4 | int main(){ 5 | int y[1000] = {2}; 6 | printf("hello world.\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /expertc/kbhit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int kbhit(){ 4 | int i; 5 | ioctl(0, FIONREAD, &i); 6 | return i; 7 | } 8 | 9 | main(){ 10 | int i = 0; 11 | int c = ''; 12 | system("stty raw -echo"); 13 | printf("enter 'q' to quit \n"); 14 | for(; c !='q'; i++){ 15 | if(kbhit()){ 16 | c = getchar(); 17 | printf(" \n got %c, on iteration %d", c, i); 18 | } 19 | } 20 | system("stty cooked echo"); 21 | } 22 | -------------------------------------------------------------------------------- /expertc/libfruit.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/expertc/libfruit.so -------------------------------------------------------------------------------- /expertc/map: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/expertc/map -------------------------------------------------------------------------------- /expertc/nm_use.sh: -------------------------------------------------------------------------------- 1 | 2 | cd /lib/x86_64-linux-gnu/ 3 | for i in lib?* 4 | do 5 | echo $i 6 | nm $i | grep pthread_create | grep -v UNDEF 7 | done 8 | 9 | cd ~/Coding/expertc/ 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /expertc/pid_tag.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | struct pid_tag{ 5 | unsigned int inactive : 1; 6 | unsigned int : 1 ; // 1 bit padding 7 | unsigned int refcount : 6; 8 | unsigned int :0; // padding to next word bound 9 | short pid_id; 10 | //.... 11 | }; 12 | 13 | int main(){ 14 | struct pid_tag t; 15 | t.refcount = 10; 16 | t.inactive = 0; 17 | printf("%x\n", &t); 18 | //注意 cannot take address of bit-field ‘refcount’ 19 | printf("%x\n",&(t.pid_id)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /expertc/self-plus.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | int i=0; 7 | int j; 8 | for(j=0; j<5; j++){ 9 | i = i++; 10 | printf("%d\n", i); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /expertc/stack_addr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static int x = 1; 4 | int y[100]; 5 | 6 | main(){ 7 | int i; 8 | int y = 2; 9 | printf("The stack top is near %p\n",&i); 10 | printf("The stack change %p\n",&y); 11 | printf("The data section is near %p\n",&x); 12 | printf("The BSS is near %p\n",y); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /expertc/string_literal.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main(){ 5 | char *s = "abcaaaaaaaaaa"; 6 | printf("%s\n", s); 7 | return 0; 8 | } 9 | 10 | /* 11 | vonzhou@de15:~/Coding/expertc$ gcc string_literal.c 12 | vonzhou@de15:~/Coding/expertc$ size a.out 13 | text data bss dec hex filename 14 | 1148 552 8 1708 6ac a.out 15 | vonzhou@de15:~/Coding/expertc$ vi string_literal.c 16 | vonzhou@de1 5:~/Coding/expertc$ gcc string_literal.c 17 | vonzhou@de15:~/Coding/expertc$ size a.out 18 | text data bss dec hex filename 19 | 1158 552 8 1718 6b6 a.out 20 | * 当字符串常量增大10个时候,可以看到文本段也对应增大了 21 | * **/ 22 | -------------------------------------------------------------------------------- /expertc/swordoffer/README.txt: -------------------------------------------------------------------------------- 1 | 剑指offer 做题与思考 2 | -------------------------------------------------------------------------------- /expertc/swordoffer/str.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /expertc/test.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | extern void my_lib_func(); 4 | int main(){ 5 | my_lib_func(); 6 | } 7 | -------------------------------------------------------------------------------- /expertc/test_nm.c: -------------------------------------------------------------------------------- 1 | 2 | char pear[40]; 3 | static double peach; 4 | int mango = 1; 5 | static long melon = 2013; 6 | 7 | main(){ 8 | int i = 3, j, *ip; 9 | ip = malloc(sizeof(i)); 10 | pear[5] = i; 11 | peach = 2.0 * mango; 12 | } 13 | -------------------------------------------------------------------------------- /expertc/tomato.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | void my_lib_fun(){ 5 | printf("library routine called\n"); 6 | } 7 | -------------------------------------------------------------------------------- /expertc/tomato.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/expertc/tomato.o -------------------------------------------------------------------------------- /expertc/trick.c: -------------------------------------------------------------------------------- 1 | #if 0 2 | 3 | here will not enter into compile 4 | we can move the code we not need now here 5 | 6 | #endif 7 | 8 | #include 9 | 10 | int main(){ 11 | int i = 0; 12 | int arr[] = {[1]=5, [5]=10, [2]=20}; 13 | int arr2[] = {[0 ... 9] = 10, [10 ... 19] = 20, [20 ... 29] = 30}; 14 | for(i = 0; i < 6; i++) 15 | printf("arr[%d] = %d ,", i, arr[i]); 16 | printf("\n"); 17 | for(i = 0; i < 30; i++) 18 | printf("arr2[%d] = %d ,", i, arr2[i]); 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /expertc/trick2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100 3 | 4 | int main(){ 5 | int i = 0; 6 | char s1[MAX],s2[MAX]; 7 | 8 | //scanf("%[^\n]\n",s1); 9 | //read till meet '\n',and then trash the \n 10 | 11 | //scanf("%[^,]",s1); //?? also will trash the coma 12 | //scanf("%[^,],",s1); // this does not trash the coma 13 | 14 | //this * can make us skip some input 15 | //for example,we just care the last-name 16 | scanf("%*s %s",s1); 17 | 18 | printf("%s\n",s1); 19 | 20 | 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /expertc/trick3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define offset(TYPE, MEMBER) ((size_t)(&((TYPE *)0)->MEMBER)) 5 | 6 | int main(){ 7 | struct test{ 8 | int a; 9 | int b[4]; 10 | int c; 11 | }; 12 | 13 | printf("offset a : %lu\n", offset(struct test, a)); 14 | printf("offset b : %lu\n", offset(struct test, b)); 15 | printf("offset c : %lu\n", offset(struct test, c)); 16 | 17 | struct test *t = (struct test *)0; 18 | printf("%d\n", &(t->c)); // right 19 | printf("%d\n", t->c); //?but cannot do this, core dump 20 | 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /expertc/trick4.c: -------------------------------------------------------------------------------- 1 | //using inline blocks 2 | 3 | #include 4 | 5 | int main(){ 6 | printf("%d\n", ({ int n; scanf("%d", &n); n*n;})); 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /expertc/trick5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | char s[] = "vonzhou"; 5 | printf("%c\n", 2[s]); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /expertc/trick7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n; 5 | //util reach EOF(ctrl + D) 6 | while(~scanf("%d",&n)){ 7 | //logic process 8 | printf("%d^2 = %d\n", n, n*n); 9 | } 10 | 11 | // %m print success only if errno = 0 12 | printf("%m\n"); 13 | 14 | 15 | brk(0); 16 | } 17 | -------------------------------------------------------------------------------- /expertc/trick8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // ?? 4 | // implicit initiliazation of variables with 0 and 1 5 | g;main(i){ 6 | 7 | printf("%d,%d\n", g, i); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /expertc/trick9.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int a = 10; 6 | int b = 100; 7 | int *pa = &a; 8 | int *pb = &b; 9 | 10 | pa ^= pb; 11 | pb ^= pa; 12 | pa ^= pb; 13 | 14 | printf("a = %d, b = %d\n", *pa, *pb); 15 | 16 | brk(0); 17 | } 18 | -------------------------------------------------------------------------------- /expertc/union.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | union bits32_tag{ 5 | int whole; 6 | struct { 7 | char cs[4]; 8 | }byte; 9 | }value; 10 | 11 | int main(){ 12 | value.whole = 0x12345678; 13 | printf("whole : %x\n", value.whole); 14 | int i; 15 | for(i=0; i<4;i++){ 16 | printf("byte[%d] : %x\n", i, value.byte.cs[i]); 17 | } 18 | 19 | } 20 | 21 | /* 22 | vonzhou@de15:~/Coding/expertc$ gcc union.c 23 | vonzhou@de15:~/Coding/expertc$ ./a.out 24 | whole : 12345678 25 | byte[0] : 78 26 | byte[1] : 56 27 | byte[2] : 34 28 | byte[3] : 12 可以用来判断大小端模式?? 29 | * */ 30 | -------------------------------------------------------------------------------- /kern/README.txt: -------------------------------------------------------------------------------- 1 | 2 | Linux内核编程实践 --vonzhou 3 | 1. hello- 内核编程入门 4 | 2. schedule- 工作队列 schedule_delayed_work 使用方法 5 | 3. netdev-notifier - 网络设备事件通知 6 | 4. netdev-test - 网络设备API实践 7 | 5. page - 页分配管理 8 | 6. slab - slab分配器的使用及缓存管理 TODO 9 | 7. process - 获得当前进程,打印进程树结构 10 | 8. percpu - 使用每CPU变量,以及关于CPU的操作 11 | 9. jiffies - 定时器 12 | 10. syscall - 查看系统调用表的信息,以及拦截 13 | 11. irq-request 设置键盘中断处理程序 14 | 12. irq-tasklet 使用tasklet 15 | 13. memory-area 打印当前进程的所有内存区域(各个段) 16 | 14. fs-vfs - VFS基本操作,在内核操作文件读写 17 | -------------------------------------------------------------------------------- /kern/fs-vfs/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=vfs_test.c 3 | obj-m += vfs_test.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/fs-vfs/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 利用VFS命名在内核层操作文件 3 | -------------------------------------------------------------------------------- /kern/hello/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=hello.c 3 | obj-m += hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- 13 | endif 14 | -------------------------------------------------------------------------------- /kern/hello/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static int __init lkp_init(void) 6 | { 7 | printk(" <1> hello ,world..\n"); 8 | return 0; 9 | } 10 | 11 | static void __exit lkp_cleanup(void) 12 | { 13 | printk("<2> goodbye, leaving kernel space...\n"); 14 | } 15 | 16 | module_init(lkp_init); 17 | module_exit(lkp_cleanup); 18 | 19 | 20 | -------------------------------------------------------------------------------- /kern/high-mem/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=high_low.c 3 | obj-m += high_low.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/irq-request/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=kbd.c 3 | obj-m += kbd.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/irq-request/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 通过request_irq安装中断处理程序,IRQ=1是键盘中断请求号 3 | -------------------------------------------------------------------------------- /kern/irq-tasklet/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=mytasklet.c 3 | obj-m += mytasklet.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/irq-tasklet/README.txt: -------------------------------------------------------------------------------- 1 | 2 | tasklet demo in a module 3 | -------------------------------------------------------------------------------- /kern/irq-tasklet/mytasklet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct tasklet_struct my; 6 | 7 | void my_handler (unsigned long data) { 8 | pr_info("my tasklet handler haha...\n"); 9 | return ; 10 | } 11 | 12 | /* Initialize the module and a tasklet */ 13 | static int __init my_tasklet_init(void) 14 | { 15 | int result; 16 | tasklet_init(&my, my_handler, 0); 17 | pr_info("init our tasklet handler...\n"); 18 | 19 | tasklet_schedule(&my); 20 | return result; 21 | } 22 | 23 | /* Remove the interrupt handler */ 24 | static void __exit my_tasklet_exit(void) { 25 | tasklet_kill(&my); 26 | } 27 | 28 | MODULE_LICENSE ("GPL"); 29 | module_init(my_tasklet_init); 30 | module_exit(my_tasklet_exit); 31 | 32 | -------------------------------------------------------------------------------- /kern/jiffies/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=timer.c 3 | obj-m += timer.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/jiffies/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | jiffies , HZ 4 | 定时器 5 | 延迟调度 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /kern/kthread/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=kthread_demo.c 3 | obj-m += kthread_demo.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/kthread/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 内核线程 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /kern/kthread/kthread_demo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // task_struct 4 | #include // kthread api 5 | #include 6 | 7 | int thread_fn() { 8 | printk(KERN_INFO "In my thread fn\n"); 9 | return 0; 10 | } 11 | 12 | struct task_struct *thread1; 13 | 14 | static int thread_init(void){ 15 | thread1 = kthread_create(thread_fn,NULL,"my_thread"); 16 | /* 17 | set_task_state(thread1, __TASK_STOPPED); 18 | 19 | if ((thread1)) { 20 | printk(KERN_INFO "in if \n"); 21 | wake_up_process(thread1); 22 | } 23 | */ 24 | return 0; 25 | } 26 | 27 | static void thread_exit(void) 28 | { 29 | kthread_stop(thread1); 30 | printk(KERN_ALERT"Exiting Module\n"); 31 | } 32 | 33 | MODULE_LICENSE("GPL"); 34 | module_init(thread_init); 35 | module_exit(thread_exit); 36 | -------------------------------------------------------------------------------- /kern/list/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=hello.c 3 | obj-m += hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/list/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | Linux list practice. 4 | 5 | linux/type.h 6 | linux/list.h 7 | -------------------------------------------------------------------------------- /kern/memory-area/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=kern_print_segs.c 3 | obj-m += kern_print_segs.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/memory-area/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 分别从用户空间和内核空间的代码查看进程的虚拟内存区域(代码段 数据段 BSS 栈) 3 | -------------------------------------------------------------------------------- /kern/memory-area/print-segments.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int our_init_data = 30; 7 | int our_noinit_data; 8 | 9 | void our_prints(void) 10 | { 11 | int our_local_data = 1; 12 | printf("\nPid of the process is = %d", getpid()); 13 | printf("\nAddresses which fall into:"); 14 | printf("\n 1) Data segment = %p", 15 | &our_init_data); 16 | printf("\n 2) BSS segment = %p", 17 | &our_noinit_data); 18 | printf("\n 3) Code segment = %p", 19 | &our_prints); 20 | printf("\n 4) Stack segment = %p\n", 21 | &our_local_data); 22 | 23 | while(1); // 使得我们可以看到该进程的信息 24 | } 25 | 26 | int main() 27 | { 28 | our_prints(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /kern/mengning/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 学习 孟宁-Linux内核分析 ,网易云课堂 3 | -------------------------------------------------------------------------------- /kern/mengning/main.c: -------------------------------------------------------------------------------- 1 | int g(int x){ 2 | return x+3; 3 | } 4 | 5 | int f(int x){ 6 | return g(x); 7 | } 8 | 9 | int main(){ 10 | return f(8)+1; 11 | } 12 | -------------------------------------------------------------------------------- /kern/mengning/main2.S: -------------------------------------------------------------------------------- 1 | g: 2 | pushl %ebp 3 | movl %esp, %ebp 4 | movl 8(%ebp), %eax 5 | addl $3, %eax 6 | popl %ebp 7 | ret 8 | f: 9 | pushl %ebp 10 | movl %esp, %ebp 11 | subl $4, %esp 12 | movl 8(%ebp), %eax 13 | movl %eax, (%esp) 14 | call g 15 | leave 16 | ret 17 | main: 18 | pushl %ebp 19 | movl %esp, %ebp 20 | subl $4, %esp 21 | movl $8, (%esp) 22 | call f 23 | addl $1, %eax 24 | leave 25 | ret 26 | -------------------------------------------------------------------------------- /kern/mengning/test.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | -------------------------------------------------------------------------------- /kern/nelink/README.txt: -------------------------------------------------------------------------------- 1 | 2 | netlink socket programming demos. 3 | 1. echo - 利用netlink实现用户空间和内核模块的通信 4 | 2. genl-hello - 使用generic netlink编程:可以看到netlink的接口变化非常快,但是genl做了一定的封装,使得编程更加方便 5 | 3. route - 利用route netlink 获得路由表信息 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/.built-in.o.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/nelink/echo-unicast/built-in.o := rm -f /home/vonzhou/Coding/kern/nelink/echo-unicast/built-in.o; ar rcsD /home/vonzhou/Coding/kern/nelink/echo-unicast/built-in.o 2 | -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/.kern.ko.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/nelink/echo-unicast/kern.ko := ld -r -m elf_x86_64 -T /usr/src/linux-headers-3.11.0-15-generic/scripts/module-common.lds --build-id -o /home/vonzhou/Coding/kern/nelink/echo-unicast/kern.ko /home/vonzhou/Coding/kern/nelink/echo-unicast/kern.o /home/vonzhou/Coding/kern/nelink/echo-unicast/kern.mod.o 2 | -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/.tmp_versions/kern.mod: -------------------------------------------------------------------------------- 1 | /home/vonzhou/Coding/kern/nelink/echo-unicast/kern.ko 2 | /home/vonzhou/Coding/kern/nelink/echo-unicast/kern.o 3 | -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=kern.c 3 | obj-m += kern.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- 13 | endif 14 | -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/echo-unicast/Module.symvers -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/built-in.o: -------------------------------------------------------------------------------- 1 | ! 2 | -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/kern.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/echo-unicast/kern.ko -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/kern.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/echo-unicast/kern.mod.o -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/kern.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/echo-unicast/kern.o -------------------------------------------------------------------------------- /kern/nelink/echo-unicast/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/vonzhou/Coding/kern/nelink/echo-unicast/kern.ko 2 | -------------------------------------------------------------------------------- /kern/nelink/echo/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=nl_hello.c 3 | obj-m += nl_hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- 13 | endif 14 | -------------------------------------------------------------------------------- /kern/nelink/genl-hello/.built-in.o.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/nelink/genl-hello/built-in.o := rm -f /home/vonzhou/Coding/kern/nelink/genl-hello/built-in.o; ar rcsD /home/vonzhou/Coding/kern/nelink/genl-hello/built-in.o 2 | -------------------------------------------------------------------------------- /kern/nelink/genl-hello/.genl_kern.ko.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/nelink/genl-hello/genl_kern.ko := ld -r -m elf_x86_64 -T /usr/src/linux-headers-3.11.0-15-generic/scripts/module-common.lds --build-id -o /home/vonzhou/Coding/kern/nelink/genl-hello/genl_kern.ko /home/vonzhou/Coding/kern/nelink/genl-hello/genl_kern.o /home/vonzhou/Coding/kern/nelink/genl-hello/genl_kern.mod.o 2 | -------------------------------------------------------------------------------- /kern/nelink/genl-hello/.tmp_versions/genl_kern.mod: -------------------------------------------------------------------------------- 1 | /home/vonzhou/Coding/kern/nelink/genl-hello/genl_kern.ko 2 | /home/vonzhou/Coding/kern/nelink/genl-hello/genl_kern.o 3 | -------------------------------------------------------------------------------- /kern/nelink/genl-hello/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=genl_kern.c 3 | obj-m += genl_kern.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- .*.cmd *.out 13 | endif 14 | 15 | -------------------------------------------------------------------------------- /kern/nelink/genl-hello/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/genl-hello/Module.symvers -------------------------------------------------------------------------------- /kern/nelink/genl-hello/built-in.o: -------------------------------------------------------------------------------- 1 | ! 2 | -------------------------------------------------------------------------------- /kern/nelink/genl-hello/genl_kern.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/genl-hello/genl_kern.ko -------------------------------------------------------------------------------- /kern/nelink/genl-hello/genl_kern.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/genl-hello/genl_kern.mod.o -------------------------------------------------------------------------------- /kern/nelink/genl-hello/genl_kern.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/kern/nelink/genl-hello/genl_kern.o -------------------------------------------------------------------------------- /kern/nelink/genl-hello/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/vonzhou/Coding/kern/nelink/genl-hello/genl_kern.ko 2 | -------------------------------------------------------------------------------- /kern/nelink/kern2user/.built-in.o.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/nelink/kern2user/built-in.o := rm -f /home/vonzhou/Coding/kern/nelink/kern2user/built-in.o; ar rcsD /home/vonzhou/Coding/kern/nelink/kern2user/built-in.o 2 | -------------------------------------------------------------------------------- /kern/nelink/kern2user/.kern.ko.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/nelink/kern2user/kern.ko := ld -r -m elf_x86_64 -T /usr/src/linux-headers-3.11.0-15-generic/scripts/module-common.lds --build-id -o /home/vonzhou/Coding/kern/nelink/kern2user/kern.ko /home/vonzhou/Coding/kern/nelink/kern2user/kern.o /home/vonzhou/Coding/kern/nelink/kern2user/kern.mod.o 2 | -------------------------------------------------------------------------------- /kern/nelink/kern2user/.nl_hello.ko.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/nelink/kern2user/nl_hello.ko := ld -r -m elf_x86_64 -T /usr/src/linux-headers-3.11.0-15-generic/scripts/module-common.lds --build-id -o /home/vonzhou/Coding/kern/nelink/kern2user/nl_hello.ko /home/vonzhou/Coding/kern/nelink/kern2user/nl_hello.o /home/vonzhou/Coding/kern/nelink/kern2user/nl_hello.mod.o 2 | -------------------------------------------------------------------------------- /kern/nelink/kern2user/.tmp_versions/nl_hello.mod: -------------------------------------------------------------------------------- 1 | /home/vonzhou/Coding/kern/nelink/kern2user/nl_hello.ko 2 | /home/vonzhou/Coding/kern/nelink/kern2user/nl_hello.o 3 | -------------------------------------------------------------------------------- /kern/nelink/kern2user/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=nl_hello.c 3 | obj-m += nl_hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- 13 | endif 14 | -------------------------------------------------------------------------------- /kern/netdev-notifier/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=hello.c 3 | obj-m += hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- *.cmd 13 | endif 14 | -------------------------------------------------------------------------------- /kern/netdev-notifier/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netdevice notifier chain demo 5 | 6 | insmod .. 7 | ifconfig lo down/up 8 | and you will seee some event happened. 9 | 10 | 11 | -------------------------------------------------------------------------------- /kern/netdev-notifier/notifier.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /kern/netdev-test/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=hello.c 3 | obj-m += hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- *.cmd 13 | endif 14 | -------------------------------------------------------------------------------- /kern/netdev-test/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netdevice API practice. 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /kern/page/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=hello.c 3 | obj-m += hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/page/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static void page_test(){ 7 | unsigned long page; 8 | page = __get_free_pages(GFP_KERNEL, 2); // 4 pages 9 | // in x86 the virtual is 16bits 10 | printk(KERN_INFO "page virtual addr:%x\n", page); 11 | 12 | free_pages(page, 2); 13 | } 14 | 15 | static int __init lkp_init(void) 16 | { 17 | printk(" ===module init====\n"); 18 | page_test(); 19 | return 0; 20 | } 21 | 22 | static void __exit lkp_cleanup(void) 23 | { 24 | printk(" ===module exit====\n"); 25 | } 26 | 27 | module_init(lkp_init); 28 | module_exit(lkp_cleanup); 29 | 30 | 31 | -------------------------------------------------------------------------------- /kern/percpu/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=hello.c 3 | obj-m += hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/process/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=current.c 3 | obj-m += current.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/process/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 进程 3 | 4 | [X]得到当前进程 5 | [X]获得init进程的信息 6 | [X]打印出进程树结构 7 | [X]特别是 pid=2 的kthreadd 衍生出了很多内核线程 8 | 9 | -------------------------------------------------------------------------------- /kern/schedule/.built-in.o.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/schedule/built-in.o := rm -f /home/vonzhou/Coding/kern/schedule/built-in.o; ar rcsD /home/vonzhou/Coding/kern/schedule/built-in.o 2 | -------------------------------------------------------------------------------- /kern/schedule/.schedule_test.ko.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/kern/schedule/schedule_test.ko := ld -r -m elf_x86_64 -T /usr/src/linux-headers-3.11.0-15-generic/scripts/module-common.lds --build-id -o /home/vonzhou/Coding/kern/schedule/schedule_test.ko /home/vonzhou/Coding/kern/schedule/schedule_test.o /home/vonzhou/Coding/kern/schedule/schedule_test.mod.o 2 | -------------------------------------------------------------------------------- /kern/schedule/.tmp_versions/schedule_test.mod: -------------------------------------------------------------------------------- 1 | /home/vonzhou/Coding/kern/schedule/schedule_test.ko 2 | /home/vonzhou/Coding/kern/schedule/schedule_test.o 3 | -------------------------------------------------------------------------------- /kern/schedule/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=schedule_test.c 3 | obj-m += schedule_test.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order *- 13 | endif 14 | -------------------------------------------------------------------------------- /kern/schedule/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 使用工作队列 4 | -------------------------------------------------------------------------------- /kern/schedule/schedule_test.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define TEST_INTERVAL 10 * HZ 8 | 9 | static void test_work_handler(struct work_struct *work); 10 | 11 | static DECLARE_DELAYED_WORK(test_work, test_work_handler); 12 | 13 | static void test_work_handler(struct work_struct *work){ 14 | // do sth 15 | printk(KERN_INFO "test work function.\n"); 16 | // and then schedule it again 17 | schedule_delayed_work(&test_work, TEST_INTERVAL); 18 | } 19 | 20 | static int __init main_init(void){ 21 | schedule_delayed_work(&test_work, TEST_INTERVAL); 22 | 23 | return 0; 24 | } 25 | 26 | static void __exit main_exit(void){ 27 | cancel_delayed_work_sync(&test_work); 28 | } 29 | 30 | module_init(main_init); 31 | module_exit(main_exit); 32 | MODULE_LICENSE("GPL"); 33 | 34 | 35 | -------------------------------------------------------------------------------- /kern/slab/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=hello.c 3 | obj-m += hello.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/slab/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static void slab_test(){ 7 | unsigned long page; 8 | page = __get_free_pages(GFP_KERNEL, 2); // 4 pages 9 | // in x86 the virtual is 16bits 10 | printk(KERN_INFO "page virtual addr:%x\n", page); 11 | 12 | free_pages(page, 2); 13 | } 14 | 15 | static int __init lkp_init(void) 16 | { 17 | printk(" ===module init====\n"); 18 | page_test(); 19 | return 0; 20 | } 21 | 22 | static void __exit lkp_cleanup(void) 23 | { 24 | printk(" ===module exit====\n"); 25 | } 26 | 27 | module_init(lkp_init); 28 | module_exit(lkp_cleanup); 29 | 30 | 31 | -------------------------------------------------------------------------------- /kern/syscall/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | mymodule-objs:=interceptor.c 3 | obj-m += interceptor.o 4 | else 5 | PWD := $(shell pwd) 6 | KVER := $(shell uname -r) 7 | KDIR := /lib/modules/$(KVER)/build 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) 11 | clean: 12 | rm -rf *.o *.mod.c *.ko *.mod.o *.symvers *.order .*.cmd .tmp* 13 | endif 14 | -------------------------------------------------------------------------------- /kern/syscall/data.txt: -------------------------------------------------------------------------------- 1 | abcd1234 2 | -------------------------------------------------------------------------------- /kern/syscall/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(){ 6 | //#ifdef NTDEF 7 | int n; 8 | unsigned long i ; 9 | char buf[10]; 10 | FILE * file = fopen("data.txt", "rw"); 11 | n = fread(buf, 10, 1, file); 12 | //#endif 13 | //printf("123abc\n"); 14 | for(i=0;i < 1000000; i++){ 15 | //printf("=====\n"); 16 | } 17 | fclose(file); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /kern/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int i = 0; 4 | int j=(i++)+(++i)+(++i); 5 | int x = 0; 6 | int y=(x++)+(++x); 7 | // printf("%d\n",(i++)+(++i)+(++i)); 8 | printf("%d,%d\n",i,j); 9 | printf("%d,%d\n",x,y); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /ldd/completion/.complete.ko.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/ldd/completion/complete.ko := ld -r -m elf_i386 -T /usr/src/linux-headers-3.8.0-29-generic/scripts/module-common.lds --build-id -o /home/vonzhou/Coding/ldd/completion/complete.ko /home/vonzhou/Coding/ldd/completion/complete.o /home/vonzhou/Coding/ldd/completion/complete.mod.o 2 | -------------------------------------------------------------------------------- /ldd/completion/.tmp_versions/complete.mod: -------------------------------------------------------------------------------- 1 | /home/vonzhou/Coding/ldd/completion/complete.ko 2 | /home/vonzhou/Coding/ldd/completion/complete.o 3 | -------------------------------------------------------------------------------- /ldd/completion/Makefile: -------------------------------------------------------------------------------- 1 | obj-m := complete.o 2 | KDIR := /lib/modules/$(shell uname -r)/build 3 | PWD := $(shell pwd) 4 | 5 | default: 6 | $(MAKE) -C $(KDIR) M=$(PWD) modules 7 | clean: 8 | rm -f *.ko *.o *.mod.c 9 | -------------------------------------------------------------------------------- /ldd/completion/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/ldd/completion/Module.symvers -------------------------------------------------------------------------------- /ldd/completion/complete.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/ldd/completion/complete.ko -------------------------------------------------------------------------------- /ldd/completion/complete.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/ldd/completion/complete.mod.o -------------------------------------------------------------------------------- /ldd/completion/complete.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/ldd/completion/complete.o -------------------------------------------------------------------------------- /ldd/completion/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/vonzhou/Coding/ldd/completion/complete.ko 2 | -------------------------------------------------------------------------------- /leetcode/test.c: -------------------------------------------------------------------------------- 1 | //#include 2 | #include 3 | //using namespace std; 4 | 5 | void func(int n){ 6 | //int i; 7 | int arr[n];// = {0}; 8 | for(int i=0; i < n; i++) 9 | arr[i] = i; 10 | 11 | for( int i=0; i < n; i++) 12 | printf("%d ",arr[i]); 13 | } 14 | 15 | int main(){ 16 | int len = 7; 17 | //int arr[7][7][7]; 18 | //int arr2[7]; 19 | //arr[0][0][7] = 10; 20 | //arr2[7] =9; 21 | //cout << arr[0][0][7] << endl; 22 | int n; 23 | int arr[n]; 24 | //scanf("%d",&n); 25 | func(n); 26 | //cout << arr2[7] << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/test2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | int len = 7; 6 | int arr[7][7][7]; 7 | int arr2[7]; 8 | arr[0][0][7] = 10; 9 | arr2[7] =9; 10 | printf("%d\n",arr[0][0][7]); 11 | printf("%d\n",arr2[7]); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /linux-expert/namespaces/main-0-template.c: -------------------------------------------------------------------------------- 1 | 2 | #define _GNU_SOURCE 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define STACK_SIZE (1024 * 1024) 11 | 12 | static char child_stack[STACK_SIZE]; 13 | char* const child_args[] = { 14 | "/bin/bash", 15 | NULL 16 | }; 17 | int child_main(void *arg){ 18 | printf(" - World!\n"); 19 | execv(child_args[0], child_args); 20 | printf("Ooops\n"); 21 | return 1; 22 | } 23 | 24 | int main(){ 25 | printf(" - Hello ?\n"); 26 | int child_pid = clone(child_main, child_stack + STACK_SIZE, SIGCHLD, NULL); 27 | waitpid(child_pid, NULL, 0); 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /linux-expert/namespaces/main-1-uts.c: -------------------------------------------------------------------------------- 1 | 2 | #define _GNU_SOURCE 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define STACK_SIZE (1024 * 1024) 11 | 12 | static char child_stack[STACK_SIZE]; 13 | char* const child_args[] = { 14 | "/bin/bash", 15 | NULL 16 | }; 17 | int child_main(void *arg){ 18 | printf(" - World!\n"); 19 | sethostname("In Namespace", 12); 20 | execv(child_args[0], child_args); 21 | printf("Ooops\n"); 22 | return 1; 23 | } 24 | 25 | int main(){ 26 | printf(" - Hello ?\n"); 27 | int child_pid = clone(child_main, child_stack + STACK_SIZE, CLONE_NEWUTS | SIGCHLD, NULL); 28 | waitpid(child_pid, NULL, 0); 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /linux-expert/namespaces/ns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/linux-expert/namespaces/ns -------------------------------------------------------------------------------- /master-alg/test-list.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "list.h" 4 | 5 | void free_int(void *data){ 6 | free(data); 7 | } 8 | 9 | int main(){ 10 | int i; 11 | int arr[10]; 12 | List *list; 13 | for(i = 0; i< 10; i++) 14 | arr[i] = 3 * i + 1; 15 | 16 | list = (List *)malloc(sizeof(List)); 17 | if(list == NULL) 18 | return -1; 19 | 20 | for(i = 0; i< 10; i++) 21 | list_ins_next(list, NULL, (void *)(arr + i)); 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /master-alg/vonzhou.txt: -------------------------------------------------------------------------------- 1 | Matering Algorithms With C 2 | 3 | by Kely Loudon 4 | -------------------------------------------------------------------------------- /ovs-learn/StaticVariabe.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | void func(){ 6 | static int x = 0; 7 | if(!x){ 8 | x = 1; 9 | printf("I am got here!\n"); 10 | } 11 | } 12 | 13 | int main(){ 14 | func(); 15 | func(); 16 | } 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /pcap/capture: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/pcap/capture -------------------------------------------------------------------------------- /pcap/ip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/pcap/ip -------------------------------------------------------------------------------- /pcap/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/pcap/test -------------------------------------------------------------------------------- /pcap/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | main(){ 5 | char errBuf[PCAP_ERRBUF_SIZE], *device; 6 | printf("%d\n",PCAP_ERRBUF_SIZE); 7 | device = pcap_lookupdev(errBuf); 8 | if(device){ 9 | printf("success, device:%s \n",device); 10 | }else{ 11 | printf("error: %s\n",errBuf); 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /playwithds/README.txt: -------------------------------------------------------------------------------- 1 | 大话数据结构学习过程中写的代码。 2 | -------------------------------------------------------------------------------- /playwithds/sort/BubbleSort0.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | void BubbleSort0(SqList *L){ 4 | 5 | 6 | 7 | } 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /playwithds/sort/InsertSort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | void InsertSort(SqList *L){ 4 | int i, j; 5 | for(i = 2; i <= L->length; i++){ 6 | if(L->r[i] < L->r[i-1]){ 7 | // 需要在有序子表中找到自己合适的位置 8 | L->r[0] = L->r[i]; 9 | for(j = i-1; L->r[j] > L->r[0]; j--) 10 | L->r[j+1] = L->r[j]; 11 | L->r[j+1] = L->r[0]; 12 | } 13 | } 14 | } 15 | 16 | int main(){ 17 | SqList list; 18 | SqList *L = &list; 19 | int data[] = {0,34,4,5,32,6,11,10}; 20 | L->r = data; 21 | L->length = 7; 22 | InsertSort(L); 23 | show(L); 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /playwithds/sort/sort.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAXSIZE 50 5 | 6 | typedef struct { 7 | int *r; 8 | int length; 9 | }SqList; 10 | 11 | void swap(SqList *L, int i, int j){ 12 | int temp = L->r[i]; 13 | L->r[i] = L->r[j]; 14 | L->r[j] = temp; 15 | } 16 | 17 | void show(SqList *L){ 18 | int i; 19 | if(L->length <= 0) 20 | return; 21 | for(i = 1; i <= L->length; i++) 22 | printf("%d ", L->r[i]); 23 | printf("\n"); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /playwithds/tree/BiTree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef char TElemType; 5 | 6 | typedef struct BiTNode{ 7 | TElemType data; 8 | struct BiTNode * lchild, *rchild; 9 | }BiTNode, *BiTree; 10 | 11 | void CreateBiTree(BiTree *T){ 12 | TElemType ch; 13 | scanf("%c", &ch); 14 | if(ch == '#') 15 | *T = NULL; 16 | else{ 17 | *T = (BiTree)malloc(sizeof(BiTNode)); 18 | if((*T) == NULL) 19 | exit(-1); 20 | (*T)->data = ch; 21 | CreateBiTree(&(*T)->lchild); 22 | CreateBiTree(&(*T)->rchild); 23 | } 24 | 25 | } 26 | 27 | 28 | //先序遍历 29 | void PreOrderTraverse(BiTree T){ 30 | if(T == NULL) 31 | return; 32 | printf("%c\n", T->data); 33 | PreOrderTraverse(T->lchild); 34 | PreOrderTraverse(T->rchild); 35 | } 36 | 37 | 38 | int main(){ 39 | 40 | BiTree T = NULL; 41 | CreateBiTree(&T); 42 | PreOrderTraverse(T); 43 | 44 | 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /pong/play.lua: -------------------------------------------------------------------------------- 1 | 2 | 3 | -- create image 4 | 5 | Player = {} 6 | Player[1] = {x=2, y = 50, speed = 2, score=0, img= 7 | Image.createEmpty(16,64)} 8 | Player[2] = {x = 462, y = 50, speed = 2.4, score = 0, img = 9 | Image.createEmpty(16,64)} 10 | 11 | Ball = {x=240, y = math.random(60,180), xspeed = -3, yspeed = 3, 12 | image = Image.createEmpty(8,8)} 13 | 14 | --color the imgs 15 | Player[1].img:clear(Color.new(255,128,64)) 16 | Player[2].img:clear(Color.new(128, 0, 64)) 17 | Ball.img:clear(Color.new(218, 218, 218)) 18 | -------------------------------------------------------------------------------- /problems/tecent_2016_11.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // What can "int *p" be used for ?? 5 | int main(){ 6 | int *p1; 7 | int *p2; 8 | int *p3; 9 | int *p4; 10 | 11 | int a =1; 12 | p1 = &a; //case1: pointer to int 13 | 14 | int b[10]; 15 | p2 = b; // case2: pointer to a one dimension arr 16 | 17 | p3 = malloc( 10 * sizeof(int)); // case3: dynamic arr 18 | p3[3] = 99; 19 | free(p3); 20 | 21 | int c[3][4]; 22 | p4 = c; // warning: assignment from incompatible pointer type [enabled by default] 23 | int (*p5)[4]; 24 | p5 = c; // OK!! 25 | p5[1][0] = 100; 26 | printf("%d\n", c[1][0]); 27 | 28 | printf("%s\n", "Hello T!"); 29 | 30 | 31 | } -------------------------------------------------------------------------------- /py-code/byte/break.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | while True: 4 | s = raw_input('Enter a string: ') 5 | if ('quit'==s): 6 | print 'The loop is exited.' 7 | break; 8 | print 'The len of your input is: ',len(s) 9 | print 'Done' 10 | -------------------------------------------------------------------------------- /py-code/byte/cat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | 5 | def readfile(filename): 6 | '''Print file a the standard terminal.''' 7 | f = file(filename) 8 | while True: 9 | line = f.readline() 10 | if len(line)==0: 11 | break 12 | print line, 13 | f.close() 14 | 15 | #scripts start from here 16 | if len(sys.argv) < 2: 17 | print 'Do nothing' 18 | sys.exit() 19 | 20 | if sys.argv[1].startswith('--'): 21 | option = sys.argv[1][2:] 22 | if option == 'version': 23 | print 'Version 0.1' 24 | elif option == 'help': 25 | print '''\ 26 | This program prints file to the standard terminal, 27 | Any number of files can be specified, 28 | Options include:--version,--help ''' 29 | else: 30 | print 'Unkonwn option' 31 | sys.exit() 32 | else: 33 | for filename in sys.argv[1:]: 34 | readfile(filename) 35 | -------------------------------------------------------------------------------- /py-code/byte/continue.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | while True: 4 | s = raw_input('Enter a string: ') 5 | if ('quit'==s): 6 | print 'The loop is exited.' 7 | break; 8 | if len(s)<5: 9 | continue 10 | print 'The len of your input < 5 cannot got here ' 11 | print 'Done' 12 | -------------------------------------------------------------------------------- /py-code/byte/expression.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Filename: expression.py 3 | 4 | length = 5 5 | breadth = 2 6 | area = length * breadth 7 | print 'Area is', area 8 | print 'Perimeter is', 2*(length+breadth) 9 | -------------------------------------------------------------------------------- /py-code/byte/finally.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import time 4 | 5 | try: 6 | f = file('poem.txt','r') 7 | while True: 8 | line = f.readline() 9 | if len(line)==0: 10 | break; 11 | time.sleep(3) 12 | print line 13 | finally: 14 | f.close() 15 | print 'Exception occurred,file closed normally.' 16 | 17 | 18 | -------------------------------------------------------------------------------- /py-code/byte/for.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #filename: for.py 3 | 4 | for i in range(1,6): 5 | print i**2 6 | else: 7 | print 'done' 8 | 9 | -------------------------------------------------------------------------------- /py-code/byte/func1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def sayHello(): 4 | print('Hello world.') 5 | 6 | sayHello() 7 | -------------------------------------------------------------------------------- /py-code/byte/func_default.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def say(msg,times=1): 4 | print msg*times 5 | 6 | say('vonzhou') 7 | say('hello ',4) 8 | -------------------------------------------------------------------------------- /py-code/byte/func_doc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Filename: func_doc.py 3 | 4 | def printMax(x,y): 5 | '''Prints the maximum of two numbers. 6 | 7 | The two values must be integers.''' 8 | x=int(x) # convert to integers, if possible 9 | y=int(y) 10 | 11 | if x>y: 12 | print x,'is maximum' 13 | else: 14 | print y,'is maximum' 15 | 16 | printMax(3,5) 17 | print printMax.__doc__ 18 | help(printMax) 19 | 20 | -------------------------------------------------------------------------------- /py-code/byte/func_key.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def func(a,b=8,c=0): 4 | print 'a=',a,'b=',b,'c=',c 5 | func(1) 6 | func(23,c=9) 7 | func(10,b=0) 8 | 9 | -------------------------------------------------------------------------------- /py-code/byte/func_return.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def max(a,b): 4 | if a>b: 5 | return a 6 | else:return b 7 | 8 | def nothing(): 9 | pass 10 | print max(10,-4) 11 | print nothing() 12 | 13 | -------------------------------------------------------------------------------- /py-code/byte/helloworld.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #FIlename: helloworld.py 3 | print 'hello, world!' 4 | 5 | -------------------------------------------------------------------------------- /py-code/byte/if.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Filename: if.py 3 | 4 | number=23 5 | guess=int(raw_input('Enter an integer : ')) 6 | 7 | if guess==number: 8 | print 'Congratulations, you guessed it.' # New block starts here 9 | print "(but you do not win any prizes!)" # New block ends here 10 | elif guess number to reach here 16 | 17 | print 'Done' 18 | # This last statement is always executed, after the if statement is executed 19 | 20 | 21 | -------------------------------------------------------------------------------- /py-code/byte/lambda.py: -------------------------------------------------------------------------------- 1 | #/usr/bin/python 2 | 3 | def make_repeater(n): 4 | return lambda s:s*n 5 | 6 | twice = make_repeater(2) 7 | print twice('vonzhou') 8 | print twice(3) 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /py-code/byte/local_global.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def func(): 4 | x = 2; 5 | print 'change x to ',x 6 | #global y = 34 ,is wrong 7 | global y 8 | y=100 9 | print 'change y to ',y 10 | 11 | x=1 12 | y=4 13 | func() 14 | print 'After func x=',x,' and y=',y 15 | -------------------------------------------------------------------------------- /py-code/byte/mymodule.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def sayhi(): 4 | print 'Hi ,this is my module.' 5 | version='0.1' 6 | 7 | #End of mymodule.py 8 | -------------------------------------------------------------------------------- /py-code/byte/mymodule.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/py-code/byte/mymodule.pyc -------------------------------------------------------------------------------- /py-code/byte/mymodule_demo2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from mymodule import * 4 | #from mymodule import sayhi,version 5 | 6 | sayhi() 7 | print 'Version ',version 8 | 9 | -------------------------------------------------------------------------------- /py-code/byte/odbchelper.py: -------------------------------------------------------------------------------- 1 | def buildConnStr(params): 2 | """Build a connection string form a dictionary of params. 3 | Return string.""" 4 | return ";".join(["%s=%s" %(k,v) for k,v in params.items()]) 5 | 6 | if __name__=="__main__": 7 | myParams={"server":"de15",\ 8 | "database":"master",\ 9 | "uid":"sa",\ 10 | "pwd":"secret"\ 11 | } 12 | 13 | print buildConnStr(myParams) 14 | 15 | -------------------------------------------------------------------------------- /py-code/byte/odbchelper.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/py-code/byte/odbchelper.pyc -------------------------------------------------------------------------------- /py-code/byte/pickling.py: -------------------------------------------------------------------------------- 1 | #/usr/bin/python 2 | 3 | import cPickle as p 4 | #aslo can use pickle but cPickle is faster 1000x 5 | 6 | shoplist = ['apple', 'mango', 'pear'] 7 | shoplistfile = 'shoplist.dat' 8 | 9 | #write this object to the file 10 | f = file(shoplistfile,'w') 11 | p.dump(shoplist, f) 12 | f.close() 13 | 14 | del shoplist 15 | 16 | #readback from the file 17 | f = file(shoplistfile) 18 | storedlist = p.load(f) 19 | print storedlist 20 | -------------------------------------------------------------------------------- /py-code/byte/poem.txt: -------------------------------------------------------------------------------- 1 | Programming with python fun. 2 | work is fun. 3 | sfasdfsa 4 | gasdgfsdgdsfg 5 | 6 | sag 7 | sag 8 | asgsg 9 | s 10 | ggggggggggggggggggggggggggggggggggggggggggggggggg 11 | -------------------------------------------------------------------------------- /py-code/byte/printMax.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def printMax(a,b): 4 | if a>b: 5 | print a, 'is Maximum' 6 | elif a bool 3 | 4 | Return True if and only if s is a palindrome. 5 | 6 | >>> is_palindrome_v1('noon') 7 | True 8 | >>> is_palindrome_v1('racecar') 9 | True 10 | >>> is_palindrome_v1('dented') 11 | False 12 | """ 13 | 14 | return reverse(s) == s 15 | 16 | 17 | def reverse(s): 18 | """ (str) -> str 19 | 20 | Return a reversed version of s. 21 | 22 | >>> reverse('hello') 23 | 'olleh' 24 | >>> reverse('a') 25 | 'a' 26 | """ 27 | 28 | rev = '' 29 | 30 | # For each character in s, add that char to the beginning of rev. 31 | for ch in s: 32 | rev = ch + rev 33 | 34 | return rev 35 | 36 | -------------------------------------------------------------------------------- /redis-learn/demo_isnan.c: -------------------------------------------------------------------------------- 1 | /* isnan example 2 | The NaN values are used to identify undefined or non-representable values for floating-point elements, 3 | such as the square root of negative numbers or the result of 0/0. 4 | */ 5 | #include /* printf */ 6 | #include /* isnan, sqrt */ 7 | 8 | int main(){ 9 | printf ("isnan(0.0) : %d\n",isnan(0.0)); 10 | printf ("isnan(1.0/0.0) : %d\n",isnan(1.0/0.0)); 11 | printf ("isnan(-1.0/0.0) : %d\n",isnan(-1.0/0.0)); 12 | printf ("isnan(sqrt(-1.0)): %d\n",isnan(sqrt(-1.0))); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /scull/.scull.ko.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/scull/scull.ko := ld -r -m elf_i386 -T /usr/src/linux-headers-3.8.0-29-generic/scripts/module-common.lds --build-id -o /home/vonzhou/Coding/scull/scull.ko /home/vonzhou/Coding/scull/scull.o /home/vonzhou/Coding/scull/scull.mod.o 2 | -------------------------------------------------------------------------------- /scull/.scull.o.cmd: -------------------------------------------------------------------------------- 1 | cmd_/home/vonzhou/Coding/scull/scull.o := ld -m elf_i386 -r -o /home/vonzhou/Coding/scull/scull.o /home/vonzhou/Coding/scull/main.o /home/vonzhou/Coding/scull/pipe.o /home/vonzhou/Coding/scull/access.o 2 | -------------------------------------------------------------------------------- /scull/.tmp_versions/scull.mod: -------------------------------------------------------------------------------- 1 | /home/vonzhou/Coding/scull/scull.ko 2 | /home/vonzhou/Coding/scull/main.o /home/vonzhou/Coding/scull/pipe.o /home/vonzhou/Coding/scull/access.o 3 | -------------------------------------------------------------------------------- /scull/Makefile: -------------------------------------------------------------------------------- 1 | # Comment/uncomment the following line to disable/enable debugging 2 | #DEBUG = y 3 | 4 | 5 | # Add your debugging flag (or not) to CFLAGS 6 | ifeq ($(DEBUG),y) 7 | DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines 8 | else 9 | DEBFLAGS = -O2 10 | endif 11 | 12 | CFLAGS += $(DEBFLAGS) 13 | CFLAGS += -I$(LDDINC) 14 | 15 | ifneq ($(KERNELRELEASE),) 16 | # call from kernel build system 17 | 18 | scull-objs := main.o pipe.o access.o 19 | 20 | obj-m := scull.o 21 | 22 | else 23 | 24 | KERNELDIR ?= /lib/modules/$(shell uname -r)/build 25 | PWD := $(shell pwd) 26 | 27 | modules: 28 | $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD)/../include modules 29 | 30 | endif 31 | 32 | 33 | 34 | clean: 35 | rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions 36 | 37 | depend .depend dep: 38 | $(CC) $(CFLAGS) -M *.c > .depend 39 | 40 | 41 | ifeq (.depend,$(wildcard .depend)) 42 | include .depend 43 | endif 44 | -------------------------------------------------------------------------------- /scull/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/scull/Module.symvers -------------------------------------------------------------------------------- /scull/access.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/scull/access.o -------------------------------------------------------------------------------- /scull/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/scull/main.o -------------------------------------------------------------------------------- /scull/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/vonzhou/Coding/scull/scull.ko 2 | -------------------------------------------------------------------------------- /scull/pipe.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/scull/pipe.o -------------------------------------------------------------------------------- /scull/scull.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/scull/scull.ko -------------------------------------------------------------------------------- /scull/scull.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/scull/scull.mod.o -------------------------------------------------------------------------------- /scull/scull.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/scull/scull.o -------------------------------------------------------------------------------- /scull/scull_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="scull" 3 | device="scull" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # Remove stale nodes 9 | 10 | rm -f /dev/${device} /dev/${device}[0-3] 11 | rm -f /dev/${device}priv 12 | rm -f /dev/${device}pipe /dev/${device}pipe[0-3] 13 | rm -f /dev/${device}single 14 | rm -f /dev/${device}uid 15 | rm -f /dev/${device}wuid 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /scull/test_scull.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | char path[20] = "/dev/scull0"; 9 | char buf[128]; 10 | int main(){ 11 | int f = open(path,O_RDWR); 12 | if(f == -1){ 13 | printf("scull open failed ,errno = %d\n",errno); 14 | exit(-1); 15 | } 16 | 17 | printf("Input a str to write to the scull device:\n"); 18 | scanf("%s",buf); 19 | write(f,buf,sizeof(buf)); 20 | 21 | printf("Read the str from the device\n"); 22 | read(f, buf, sizeof(buf)); 23 | printf("result:%s\n",buf); 24 | 25 | close(f); 26 | } 27 | -------------------------------------------------------------------------------- /test1.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | void func(int a){ 4 | } 5 | 6 | void func(char a){ 7 | 8 | } 9 | 10 | 11 | int main(){ 12 | func(2); 13 | func('a'); 14 | } 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /test_memcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | char tmp[20]; 6 | char *str = "vonzhou\n"; 7 | memcpy((void *)tmp,str, strlen(str) + 1);// remember to include the tailing '\0' 8 | printf("%s",tmp); 9 | 10 | return 0; 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /thinking/func1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *func(int n){ 4 | 5 | int x = n *n; 6 | int a[] = {1,3,4,5,7}; 7 | a[0] = x; 8 | return a; 9 | } 10 | 11 | void func2(){ 12 | int a = 1; 13 | int b = 78; 14 | printf("%d vonzhou jjjj.\n",a); 15 | } 16 | 17 | int main(){ 18 | int n = 3; 19 | int *a = func(3); 20 | //func2(2); 21 | printf("%d\n", a[0]); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /thinking/func2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int *func(int n){ 5 | 6 | int x = n *n; 7 | int *a = (int *)malloc(5 * sizeof(int)); 8 | a[0] = x; 9 | return a; 10 | } 11 | 12 | void func2(){ 13 | int a = 1; 14 | int b = 78; 15 | printf("%d vonzhou jjjj.\n",a); 16 | } 17 | 18 | int main(){ 19 | int n = 3; 20 | int *a = func(3); 21 | func(2); 22 | printf("%d\n", a[0]); 23 | 24 | free(a); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /thinking/func3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *func(int n){ 4 | 5 | int x = n *n; 6 | static int a[] = {1,3,4,5,7}; 7 | a[0] = x; 8 | return a; 9 | } 10 | 11 | void func2(){ 12 | int a = 1; 13 | int b = 78; 14 | printf("%d vonzhou jjjj.\n",a); 15 | } 16 | 17 | int main(){ 18 | int n = 3; 19 | int *a = func(3); 20 | func2(2); 21 | printf("%d\n", a[0]); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/Makefile: -------------------------------------------------------------------------------- 1 | all: httpd 2 | 3 | httpd: httpd.c 4 | gcc -W -Wall -o httpd httpd.c -lpthread 5 | 6 | clean: 7 | rm httpd *.o 8 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/README: -------------------------------------------------------------------------------- 1 | These are sample CGI scripts and webpages for tinyhttpd. They can 2 | be redistributed under the terms of the GPL. 3 | 4 | The most impressive demonstration I gave of tinyhttpd to my 5 | professor and my classmates was to load color.cgi with a value of 6 | "chartreuse." :) It's actually a very simple script, guys. 7 | 8 | jdb 9 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/a.html: -------------------------------------------------------------------------------- 1 | 2 | Index 3 | 4 | 5 |

Hello WOrld

6 | 7 | Vonzhou TO DO the best 8 | 9 | 10 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/check.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -Tw 2 | 3 | use strict; 4 | use CGI; 5 | 6 | my($cgi) = new CGI; 7 | 8 | print $cgi->header('text/html'); 9 | print $cgi->start_html(-title => "Example CGI script", 10 | -BGCOLOR => 'red'); 11 | print $cgi->h1("CGI Example"); 12 | print $cgi->p, "This is an example of CGI\n"; 13 | print $cgi->p, "Parameters given to this script:\n"; 14 | print "
    \n"; 15 | foreach my $param ($cgi->param) 16 | { 17 | print "
  • ", "$param ", $cgi->param($param), "\n"; 18 | } 19 | print "
"; 20 | print $cgi->end_html, "\n"; 21 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/color.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -Tw 2 | 3 | use strict; 4 | use CGI; 5 | 6 | my($cgi) = new CGI; 7 | 8 | print $cgi->header; 9 | my($color) = "blue"; 10 | $color = $cgi->param('color') if defined $cgi->param('color'); 11 | 12 | print $cgi->start_html(-title => uc($color), 13 | -BGCOLOR => $color); 14 | print $cgi->h1("This is $color"); 15 | print $cgi->end_html; 16 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/index.html: -------------------------------------------------------------------------------- 1 | 2 | Index 3 | 4 |

Welcome to J. David's webserver. 5 |

CGI demo 6 |
7 | Enter a color: 8 | 9 |
10 | 11 | 12 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/test/a.html: -------------------------------------------------------------------------------- 1 | 2 | Index 3 | 4 | 5 |

Hello WOrld

6 | 7 | Vonzhou TO DO the best 8 | 9 | 10 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/test/index.html: -------------------------------------------------------------------------------- 1 | 2 | Index 3 | 4 | 5 |

Hello WOrld

6 | 7 | Vonzhou TO DO the best 8 | 9 | 10 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/htdocs/test/index.html~: -------------------------------------------------------------------------------- 1 | 2 | Index 3 | 4 |

Welcome to J. David's webserver. 5 |

CGI demo 6 |
7 | Enter a color: 8 | 9 |
10 | 11 | 12 | -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/httpd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/tinyhttpd-0.1.0/httpd -------------------------------------------------------------------------------- /tinyhttpd-0.1.0/simpleclient.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | int sockfd; 11 | int len; 12 | struct sockaddr_in address; 13 | int result; 14 | char ch = 'A'; 15 | 16 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 17 | address.sin_family = AF_INET; 18 | address.sin_addr.s_addr = inet_addr("127.0.0.1"); 19 | address.sin_port = htons(9734); 20 | len = sizeof(address); 21 | result = connect(sockfd, (struct sockaddr *)&address, len); 22 | 23 | if (result == -1) 24 | { 25 | perror("oops: client1"); 26 | exit(1); 27 | } 28 | write(sockfd, &ch, 1); 29 | read(sockfd, &ch, 1); 30 | printf("char from server = %c\n", ch); 31 | close(sockfd); 32 | exit(0); 33 | } 34 | -------------------------------------------------------------------------------- /tlpi/.gitignore: -------------------------------------------------------------------------------- 1 | .o 2 | tlpi-book/ 3 | -------------------------------------------------------------------------------- /tlpi/55filelock/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all:tflock 3 | 4 | tflock:t_flock.o error_functions.o get_num.o curr_time.o 5 | gcc t_flock.o error_functions.o get_num.o curr_time.o -o tflock 6 | 7 | t_flock.o:t_flock.c tlpi_hdr.h 8 | 9 | error_functions.o:error_functions.c error_functions.h tlpi_hdr.h 10 | 11 | get_num.o:get_num.c get_num.h tlpi_hdr.h 12 | 13 | curr_time.o:curr_time.c curr_time.h tlpi_hdr.h 14 | 15 | 16 | clean: 17 | rm -f *.o tflock 18 | -------------------------------------------------------------------------------- /tlpi/55filelock/curr_time.h: -------------------------------------------------------------------------------- 1 | 2 | /* Header file for Listing 10-2 */ 3 | 4 | #ifndef CURR_TIME_H 5 | #define CURR_TIME_H /* Prevent accidental double inclusion */ 6 | 7 | char *currTime(const char *fmt); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /tlpi/55filelock/get_num.h: -------------------------------------------------------------------------------- 1 | 2 | /* Listing 3-5 */ 3 | 4 | #ifndef GET_NUM_H 5 | #define GET_NUM_H 6 | 7 | #define GN_NONNEG 01 /* Value must be >= 0 */ 8 | #define GN_GT_0 02 /* Value must be > 0 */ 9 | 10 | /* By default, integers are decimal */ 11 | #define GN_ANY_BASE 0100 /* Can use any base - like strtol(3) */ 12 | #define GN_BASE_8 0200 /* Value is expressed in octal */ 13 | #define GN_BASE_16 0400 /* Value is expressed in hexadecimal */ 14 | 15 | long getLong(const char *arg, int flags, const char *name); 16 | 17 | int getInt(const char *arg, int flags, const char *name); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /tlpi/55filelock/tfile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/tlpi/55filelock/tfile -------------------------------------------------------------------------------- /tlpi/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 学习 The Linux Programming Interface 这本书 3 | -------------------------------------------------------------------------------- /tset.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/tset.c -------------------------------------------------------------------------------- /unp/.inet_ntop_ipv4.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/.inet_ntop_ipv4.c.swp -------------------------------------------------------------------------------- /unp/Makefile: -------------------------------------------------------------------------------- 1 | prifinfo:prifinfo.o sock_ntop_host.o get_ifi_info.o inet_ntop_ipv4.o 2 | cc -o prifinfo prifinfo.o sock_ntop_host.o get_ifi_info.o 3 | prifinfo.o:prifinfo.c unpifi.h 4 | cc -c prifinfo.c 5 | sock_ntop_host.o:sock_ntop_host.c unpifi.h 6 | get_ifi_info.o:get_ifi_info.c unpifi.h 7 | 8 | 9 | clean: 10 | rm prifinfo *.o 11 | 12 | -------------------------------------------------------------------------------- /unp/README.md: -------------------------------------------------------------------------------- 1 | # UNPv1 - The sockets networking API 2 | --- 3 | 4 | 5 | 6 | ### [1. 简介](intro/) 7 | -------------------------------------------------------------------------------- /unp/get_ifi_info.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/get_ifi_info.o -------------------------------------------------------------------------------- /unp/inet_ntop_ipv4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #ifndef INET_ADDRSTRLEN 7 | #define INET_ADDRSTRLEN 16 8 | #endif 9 | 10 | /* include inet_ntop */ 11 | const char * 12 | inet_ntop(int family, const void *addrptr, char *strptr, size_t len) 13 | { 14 | const u_char *p = (const u_char *) addrptr; 15 | 16 | if (family == AF_INET) { 17 | char temp[INET_ADDRSTRLEN]; 18 | 19 | snprintf(temp, sizeof(temp), "%d.%d.%d.%d", 20 | p[0], p[1], p[2], p[3]); 21 | if (strlen(temp) >= len) { 22 | errno = ENOSPC; 23 | return (NULL); 24 | } 25 | strcpy(strptr, temp); 26 | return (strptr); 27 | } 28 | errno = EAFNOSUPPORT; 29 | return (NULL); 30 | } 31 | /* end inet_ntop */ 32 | 33 | -------------------------------------------------------------------------------- /unp/inet_ntop_ipv4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/inet_ntop_ipv4.o -------------------------------------------------------------------------------- /unp/intro/README.md: -------------------------------------------------------------------------------- 1 | # 1. 简介 2 | --- 3 | 4 | ### 时间获取客户端服务器端程序入门, [daytimetcpcli.c](daytimetcpcli.c), [daytimetcpsrv.c](daytimetcpsrv.c) 5 | 6 | 7 | 运行效果: 8 | 9 | ![](daytimetcp-intro.png) 10 | 11 | 一次请求的抓包结果: 12 | 13 | ![](daytimetcp-intro-tcpdump.png) 14 | 15 | 16 | # 3. 套接字编程简介 17 | --- 18 | 19 | ### 确定主机字节序 [byteorder.c](byteorder.c) 20 | 21 | 22 | # 4. 基本TCP套接字编程 23 | --- 24 | 25 | ### 使用daytimetcpcli这个程序来看connect不同的出错情况 26 | 27 | ![](connect-error.png) 28 | 29 | 当某主机没有运行对应的服务时,立即回应一个RST包,抓包如下: 30 | 31 | ![](connect-RST.png) 32 | 33 | ### 显示客户端IP地址和端口号的时间获取服务器 [daytimetcpsrv1.c](daytimetcpsrv1.c) 34 | 35 | ![](daytimetcpsrv1.png) 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /unp/intro/byteorder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(int argc, char **argv) 6 | { 7 | union { 8 | short s; 9 | char c[sizeof(short)]; 10 | } un;//?? 11 | 12 | un.s = 0x0102; 13 | if (sizeof(short) == 2) { 14 | if (un.c[0] == 1 && un.c[1] == 2) 15 | printf("big-endian\n"); 16 | else if (un.c[0] == 2 && un.c[1] == 1) 17 | printf("little-endian\n"); 18 | else 19 | printf("unknown\n"); 20 | } else 21 | printf("sizeof(short) = %ld\n", sizeof(short)); 22 | 23 | exit(0); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /unp/intro/cli: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/cli -------------------------------------------------------------------------------- /unp/intro/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/client -------------------------------------------------------------------------------- /unp/intro/daytimecli.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/daytimecli.o -------------------------------------------------------------------------------- /unp/intro/daytimeser.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/daytimeser.o -------------------------------------------------------------------------------- /unp/intro/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/error.o -------------------------------------------------------------------------------- /unp/intro/ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/ser -------------------------------------------------------------------------------- /unp/intro/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/server -------------------------------------------------------------------------------- /unp/intro/sock_ntop.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/intro/sock_ntop.o -------------------------------------------------------------------------------- /unp/intro/test_sock_ntop.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int main(){ 4 | struct sockaddr_in addr; 5 | addr.sin_family = AF_INET; 6 | addr.sin_addr.s_addr = inet_addr("192.168.4.135"); 7 | addr.sin_port = htons(13); 8 | 9 | printf("%s\n",Sock_ntop((const struct sockaddr *)&addr,sizeof(addr))); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /unp/intro/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *sock_ntop(const struct sockaddr *,socklen_t); 8 | char *Sock_ntop(const struct sockaddr *,socklen_t); 9 | -------------------------------------------------------------------------------- /unp/libevent_learn/timer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/libevent_learn/timer -------------------------------------------------------------------------------- /unp/libevent_learn/timer_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | struct event ev; 7 | struct timeval tv; 8 | 9 | void time_cb(int fd,short event,void *arg){ 10 | printf("timer wakeup\n"); 11 | event_add(&ev,&tv);//重新调度,类似于信号处理/中断函数 12 | } 13 | 14 | int main(){ 15 | struct event_base *base = event_base_new(); 16 | tv.tv_sec = 5; 17 | tv.tv_usec = 0; 18 | evtimer_set(&ev,time_cb,NULL); 19 | event_base_set(base,&ev); 20 | event_add(&ev,&tv); 21 | event_base_dispatch(base); 22 | } 23 | -------------------------------------------------------------------------------- /unp/prifinfo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/prifinfo.o -------------------------------------------------------------------------------- /unp/sock_ntop_host.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/sock_ntop_host.o -------------------------------------------------------------------------------- /unp/socketpair2.c: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /unp/tcpepool/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpepool/server -------------------------------------------------------------------------------- /unp/tcpsercli/Makefile: -------------------------------------------------------------------------------- 1 | default:tcpserver tcpclient 2 | 3 | tcpserver:tcpserver01.o wrapsock.o str_echo.o writen.o util.h error.o wrapunix.o 4 | cc -o tcpserver tcpserver01.o wrapsock.o str_echo.o writen.o error.o wrapunix.o 5 | tcpserver01.o:tcpserver01.c util.h 6 | cc -c tcpserver01.c 7 | wrapsock.o:wrapsock.c util.h 8 | cc -c wrapsock.c 9 | 10 | 11 | tcpclient:tcpcli01.o wrapsock.o writen.o str_cli.o util.h error.o wrapunix.o wrapstdio.o readline.o 12 | cc -o tcpclient tcpcli01.o wrapsock.o writen.o str_cli.o error.o wrapunix.o wrapstdio.o readline.o 13 | 14 | clean: 15 | rm -f *.o tcpserver tcpclient 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /unp/tcpsercli/echo_server_event: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/echo_server_event -------------------------------------------------------------------------------- /unp/tcpsercli/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/error.o -------------------------------------------------------------------------------- /unp/tcpsercli/readline.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/readline.o -------------------------------------------------------------------------------- /unp/tcpsercli/serverepoll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/serverepoll -------------------------------------------------------------------------------- /unp/tcpsercli/str_cli.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | void 4 | str_cli(FILE *fp, int sockfd) 5 | { 6 | char sendline[MAXLINE], recvline[MAXLINE]; 7 | 8 | while (Fgets(sendline, MAXLINE, fp) != NULL) { 9 | 10 | Writen(sockfd, sendline, strlen(sendline)); 11 | 12 | if (Readline(sockfd, recvline, MAXLINE) == 0) 13 | err_quit("str_cli: server terminated prematurely"); 14 | 15 | Fputs(recvline, stdout); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /unp/tcpsercli/str_cli.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/str_cli.o -------------------------------------------------------------------------------- /unp/tcpsercli/str_echo.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | void 4 | str_echo(int sockfd) 5 | { 6 | ssize_t n; 7 | char buf[MAXLINE]; 8 | 9 | again: 10 | while ( (n = read(sockfd, buf, MAXLINE)) > 0) 11 | Writen(sockfd, buf, n); 12 | 13 | if (n < 0 && errno == EINTR) 14 | goto again; 15 | else if (n < 0) 16 | err_sys("str_echo: read error"); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /unp/tcpsercli/str_echo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/str_echo.o -------------------------------------------------------------------------------- /unp/tcpsercli/tcpcli01.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int 4 | main(int argc, char **argv) 5 | { 6 | int sockfd; 7 | struct sockaddr_in servaddr; 8 | 9 | if (argc != 2) 10 | err_quit("usage: tcpcli "); 11 | 12 | sockfd = Socket(AF_INET, SOCK_STREAM, 0); 13 | 14 | bzero(&servaddr, sizeof(servaddr)); 15 | servaddr.sin_family = AF_INET; 16 | servaddr.sin_port = htons(1234); 17 | Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); 18 | 19 | Connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); 20 | 21 | str_cli(stdin, sockfd); /* do it all */ 22 | 23 | exit(0); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /unp/tcpsercli/tcpcli01.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/tcpcli01.o -------------------------------------------------------------------------------- /unp/tcpsercli/tcpclient: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/tcpclient -------------------------------------------------------------------------------- /unp/tcpsercli/tcpserver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/tcpserver -------------------------------------------------------------------------------- /unp/tcpsercli/tcpserver01.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int 4 | main(int argc, char **argv) 5 | { 6 | int listenfd, connfd; 7 | pid_t childpid; 8 | socklen_t clilen; 9 | struct sockaddr_in cliaddr, servaddr; 10 | 11 | listenfd = Socket(AF_INET, SOCK_STREAM, 0); 12 | 13 | bzero(&servaddr, sizeof(servaddr)); 14 | servaddr.sin_family = AF_INET; 15 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 16 | servaddr.sin_port = htons(1234); 17 | 18 | Bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); 19 | 20 | Listen(listenfd,1024 ); 21 | 22 | for ( ; ; ) { 23 | clilen = sizeof(cliaddr); 24 | connfd = Accept(listenfd, (struct sockaddr *) &cliaddr, &clilen); 25 | 26 | if ( (childpid = Fork()) == 0) { /* child process */ 27 | Close(listenfd); /* close listening socket */ 28 | str_echo(connfd); /* process the request */ 29 | exit(0); 30 | } 31 | Close(connfd); /* parent closes connected socket */ 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /unp/tcpsercli/tcpserver01.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/tcpserver01.o -------------------------------------------------------------------------------- /unp/tcpsercli/tcpserverepoll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/tcpserverepoll -------------------------------------------------------------------------------- /unp/tcpsercli/wrapsock.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/wrapsock.o -------------------------------------------------------------------------------- /unp/tcpsercli/wrapstdio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Standard I/O wrapper functions. 3 | */ 4 | 5 | #include "util.h" 6 | 7 | void 8 | Fclose(FILE *fp) 9 | { 10 | if (fclose(fp) != 0) 11 | err_sys("fclose error"); 12 | } 13 | 14 | FILE * 15 | Fdopen(int fd, const char *type) 16 | { 17 | FILE *fp; 18 | 19 | if ( (fp = fdopen(fd, type)) == NULL) 20 | err_sys("fdopen error"); 21 | 22 | return(fp); 23 | } 24 | 25 | char * 26 | Fgets(char *ptr, int n, FILE *stream) 27 | { 28 | char *rptr; 29 | 30 | if ( (rptr = fgets(ptr, n, stream)) == NULL && ferror(stream)) 31 | err_sys("fgets error"); 32 | 33 | return (rptr); 34 | } 35 | 36 | FILE * 37 | Fopen(const char *filename, const char *mode) 38 | { 39 | FILE *fp; 40 | 41 | if ( (fp = fopen(filename, mode)) == NULL) 42 | err_sys("fopen error"); 43 | 44 | return(fp); 45 | } 46 | 47 | void 48 | Fputs(const char *ptr, FILE *stream) 49 | { 50 | if (fputs(ptr, stream) == EOF) 51 | err_sys("fputs error"); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /unp/tcpsercli/wrapstdio.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/wrapstdio.o -------------------------------------------------------------------------------- /unp/tcpsercli/wrapunix.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/wrapunix.o -------------------------------------------------------------------------------- /unp/tcpsercli/writen.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | ssize_t /* Write "n" bytes to a descriptor. */ 4 | writen(int fd, const void *vptr, size_t n) 5 | { 6 | size_t nleft; 7 | ssize_t nwritten; 8 | const char *ptr; 9 | 10 | ptr = vptr; 11 | nleft = n; 12 | while (nleft > 0) { 13 | if ( (nwritten = write(fd, ptr, nleft)) <= 0) { 14 | if (nwritten < 0 && errno == EINTR) 15 | nwritten = 0; /* and call write() again */ 16 | else 17 | return(-1); /* error */ 18 | } 19 | 20 | nleft -= nwritten; 21 | ptr += nwritten; 22 | } 23 | return(n); 24 | } 25 | /* end writen */ 26 | 27 | void 28 | Writen(int fd, void *ptr, size_t nbytes) 29 | { 30 | if (writen(fd, ptr, nbytes) != nbytes) 31 | err_sys("writen error"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /unp/tcpsercli/writen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli/writen.o -------------------------------------------------------------------------------- /unp/tcpsercli02/Makefile: -------------------------------------------------------------------------------- 1 | default:tcpserver tcpclient 2 | 3 | tcpserver:tcpserver02.o wrapsock.o str_echo.o writen.o util.h error.o wrapunix.o signal.o 4 | cc -o tcpserver tcpserver02.o wrapsock.o str_echo.o writen.o error.o wrapunix.o signal.o 5 | tcpserver01.o:tcpserver02.c util.h 6 | cc -c tcpserver02.c 7 | wrapsock.o:wrapsock.c util.h 8 | cc -c wrapsock.c 9 | 10 | 11 | tcpclient:tcpcli01.o wrapsock.o writen.o str_cli.o util.h error.o wrapunix.o wrapstdio.o readline.o 12 | cc -o tcpclient tcpcli01.o wrapsock.o writen.o str_cli.o error.o wrapunix.o wrapstdio.o readline.o 13 | 14 | clean: 15 | rm -f *.o tcpserver tcpclient 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /unp/tcpsercli02/echo_server_event: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli02/echo_server_event -------------------------------------------------------------------------------- /unp/tcpsercli02/serverepoll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/tcpsercli02/serverepoll -------------------------------------------------------------------------------- /unp/tcpsercli02/signal.c: -------------------------------------------------------------------------------- 1 | 2 | #include "util.h" 3 | 4 | Sigfunc *signal(int signo, Sigfunc *func){ 5 | struct sigaction act, oact; 6 | act.sa_handler = func; 7 | sigemptyset(&act.sa_mask); 8 | act.sa_flags = 0; 9 | 10 | if(signo == SIGALRM){ 11 | #ifdef SA_INTERRUPT 12 | act.sa_flags |= SA_INTERRUPT; 13 | #endif 14 | }else{ 15 | #ifdef SA_RESTART 16 | act.sa_flags |= SA_RESTART; 17 | #endif 18 | } 19 | 20 | if(sigaction(signo, &act, &oact) < 0) 21 | return SIG_ERR; 22 | return oact.sa_handler; 23 | } 24 | -------------------------------------------------------------------------------- /unp/tcpsercli02/str_cli.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | void 4 | str_cli(FILE *fp, int sockfd) 5 | { 6 | char sendline[MAXLINE], recvline[MAXLINE]; 7 | 8 | while (Fgets(sendline, MAXLINE, fp) != NULL) { 9 | 10 | Writen(sockfd, sendline, strlen(sendline)); 11 | 12 | if (Readline(sockfd, recvline, MAXLINE) == 0) 13 | err_quit("str_cli: server terminated prematurely"); 14 | 15 | Fputs(recvline, stdout); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /unp/tcpsercli02/str_echo.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | void 4 | str_echo(int sockfd) 5 | { 6 | ssize_t n; 7 | char buf[MAXLINE]; 8 | 9 | again: 10 | while ( (n = read(sockfd, buf, MAXLINE)) > 0) 11 | Writen(sockfd, buf, n); 12 | 13 | if (n < 0 && errno == EINTR) 14 | goto again; 15 | else if (n < 0) 16 | err_sys("str_echo: read error"); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /unp/tcpsercli02/tcpcli01.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int 4 | main(int argc, char **argv) 5 | { 6 | int sockfd; 7 | struct sockaddr_in servaddr; 8 | 9 | if (argc != 2) 10 | err_quit("usage: tcpcli "); 11 | 12 | sockfd = Socket(AF_INET, SOCK_STREAM, 0); 13 | 14 | bzero(&servaddr, sizeof(servaddr)); 15 | servaddr.sin_family = AF_INET; 16 | servaddr.sin_port = htons(1234); 17 | Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); 18 | 19 | Connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); 20 | 21 | str_cli(stdin, sockfd); /* do it all */ 22 | 23 | exit(0); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /unp/tcpsercli02/wrapstdio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Standard I/O wrapper functions. 3 | */ 4 | 5 | #include "util.h" 6 | 7 | void 8 | Fclose(FILE *fp) 9 | { 10 | if (fclose(fp) != 0) 11 | err_sys("fclose error"); 12 | } 13 | 14 | FILE * 15 | Fdopen(int fd, const char *type) 16 | { 17 | FILE *fp; 18 | 19 | if ( (fp = fdopen(fd, type)) == NULL) 20 | err_sys("fdopen error"); 21 | 22 | return(fp); 23 | } 24 | 25 | char * 26 | Fgets(char *ptr, int n, FILE *stream) 27 | { 28 | char *rptr; 29 | 30 | if ( (rptr = fgets(ptr, n, stream)) == NULL && ferror(stream)) 31 | err_sys("fgets error"); 32 | 33 | return (rptr); 34 | } 35 | 36 | FILE * 37 | Fopen(const char *filename, const char *mode) 38 | { 39 | FILE *fp; 40 | 41 | if ( (fp = fopen(filename, mode)) == NULL) 42 | err_sys("fopen error"); 43 | 44 | return(fp); 45 | } 46 | 47 | void 48 | Fputs(const char *ptr, FILE *stream) 49 | { 50 | if (fputs(ptr, stream) == EOF) 51 | err_sys("fputs error"); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /unp/tcpsercli02/writen.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | ssize_t /* Write "n" bytes to a descriptor. */ 4 | writen(int fd, const void *vptr, size_t n) 5 | { 6 | size_t nleft; 7 | ssize_t nwritten; 8 | const char *ptr; 9 | 10 | ptr = vptr; 11 | nleft = n; 12 | while (nleft > 0) { 13 | if ( (nwritten = write(fd, ptr, nleft)) <= 0) { 14 | if (nwritten < 0 && errno == EINTR) 15 | nwritten = 0; /* and call write() again */ 16 | else 17 | return(-1); /* error */ 18 | } 19 | 20 | nleft -= nwritten; 21 | ptr += nwritten; 22 | } 23 | return(n); 24 | } 25 | /* end writen */ 26 | 27 | void 28 | Writen(int fd, void *ptr, size_t nbytes) 29 | { 30 | if (writen(fd, ptr, nbytes) != nbytes) 31 | err_sys("writen error"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /unp/udp/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/udp/client -------------------------------------------------------------------------------- /unp/udp/client2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/udp/client2 -------------------------------------------------------------------------------- /unp/udp/hello: -------------------------------------------------------------------------------- 1 | sfjfjljfljfljfg 2 | ggdfg 3 | shgh 4 | hsd 5 | hgh 6 | h 7 | -------------------------------------------------------------------------------- /unp/udp/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/udp/server -------------------------------------------------------------------------------- /unp/udp/server2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/udp/server2 -------------------------------------------------------------------------------- /unp/udptest/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 一个简单的UDP服务器端/客户端代码. 4 | 5 | ------2014-12-24 vonzhou 6 | -------------------------------------------------------------------------------- /unp/udptest/global.h: -------------------------------------------------------------------------------- 1 | #ifndef GLOBAL_H_ 2 | #define GLOBAL_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /unp/unixdomain/Makefile: -------------------------------------------------------------------------------- 1 | all: mycat openfile 2 | mycat : error.o wrapunix.o my_open.o wrapsock.o read_fd.o 3 | gcc -o mycat mycat.c error.o my_open.o wrapunix.o wrapsock.o read_fd.o 4 | openfile : error.o write_fd.o 5 | gcc -o openfile openfile.c error.o write_fd.o 6 | 7 | error.o : error.c util.h 8 | gcc -c error.c 9 | wrapunix.o : wrapunix.c util.h 10 | gcc -c wrapunix.c 11 | my_open.o : my_open.c util.h 12 | gcc -c my_open.c 13 | wrapsocket.o : wrapsock.c util.h 14 | gcc -c wrapsock.c 15 | read_fd.o : read_fd.c util.h 16 | gcc -c read_fd.c 17 | write_fd.o : write_fd.c util.h 18 | gcc -c write_fd.c 19 | 20 | 21 | 22 | 23 | clean : 24 | -rm *.o 25 | -------------------------------------------------------------------------------- /unp/unixdomain/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/error.o -------------------------------------------------------------------------------- /unp/unixdomain/my_open.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/my_open.o -------------------------------------------------------------------------------- /unp/unixdomain/mycat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/mycat -------------------------------------------------------------------------------- /unp/unixdomain/mycat.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | int my_open(const char *,int); 4 | 5 | int 6 | main(int argc, char **argv){ 7 | int fd,n; 8 | char buff[BUFFSIZE]; 9 | if(argc != 2) 10 | err_quit("usage: mycat "); 11 | if((fd = my_open(argv[1],O_RDONLY)) < 0) 12 | err_sys("cannot open %s.",argv[1]); 13 | while((n = Read(fd,buff,BUFFSIZE)) > 0){ 14 | Write(STDOUT_FILENO, buff, n); 15 | } 16 | exit(0); 17 | } 18 | -------------------------------------------------------------------------------- /unp/unixdomain/openfile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/openfile -------------------------------------------------------------------------------- /unp/unixdomain/openfile.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | // we invoke the standard sys call to open a file and then 4 | //write to the sockfd the opened file's fd 5 | int 6 | main(int argc, char **argv){ 7 | int fd; 8 | if(argc != 4) 9 | err_quit("openfile "); 10 | if((fd = open(argv[2], atoi(argv[3]))) < 0) 11 | exit((errno > 0) ? errno : 255); 12 | if(write_fd(atoi(argv[1]),"",1,fd)< 0) 13 | exit((errno > 0) ? errno : 255); 14 | exit(0);// when successfully , the exit code is 0; 15 | } 16 | -------------------------------------------------------------------------------- /unp/unixdomain/read_fd.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/read_fd.o -------------------------------------------------------------------------------- /unp/unixdomain/wrapsock.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/wrapsock.o -------------------------------------------------------------------------------- /unp/unixdomain/wrapunix.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/wrapunix.o -------------------------------------------------------------------------------- /unp/unixdomain/write_fd.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unp/unixdomain/write_fd.o -------------------------------------------------------------------------------- /unpv2-old/Makefile: -------------------------------------------------------------------------------- 1 | all: px-ipc-name 2 | 3 | px-ipc-name: px_ipc_name.o error.o 4 | gcc -o px-ipc-name px_ipc_name.o error.o 5 | 6 | -------------------------------------------------------------------------------- /unpv2-old/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/error.o -------------------------------------------------------------------------------- /unpv2-old/pipe/Makefile: -------------------------------------------------------------------------------- 1 | all: mainpipe fduplex 2 | 3 | mainpipe: mainpipe.o error.o wrapstdio.o wrapunix.o client.o server.o 4 | gcc -o mainpipe mainpipe.o error.o wrapstdio.o wrapunix.o client.o server.o 5 | 6 | fduplex: fduplex.o error.o wrapstdio.o wrapunix.o client.o server.o 7 | gcc -o fduplex fduplex.o error.o wrapstdio.o wrapunix.o client.o server.o 8 | 9 | error.o: error.c unpipc.h 10 | gcc -c error.c 11 | 12 | wrapstdio.o: wrapstdio.c unpipc.h 13 | gcc -c wrapstdio.c 14 | 15 | wrapunix.o : wrapunix.c unpipc.h 16 | gcc -c wrapunix.c 17 | 18 | client.o : client.c unpipc.h 19 | gcc -c client.c 20 | 21 | server.o : server.c unpipc.h 22 | gcc -c server.c 23 | -------------------------------------------------------------------------------- /unpv2-old/pipe/client.c: -------------------------------------------------------------------------------- 1 | #include "unpipc.h" 2 | 3 | void 4 | client(int readfd, int writefd) 5 | { 6 | size_t len; 7 | ssize_t n; 8 | char buff[MAXLINE]; 9 | 10 | /* read pathname */ 11 | Fgets(buff, MAXLINE, stdin); 12 | len = strlen(buff); /* fgets() guarantees null byte at end */ 13 | if (buff[len-1] == '\n') 14 | len--; /* delete newline from fgets() */ 15 | 16 | /* write pathname to IPC channel */ 17 | Write(writefd, buff, len); 18 | 19 | /* read from IPC, write to standard output */ 20 | while ( (n = Read(readfd, buff, MAXLINE)) > 0) 21 | Write(STDOUT_FILENO, buff, n); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /unpv2-old/pipe/client.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/client.o -------------------------------------------------------------------------------- /unpv2-old/pipe/error.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/error.o -------------------------------------------------------------------------------- /unpv2-old/pipe/fduplex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/fduplex -------------------------------------------------------------------------------- /unpv2-old/pipe/fduplex.c: -------------------------------------------------------------------------------- 1 | #include "unpipc.h" 2 | 3 | int 4 | main(int argc, char **argv) 5 | { 6 | int fd[2], n; 7 | char c; 8 | pid_t childpid; 9 | 10 | Pipe(fd); /* assumes a full-duplex pipe (e.g., SVR4) */ 11 | if ( (childpid = Fork()) == 0) { /* child */ 12 | sleep(3); 13 | if ( (n = Read(fd[0], &c, 1)) != 1) 14 | err_quit("child: read returned %d", n); 15 | printf("child read %c\n", c); 16 | Write(fd[0], "c", 1); 17 | exit(0); 18 | } 19 | /* 4parent */ 20 | Write(fd[1], "p", 1); 21 | if ( (n = Read(fd[1], &c, 1)) != 1) 22 | err_quit("parent: read returned %d", n); 23 | printf("parent read %c\n", c); 24 | exit(0); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /unpv2-old/pipe/fduplex.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/fduplex.o -------------------------------------------------------------------------------- /unpv2-old/pipe/mainpipe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/mainpipe -------------------------------------------------------------------------------- /unpv2-old/pipe/mainpipe.c: -------------------------------------------------------------------------------- 1 | #include "unpipc.h" 2 | 3 | void client(int, int), server(int, int); 4 | 5 | int 6 | main(int argc, char **argv) 7 | { 8 | int pipe1[2], pipe2[2]; 9 | pid_t childpid; 10 | 11 | Pipe(pipe1); /* create two pipes */ 12 | Pipe(pipe2); 13 | 14 | if ( (childpid = Fork()) == 0) { /* child */ 15 | Close(pipe1[1]); 16 | Close(pipe2[0]); 17 | 18 | server(pipe1[0], pipe2[1]); 19 | exit(0); 20 | } 21 | /* 4parent */ 22 | Close(pipe1[0]); 23 | Close(pipe2[1]); 24 | 25 | client(pipe2[0], pipe1[1]); 26 | 27 | Waitpid(childpid, NULL, 0); /* wait for child to terminate */ 28 | exit(0); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /unpv2-old/pipe/mainpipe.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/mainpipe.o -------------------------------------------------------------------------------- /unpv2-old/pipe/server.c: -------------------------------------------------------------------------------- 1 | #include "unpipc.h" 2 | 3 | void 4 | server(int readfd, int writefd) 5 | { 6 | int fd; 7 | ssize_t n; 8 | char buff[MAXLINE+1]; 9 | 10 | /* read pathname from IPC channel */ 11 | if ( (n = Read(readfd, buff, MAXLINE)) == 0) 12 | err_quit("end-of-file while reading pathname"); 13 | buff[n] = '\0'; /* null terminate pathname */ 14 | 15 | if ( (fd = open(buff, O_RDONLY)) < 0) { 16 | /* error: must tell client */ 17 | snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n", 18 | strerror(errno)); 19 | n = strlen(buff); 20 | Write(writefd, buff, n); 21 | 22 | } else { 23 | /* open succeeded: copy file to IPC channel */ 24 | while ( (n = Read(fd, buff, MAXLINE)) > 0) 25 | Write(writefd, buff, n); 26 | Close(fd); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /unpv2-old/pipe/server.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/server.o -------------------------------------------------------------------------------- /unpv2-old/pipe/wrapstdio.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/wrapstdio.o -------------------------------------------------------------------------------- /unpv2-old/pipe/wrapunix.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/pipe/wrapunix.o -------------------------------------------------------------------------------- /unpv2-old/px-ipc-name: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/px-ipc-name -------------------------------------------------------------------------------- /unpv2-old/px_ipc_name.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vonzhou/Coding/0693e369b8982adba5cc4610abf31799d3931475/unpv2-old/px_ipc_name.o --------------------------------------------------------------------------------