程序仅打印僵尸进程中的行

时间:2018-06-26 09:00:09

标签: c linux unix process fork

我有以下源代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
    int p, i;
    p=fork();
    if (p == -1) {perror("fork() error "); exit(EXIT_FAILURE);}
    if (p == 0) { 
        for (i = 0; i < 10; i++) 
            printf("Child: i=%d pid=%d, ppid=%d\n", i, getpid(), getppid());
        exit(0);
    } else { 
        for (i = 0; i < 10; i++)
            printf("Parent: i=%d pid=%d ppid=%d\n", i, getpid(), getppid());
        wait(0); 
    }
}

如果父进程决定不等待子进程(删除包含wait(0)语句的行),则在终端上将仅打印子进程中的那些行。如果我选择将程序的输出重定向到任意文件(即:./source_code> some_file),则some_file文件将包含来自父进程和子进程的行,而不仅仅是上述子进程的行。这怎么可能?

0 个答案:

没有答案
相关问题