I have been trying to understand fork()
behavior. This time in a for-loop
. Observe the following code:
#include <stdio.h>
void main()
{
int i;
for (i=0;i<3;i++)
{
fork();
// This printf statement is for debugging purposes
// getppid(): gets the parent process-id
// getpid(): get child process-id
printf("[%d] [%d] i=%d
", getppid(), getpid(), i);
}
printf("[%d] [%d] hi
", getppid(), getpid());
}
Here is the output:
[6909][6936] i=0
[6909][6936] i=1
[6936][6938] i=1
[6909][6936] i=2
[6909][6936] hi
[6936][6938] i=2
[6936][6938] hi
[6938][6940] i=2
[6938][6940] hi
[1][6937] i=0
[1][6939] i=2
[1][6939] hi
[1][6937] i=1
[6937][6941] i=1
[1][6937] i=2
[1][6937] hi
[6937][6941] i=2
[6937][6941] hi
[6937][6942] i=2
[6937][6942] hi
[1][6943] i=2
[1][6943] hi
I am a very visual person, and so the only way for me to truly understand things is by diagramming. My instructor said there would be 8 hi statements. I wrote and ran the code, and indeed there were 8 hi statements. But I really didn’t understand it. So I drew the following diagram:
Diagram updated to reflect comments :)
Observations:
- Parent process (main) must iterate the loop 3 times. Then printf is called
- On each iteration of parent for-loop a fork() is called
- After each fork() call, i is incremented, and so every child starts a for-loop from i before it is incremented
- At the end of each for-loop, "hi" is printed
Here are my questions:
- Is my diagram correct?
- Why are there two instances of
i=0
in the output?
- What value of
i
is carried over to each child after the fork()? If the same value of i
is carried over, then when does the "forking" stop?
- Is it always the case that
2^n - 1
would be a way to count the number of children that are forked? So, here n=3
, which means 2^3 - 1 = 8 - 1 = 7
children, which is correct?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…