• 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
  • 相关阅读:
    HBase(九)HBase表以及Rowkey的设计
    iOS-动态库创建(详解)
    组件化-动态库实战
    Xcode创建子工程以及工程依赖
    XMPP详解
    iOS中WebKit框架应用与解析
    HYStockChart, 股票图(包括K线图、趋势图、成交量、滚动、放大缩小等)
    OC与JS交互
    iOS 与 js交互的其一方法 WebViewJavascriptBridge的使用
    OC和JS调用
  • 原文地址:https://www.cnblogs.com/qifei-liu/p/10628286.html
Copyright © 2020-2023  润新知