Given code :
main()
{
if(!fork()) // FORK-1
{
if(!fork()) // FORK-2
fork(); // FORK-3
}
fork(); // FORK-4
}
NOTE : When fork() command is called then copy of the parent process will be created.
Let, P be the parent process and C be its child, then fork() will return child Id(Some non-zero
value) to parent process and 0 will be returned to Child Process C.
NOTE : If(Non-zero value) --> TRUE | If(Zero Value) --> FALSE
If(!Non-zero value) --> FALSE | If(!Zero Value) --> TRUE
Let P be the parent process which start execution of given code :
1. P will call FORK-1.
P will get Process ID of child which is non-zero value and negation of that will be FALSE. So, P will call
FORK-4 and will go out
But, when FORK-1 is called Child A will get 0 or FALSE and negation of FALSE will be TRUE. So, A will
go inside $1^{st}$ If clause.
2. For the execution of $2^{nd}$ we can use the same understanding that we have developed above and
tree will look something like this :
Here, A will be the parent and after calling FORK-2, A will execute FORK-4 and will go out. Whereas, After
calling FORK-2, D will be it's child and D will also execute FORK-3.
3. Now D & F both will execute FORK-4 & tree will look something like this :
All the leaf node except P are child processes.
Parent Process : P
Child Process : A, B, D, E, F, G, H
No. of Child Processes : 7