Tinyhttpd 知识点

1. fork 子进程

 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
",getpid());
14             count ++;
15             printf("count is:%d
",count);
16             sleep(1);
17         }else{
18             printf("i am the parent process,my process id is %d
",getpid());
19             printf("count is:%d
",count);
20             sleep(1);
21         }
22     }
23     return 0;
24 }

运行结果:

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 不是同一个参数

├─sshd─┬─sshd───bash───pstree
        │      ├─sshd───bash───a.out───a.out
        │      └─sshd───bash

从pstree 指令中可以看出 a.out 之间的关系

[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 掉

发现只有 父进程在独自运行

 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 父进程

发现只有子进程在 独立运行

 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
原文地址:https://www.cnblogs.com/qifei-liu/p/10628286.html