为什么孩子的过程行为如此奇怪?

时间:2015-04-03 14:33:40

标签: c++ c linux process

我编写了一个剪辑代码,用于创建子进程。

代码段:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <math.h>
void B();
void C();
void D();
void E();
int main (){
    int status;
    printf("Name: %s, PID: %d, PPID: %d.\n", "A", getpid(), getppid());
    if (fork() == 0) {
        B();
    }
    if (fork() == 0) {
        C();
    }
    wait(&status);  
}
void B(){
    //B process
    int status;
    printf("Name: %s, PID: %d, PPID: %d.\n", "B", getpid(), getppid());
    if (fork() == 0) {
        E();
    }
    if (fork() == 0) {
        D();
    }
    wait(&status);
}
void C(){
    //C process
    printf("Name: %s, PID: %d, PPID: %d.\n", "C", getpid(), getppid());
}
void E(){
    //E process
    printf("Name: %s, PID: %d, PPID: %d.\n", "E", getpid(), getppid());
}
void D(){
    //D process
    printf("Name: %s, PID: %d, PPID: %d.\n", "D", getpid(), getppid());
}

问题是我得到了输出,我根本没想到。 日志:

Name: C, PID: 4571, PPID: 4570.
Name: D, PID: 4572, PPID: 4569.
Name: C, PID: 4573, PPID: 4572.
Name: C, PID: 4574, PPID: 4567.
Name: C, PID: 4575, PPID: 4569.
------------------------------
Name: A, PID: 4576, PPID: 4147.
Name: B, PID: 4577, PPID: 4576.
Name: C, PID: 4578, PPID: 4576.
Name: D, PID: 4580, PPID: 4577.
Name: E, PID: 4579, PPID: 4577.
------------------------------
Name: D, PID: 4582, PPID: 4579.
Name: C, PID: 4581, PPID: 4580.
Name: C, PID: 4583, PPID: 4582.
Name: C, PID: 4584, PPID: 4577.
Name: C, PID: 4585, PPID: 4579.

每次发布​​由-------------------------

分隔

为什么C进程多次运行并且每次都改变他的PPID? 为什么我没有看到来自A流程(父母)的消息? 等待你的答案并链接到资源!

1 个答案:

答案 0 :(得分:3)

您正在看到stdout被缓冲这一事实的副作用。我将你的程序修改为:

  1. 每次致电stdout后,点击printf
  2. 添加了一个基于缩进级别打印缩进的功能。主进程的缩进级别为0。主进程子级的缩进为1等。
  3. 我得到以下输出:

    Name: A, PID: 694812, PPID: 23788.
       Name: B, PID: 695816, PPID: 694812.
       Name: C, PID: 693016, PPID: 694812.
          Name: E, PID: 691184, PPID: 695816.
          Name: D, PID: 695896, PPID: 695816.
             Name: D, PID: 691792, PPID: 691184.
                Name: C, PID: 696268, PPID: 691792.
             Name: C, PID: 695220, PPID: 695896.
          Name: C, PID: 694204, PPID: 695816.
             Name: C, PID: 695496, PPID: 691184.
    

    以下是修改过的程序:

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
    #include <math.h>
    void B();
    void C();
    void D();
    void E();
    
    void printIndent();
    
    int indentLevel = 0;
    
    int main (){
        int status;
        printIndent();
        printf("Name: %s, PID: %d, PPID: %d.\n", "A", getpid(), getppid());
        fflush(stdout);
        if (fork() == 0) {
           ++indentLevel;
           B();
        }
    
        if (fork() == 0) {
           ++indentLevel;
            C();
        }
        wait(&status);  
    }
    
    void B(){
        //B process
        int status;
        printIndent();
        printf("Name: %s, PID: %d, PPID: %d.\n", "B", getpid(), getppid());
        fflush(stdout);
        if (fork() == 0) {
           ++indentLevel;
            E();
        }
        if (fork() == 0) {
           ++indentLevel;
            D();
        }
        wait(&status);
    }
    
    void C(){
        //C process
        printIndent();
        printf("Name: %s, PID: %d, PPID: %d.\n", "C", getpid(), getppid());
        fflush(stdout);
    }
    
    void E(){
        //E process
        printIndent();
        printf("Name: %s, PID: %d, PPID: %d.\n", "E", getpid(), getppid());
        fflush(stdout);
    }
    
    void D(){
        //D process
        printIndent();
        printf("Name: %s, PID: %d, PPID: %d.\n", "D", getpid(), getppid());
        fflush(stdout);
    }
    
    void printIndent()
    {
       int i = 0;
       for ( ; i < indentLevel; ++i )
       {
          printf("   ");
       }
    }