gpt4 book ai didi

在 C 中创建子进程

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:32:24 26 4
gpt4 key购买 nike

是否可以帮助我创建短程序,其中父进程产生 1 个子进程,子进程产生下一个子进程,随后的子进程产生下一个子进程,等等......这样所有的 child 都是总共 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;
}

最佳答案

正如我在 comment 中指出的那样, 添加诊断打印以帮助您了解发生了什么。关键值是循环控制、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 = %d\n",
(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%.4X\n",
(int)getpid(), (int)getppid(), corpse, status);
fflush(0);
return 0;
}
}

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

如果您将输出运行到终端,则不需要调用 fflush()。如果您通过管道运行输出(就像我在下面生成输出时所做的那样),它们就变得至关重要。另见 printf() anomaly after 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

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

关于在 C 中创建子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55912112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com