在 C 中创建子进程

  • 本文关键字:子进程 创建 c linux
  • 更新时间 :
  • 英文 :


是否可以帮助我创建短程序,其中父进程产生 1 个子进程,子进程产生下一个子进程,后续子进程生成下一个子进程,等等。这样所有的孩子总共都是16。如何使用 pstree 命令,呈现这些进程的树?到目前为止,我能够写出类似的东西,但我不确定它是否正确:

int main()
{
    for(int i = 0; i < 16; ++i)
    {
        pid_t pid = fork();
        if(pid != 0)
        {
            waitpid(pid, NULL, 0);
            return 0;
        }
    }
    sleep(5);
    return 0;
}

正如我在评论中指出的那样,添加诊断打印以帮助您了解正在发生的事情。 关键值是循环控制、i、过程的 PID 和 PPID 以及 pid 变量中的值。 您还希望检测waitpid()调用。

这导致代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
    for (int i = 0; i < 16; ++i)
    {
        pid_t pid = fork();
        printf("%d: (PPID = %d) - post fork %d: pid = %dn",
               (int)getpid(), (int)getppid(), i, (int)pid);
        fflush(0);
        if (pid != 0)
        {
            int status;
            int corpse = waitpid(pid, &status, 0);
            printf("%d: (PPID = %d) child %d exited with status 0x%.4Xn",
                   (int)getpid(), (int)getppid(), corpse, status);
            fflush(0);
            return 0;
        }
    }
    printf("%d: (PPID = %d) sleepingn", (int)getpid(), (int)getppid());
    fflush(0);
    sleep(5);
    return 0;
}

如果将输出运行到终端,则不需要fflush()调用。 如果您通过管道运行输出(就像我在生成下面的输出时所做的那样(,它们变得至关重要。 另请参阅printf() fork()后的异常。 如果您将if中的return 0;替换为return i;,您还会得到一些有趣的信息。

运行该程序会导致类似于以下内容的输出:

74830: (PPID = 72799) - post fork 0: pid = 74833
74833: (PPID = 74830) - post fork 0: pid = 0
74833: (PPID = 74830) - post fork 1: pid = 74834
74834: (PPID = 74833) - post fork 1: pid = 0
74834: (PPID = 74833) - post fork 2: pid = 74835
74835: (PPID = 74834) - post fork 2: pid = 0
74835: (PPID = 74834) - post fork 3: pid = 74836
74836: (PPID = 74835) - post fork 3: pid = 0
74836: (PPID = 74835) - post fork 4: pid = 74837
74837: (PPID = 74836) - post fork 4: pid = 0
74837: (PPID = 74836) - post fork 5: pid = 74838
74838: (PPID = 74837) - post fork 5: pid = 0
74838: (PPID = 74837) - post fork 6: pid = 74839
74839: (PPID = 74838) - post fork 6: pid = 0
74839: (PPID = 74838) - post fork 7: pid = 74840
74840: (PPID = 74839) - post fork 7: pid = 0
74840: (PPID = 74839) - post fork 8: pid = 74841
74841: (PPID = 74840) - post fork 8: pid = 0
74841: (PPID = 74840) - post fork 9: pid = 74842
74842: (PPID = 74841) - post fork 9: pid = 0
74842: (PPID = 74841) - post fork 10: pid = 74843
74843: (PPID = 74842) - post fork 10: pid = 0
74843: (PPID = 74842) - post fork 11: pid = 74844
74844: (PPID = 74843) - post fork 11: pid = 0
74844: (PPID = 74843) - post fork 12: pid = 74845
74845: (PPID = 74844) - post fork 12: pid = 0
74845: (PPID = 74844) - post fork 13: pid = 74846
74846: (PPID = 74845) - post fork 13: pid = 0
74846: (PPID = 74845) - post fork 14: pid = 74847
74847: (PPID = 74846) - post fork 14: pid = 0
74847: (PPID = 74846) - post fork 15: pid = 74848
74848: (PPID = 74847) - post fork 15: pid = 0
74848: (PPID = 74847) sleeping
74847: (PPID = 74846) child 74848 exited with status 0x0000
74846: (PPID = 74845) child 74847 exited with status 0x0000
74845: (PPID = 74844) child 74846 exited with status 0x0000
74844: (PPID = 74843) child 74845 exited with status 0x0000
74843: (PPID = 74842) child 74844 exited with status 0x0000
74842: (PPID = 74841) child 74843 exited with status 0x0000
74841: (PPID = 74840) child 74842 exited with status 0x0000
74840: (PPID = 74839) child 74841 exited with status 0x0000
74839: (PPID = 74838) child 74840 exited with status 0x0000
74838: (PPID = 74837) child 74839 exited with status 0x0000
74837: (PPID = 74836) child 74838 exited with status 0x0000
74836: (PPID = 74835) child 74837 exited with status 0x0000
74835: (PPID = 74834) child 74836 exited with status 0x0000
74834: (PPID = 74833) child 74835 exited with status 0x0000
74833: (PPID = 74830) child 74834 exited with status 0x0000
74830: (PPID = 72799) child 74833 exited with status 0x0000

如果你感觉更挑剔,你可以为打印操作添加时间——你可能需要在微秒级报告才能看到很大的差异。

最新更新