是否可以帮助我创建一个简短的程序,在该程序中,父进程产生一个子进程,该子进程产生下一个子进程,随后的子进程产生下一个子进程,依此类推.....总共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;
}
答案 0 :(得分:1)
正如我在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()
。如果将return 0;
中的if
替换为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
如果您感到更加挑剔,则可以为打印操作增加时间-您可能需要以微秒为单位进行报告,以了解更多差异。