为什么在我的centos中打印“ hello”三遍?

时间:2019-07-25 09:59:13

标签: c fork

#include "csapp.h"

int main()
{
        int i;

        for (i = 0; i < 2; i++)
                fork();
        printf("hello\n");
        exit(0);
}
/*
 *                .------------------------
 *                |
 *                |
 *                |
 *    .-----------.------------------------
 *    |
 *    |
 *    |           .------------------------
 *    |           |
 *    |           |
 *    |           |
 *    .-----------.------------------------
 *   fork        fork
 *   i=0         i=1
 */

在过程图片中,该代码似乎将打印“ hello”四次。 为什么要在我的centos中打印“ hello”三遍?

2 个答案:

答案 0 :(得分:5)

除非您的csapp.h标头中有些奇怪,否则应该打印出四行,因为在循环的第一次迭代(增量之前的i == 0之后),fork()会创建两个进程,并在第二次迭代(增量之前的i == 1)上,这两个进程分别执行fork(),以创建另外两个进程。

当此代码在macOS 10.14.6上运行时,我得到4行:hello

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
        int i;

        for (i = 0; i < 2; i++)
                fork();
        printf("hello\n");
        exit(0);
}

输出:

hello
hello
hello
hello

尽管如此,我所提供的功能远远超过问题中显示的最少代码—

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int i;
    printf("P0: PID = %d, PPID = %d\n", (int)getpid(), (int)getppid());
    fflush(stdout);

    for (i = 0; i < 2; i++)
    {
        int pid = fork();
        printf("PF: i = %d, PID = %d, PPID = %d, fork = %d\n",
                i, (int)getpid(), (int)getppid(), pid);
        fflush(stdout);
    }
    printf("Hello: PID = %d, PPID = %d\n", (int)getpid(), (int)getppid());
    return(0);
}

请注意大量使用fflush()来避免printf() anomaly after fork()

我从中得到的示例输出是:

P0: PID = 5039, PPID = 916
PF: i = 0, PID = 5039, PPID = 916, fork = 5042
PF: i = 1, PID = 5039, PPID = 916, fork = 5043
Hello: PID = 5039, PPID = 916
PF: i = 0, PID = 5042, PPID = 5039, fork = 0
PF: i = 1, PID = 5043, PPID = 1, fork = 0
Hello: PID = 5043, PPID = 1
PF: i = 1, PID = 5042, PPID = 1, fork = 5044
Hello: PID = 5042, PPID = 1
PF: i = 1, PID = 5044, PPID = 5042, fork = 0
Hello: PID = 5044, PPID = 5042

请注意,其中两个进程报告了PPID = 1,因为父进程(5039)已经退出。添加一个循环来等待孩子死亡并报告其退出状态是可行/明智的。

#include <sys/wait.h>

…

int corpse;
int status;
while ((corpse = wait(&status)) > 0)
{
    printf("WT: PID = %d, PPID = %d, child %d exited 0x%.4X\n",
           (int)getpid(), (int)getppid(), corpse, status);
    fflush(stdout);
}

您在CentOS上能得到什么?

我正在终端窗口中从命令行运行程序。如果从IDE或其他方式运行此程序,则可能会丢失孤立进程的输出。添加wait()循环将阻止第一个进程退出,直到其所有(两个)子进程都退出为止,从而有序显示4条“ Hello”行。我对输出格式进行了重新设计,以便于阅读输出。

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    int i;
    printf("P0: PID = %5d, PPID = %5d\n", (int)getpid(), (int)getppid());
    fflush(stdout);

    for (i = 0; i < 2; i++)
    {
        int pid = fork();
        printf("PF: PID = %5d, PPID = %5d, i = %d, fork = %5d\n",
                (int)getpid(), (int)getppid(), i, pid);
        fflush(stdout);
    }

    printf("hello\n");
    printf("HO: PID = %5d, PPID = %5d\n", (int)getpid(), (int)getppid());
    fflush(stdout);

    int corpse;
    int status;
    while ((corpse = wait(&status)) > 0)
    {
        printf("WT: PID = %5d, PPID = %5d, child %5d exited 0x%.4X\n",
                (int)getpid(), (int)getppid(), corpse, status);
        fflush(stdout);
    }

    printf("EX: PID = %5d, PPID = %5d\n", (int)getpid(), (int)getppid());
    return(0);
}

示例输出(无孤立进程):

P0: PID =  5245, PPID =   916
PF: PID =  5245, PPID =   916, i = 0, fork =  5248
PF: PID =  5248, PPID =  5245, i = 0, fork =     0
PF: PID =  5245, PPID =   916, i = 1, fork =  5249
hello
HO: PID =  5245, PPID =   916
PF: PID =  5248, PPID =  5245, i = 1, fork =  5250
PF: PID =  5249, PPID =  5245, i = 1, fork =     0
hello
HO: PID =  5248, PPID =  5245
hello
HO: PID =  5249, PPID =  5245
EX: PID =  5249, PPID =  5245
PF: PID =  5250, PPID =  5248, i = 1, fork =     0
hello
HO: PID =  5250, PPID =  5248
EX: PID =  5250, PPID =  5248
WT: PID =  5245, PPID =   916, child  5249 exited 0x0000
WT: PID =  5248, PPID =  5245, child  5250 exited 0x0000
EX: PID =  5248, PPID =  5245
WT: PID =  5245, PPID =   916, child  5248 exited 0x0000
EX: PID =  5245, PPID =   916

答案 1 :(得分:1)

叉子可能失败,或者printf之间存在冲突。在ideone上,它可以正确打印4次。

enter image description here