1. fork 子進程docker
1 #include <stdio.h> 2 #include <unistd.h> 3 4 int main(void) 5 { 6 pid_t pid; 7 int count = 0; 8 pid = fork(); 9 while(1){ 10 if(pid < 0) 11 printf("error in fork"); 12 else if(pid == 0){ 13 printf("i am the child process, my process id is %d\n",getpid()); 14 count ++; 15 printf("count is:%d\n",count); 16 sleep(1); 17 }else{ 18 printf("i am the parent process,my process id is %d\n",getpid()); 19 printf("count is:%d\n",count); 20 sleep(1); 21 } 22 } 23 return 0; 24 }
運行結果:bash
i am the parent process,my process id is 8490 count is:0 i am the child process, my process id is 8491 count is:206
能夠看出 子進程和 父進程之間的參數 count 不是同一個參數ssh
├─sshd─┬─sshd───bash───pstree
│ ├─sshd───bash───a.out───a.out
│ └─sshd───bash
從pstree 指令中能夠看出 a.out 之間的關係spa
[root@docker01 tinyhttpd_qf]# ps -ef|grep a.out root 8655 7712 0 05:31 pts/3 00:00:00 ./a.out root 8656 8655 0 05:31 pts/3 00:00:00 ./a.out root 8694 7423 0 05:33 pts/2 00:00:00 grep --color=auto a.out
如今我把 a.out 子進程 kill 掉code
發現只有 父進程在獨自運行blog
am the parent process,my process id is 8655 count is:0 i am the parent process,my process id is 8655 count is:0 i am the parent process,my process id is 8655 count is:0
重啓 程序,而後先kill 父進程進程
發現只有子進程在 獨立運行get
is:49 i am the child process, my process id is 8780 count is:50 i am the child process, my process id is 8780 count is:51 i am the child process, my process id is 8780 count is:52 i am the child process, my process id is 8780 count is:53 i am the child process, my process id is 8780