這是SOR到目前爲止,我已經做了:嵌套fork()的樹++
#include <stdio.h>
#include <stdlib.h>
int main()
{
int p1,p2,p3,p4,i;
int left,leftPid;
int right;
left=fork();
right=fork();
for(i=0;i<=2;i++)
{
if(left==0)
printf("\nleft Child Process. Level: %d | myPID: %d | myParent: %d\n",i,getpid(),getppid());
else
leftPID=left;
if (right==0)
{
printf("\nright Child Process. Level: %d | myPID: %d | myParent: %d\n",i,getpid(),getppid());
right=fork();
}
else
{
printf("\nParent Process. Level %d | My left Child: %d | My right Child: %d | myPID: %d\n",i,leftPID,right,getpid());
}
}
}
我需要那種輸出的:
左子進程。等級:1 | myPID:23560 | myParent:23559
父進程。等級:0 |我的左小孩:23560 |我的右邊小孩:23561 | myPID:23559
left Child Process。等級:2 | myPID:23562 | myParent:23561
left Child Process。等級:3 | myPID:23564 | myParent:23563
right Child Process。等級:3 | myPID:23565 | myParent:23563
父進程。等級:2 |我的左小孩:23564 |我的右邊小孩:23565 | myPID:23564
父進程。等級:1 |我的左小孩:23562 |我的右邊小孩:23563 | myPID:23561
這裏是一個樹表示什麼,我需要:
而且我做的代碼是遠離我需要什麼。我希望有人能幫我解決這個問題。