int x=0;
int main()
{
for(i=0;i<2;i++)
{
fork();
x=x+5;
}
return 0;
}

I am a newbie to the fork() concept. Is the above tree (with x values) a correct solution for the C code mentioned above? The values in the nodes are the x values of their processes respectively.
And also can we return values to the parent process from the child process? Suppose in the above example code can I return the x value of the child to the parent process?
You mean that’s a process tree and in the bubbles is the value of
x? Then no, that’s not correct.When a child is spawned, it gets an exact copy of the parent… so let’s “print” some values so we can see the state of things (I’m makeing up PIDs for everything)
When we start, it’s just the parent:
Then we hit the
fork(), now we have two processes:Next the math:
When we loop back up, our i’s will be incremented, and each process now runs the loop and hits
fork():Now everyone hits the math again:
Finally everyone hits the loop and increments
ibreaking from it. So your end result is: