#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
for(i=0;i<4;i++)
fork();
return 0;
}
my question:Include the initial parent process, how many processes created by the program?
I think this answer is 1+4=5, 1 parent process and 4 child process. Am I right?
for(i=0;i<4;i++)
fork();
This piece of code is equivalant to:
fork();
fork();
fork();
fork();
Lets say that process start with p0. The process graph will look like:
The above image says it all, when first fork() gets executed it creates a new process and three fork() are left to execute. Now, here exists a parent thread and a child thread to execute three more fork() statments. So, child process p1 will create p5,p6 and p7 and parent process will create p2, p3 and p4. After p2 has been created two more fork() needs to be executed for this child thread p2 which was created after execution of 2nd fork() statment. In this way, this parent-child processes are created.