I'm trying to understand how fork works, and I know that when you call fork() another process is created that resumes from the exact same line with the heap and stack copied to it. For the parent, fork() returns the PID of the child and for the child it returns 0.
I stumbled across this question: "How many processes does the following sequence create?"
fork();fork();wait(0);fork();wait(0);
The answer is 8, including the parent, but I do not clearly understand if wait(0) actually waits or not. I found this related to the manual of "wait":
If there are at least one child processes running when the call to wait() is made, the caller will be blocked until one of its child processes exits. At that moment, the caller resumes its execution. If there is no child process running when the call to wait() is made, then this wait() has no effect at all. That is, it is as if no wait() is there.
My understanding of this can be drawn as such:
Where the leftmost branch is the parent and the black circles are where forks were made. This makes me believe that the wait(0)'s do nothing because there are no children to wait for, so it's simply ignored. However, i made a little modification to the code, adding an "index" to each process.
#include <stdio.h>
int main()
{
int index = 0;
if (fork()==0)
index++;
if (fork()==0)
index++;
wait(0);
if (fork()==0)
index++;
wait(0);
printf("%d\n",index);
}
And this prints out 21312021
After commenting the wait(0)'s out, so the code is:
#include <stdio.h>
int main()
{
int index = 0;
if (fork()==0)
index++;
if (fork()==0)
index++;
if (fork()==0)
index++;
printf("%d\n",index);
}
It prints out 0112223, so something is clearly different. Why are the results different?