【发布时间】:2016-09-16 23:04:44
【问题描述】:
我正在做一个关于 Unix 进程的项目,使用 languaje C。该项目是使用等待(函数 fork())exit() 来表示进程树。
我的程序的输出应该是我:
- 父亲:PID1 - 你好
- 孩子 1:PID2 - 你好
- GrandChildren1.1:PID3 - 你好,再见
- GrandChildren1.2:PID4 - 你好,再见
- 儿童 1:PID2 - 再见
- Children2:Pid6 - 你好
- GrandChildren2.1:Pid7 - 你好,再见
- Children2:Pid6 - 再见
- 父亲:PID1 - 再见
这个输出明白了吧。 我将以下代码安排到流程的开头,但没有安排它们如何完成流程:
main() {
if(fork())
{
// 1
printf("1. Father:Pid1 - Hello\n");
if(fork())
{
// 2
printf("2. Children1:Pid2 - Hello\n");
if(fork())
{
// 4
printf("3. GrandChildren1.1:Pid3 - Hello and GoodBye\n");
}
else{}
wait(1);
if (fork())
{
printf("3. GrandChildren1.2:Pid4 - Hello and GoodBye\n");
}
else {}
}
else
{
printf("4. Children1:Pid2 - GoodBye\n");
}
}
else{
if(fork())
{
// 3}
printf("6. Children2:Pid6 - Hello\n");
if (fork())
{
printf("6. GrandChildren2.1:Pid7 - Hello and GoodBye\n");
}
else{}
}
else{}
}
pause();
return 0;
}
这个输出明白了吧。 我将以下代码安排到流程的开头,但没有安排它们如何完成流程:
比如不知道怎么做完PID2,看到你打印2个孙子的时候,我要了。
怎么可能?
问候
【问题讨论】:
-
了解
kill()函数和信号处理。 -
wait(1);并阅读wait man page,因为这不是正确的称呼方式。 -
你好iharob,我用的不是kill,只是fork,wait,waitpid,exit,write,指令。
-
你应该在你的问题中解释一下,如果它必须没有杀戮
标签: c operating-system fork