Here is my code for fork() system call,
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
int main(int argc, char *argv[])
{
pid_t pid;
pid=fork();
printf("1st Fork\n");
printf("Process ID : %d, Parent Process ID : %d\n",getpid(),getppid());
pid=fork();
printf("2nd Fork\n");
printf("Process ID : %d, Parent Process ID : %d\n",getpid(),getppid());
pid=fork();
printf("3rd Fork\n");
printf("Process ID : %d, Parent Process ID : %d\n",getpid(),getppid());
return 0;
}
While running the code, i am getting output like
1st Fork
Process ID : 3393, Parent Process ID : 3392
2nd Fork
Process ID : 3394, Parent Process ID : 3393
3rd Fork
Process ID : 3395, Parent Process ID : 3394
3rd Fork
Process ID : 3394, Parent Process ID : 3393
2nd Fork
Process ID : 3393, Parent Process ID : 3392
3rd Fork
Process ID : 3397, Parent Process ID : 3393
3rd Fork
Process ID : 3393, Parent Process ID : 3392
1st Fork
Process ID : 3392, Parent Process ID : 3440
2nd Fork
Process ID : 3398, Parent Process ID : 3392
3rd Fork
Process ID : 3400, Parent Process ID : 3398
3rd Fork
Process ID : 3398, Parent Process ID : 3392
2nd Fork
Process ID : 3392, Parent Process ID : 3440
3rd Fork
Process ID : 3401, Parent Process ID : 3392
3rd Fork
Process ID : 3392, Parent Process ID : 3440
Why this fork() system call making 8 process and how?
Also i am getting 14 printf() statement executions. Why?
Each time you call fork
it returns twice. Once in the parent, once in a new process. Then as they go on, they both fork again.
It's most likely you didn't expect the children to fork again. With each fork
you usually have:
switch (fork()) {
case -1:
/* ERROR. */
break;
case 0:
/* Child process. */
break;
default:
/* Parent. */
break;
}
In your code it's like this:
- You have one process, then it forks
- You now have two processes and both fork
- You now have 4 processes and all of them fork
- You now have 8 processes and you're asking questions on SO
The purpose of fork() is to create a new process, which becomes the child process of the caller.
After a new child process is created, both processes will execute the next instruction following the fork() system call.
So, in your code, every fork, will generate two different execution paths, like a height 4 binary tree.
*
* *
* * * *
* * * * * * * *
Totally it's 15 nodes, that means you create 14 another processes.
I suggest taking another good hard look at your output -- there are eight processes:
$ cat <<EOF | sort -u
> 1st Fork
> Process ID : 3393, Parent Process ID : 3392
> 2nd Fork
> Process ID : 3394, Parent Process ID : 3393
> 3rd Fork
> Process ID : 3395, Parent Process ID : 3394
> 3rd Fork
> Process ID : 3394, Parent Process ID : 3393
> 2nd Fork
> Process ID : 3393, Parent Process ID : 3392
> 3rd Fork
> Process ID : 3397, Parent Process ID : 3393
> 3rd Fork
> Process ID : 3393, Parent Process ID : 3392
> 1st Fork
> Process ID : 3392, Parent Process ID : 3440
> 2nd Fork
> Process ID : 3398, Parent Process ID : 3392
> 3rd Fork
> Process ID : 3400, Parent Process ID : 3398
> 3rd Fork
> Process ID : 3398, Parent Process ID : 3392
> 2nd Fork
> Process ID : 3392, Parent Process ID : 3440
> 3rd Fork
> Process ID : 3401, Parent Process ID : 3392
> 3rd Fork
> Process ID : 3392, Parent Process ID : 3440
> EOF
1st Fork
2nd Fork
3rd Fork
Process ID : 3392, Parent Process ID : 3440
Process ID : 3393, Parent Process ID : 3392
Process ID : 3394, Parent Process ID : 3393
Process ID : 3395, Parent Process ID : 3394
Process ID : 3397, Parent Process ID : 3393
Process ID : 3398, Parent Process ID : 3392
Process ID : 3400, Parent Process ID : 3398
Process ID : 3401, Parent Process ID : 3392
Eight processes make sense: you have three fork(2)
calls in your program and each fork(2)
call will duplicate the running process. Since the three fork(2)
calls aren't protected from each other by branching, all three will execute: 2^3 == 8.
Update
I liked Chris's display but wanted to correct the idea that there are fifteen total processes. So I'm going to amend his triangle using different symbols for each of the processes:
~
~ $
~ @ $ ^
~ ! @ # $ % ^ &
Each time fork() is called , a copy of parent process is created from that point which is called child process.
So,after first fork you have two process(child and parent)
Likewise,
second fork() will be called in two above process,which results in additional two process.
Likewise multiplication goes on.