我需要编写一个代码,通过分叉创建下面的树,但是进程P0在它终止之前等待P2并且P2至少等待它的孩子!
P0
/ \
P1 P2
/|\
P3 P4 P5
答案 0 :(得分:0)
两个for循环可以帮助您创建该进程树。第一个循环有助于创建
for(int child = 0; child < 2; child++){
pid = fork();
if(pid<0){
perror("\n there is a fork() error. \n");
}
else if (pid == 0){
//P1
//P2
if(child == 1){
for(int grandchild = 0; grandchild < 3; grandchild++){
fork();
if(pid==0){
if(grandchild == 2){
wait(NULL);
}
else{}
}
}
}
}
else{
//parent portion
if(child == 1){
wait(NULL);
}
}
}
它的语法不准确,但我相信它会指出你正确的方向。