Here\'s a code where I use 2 fork() system calls one after another - How does it actually work?
#include
#include
usi
Just draw a tree where each root node is a fork call and leaf nodes are the code following it.
In the first program, your cout<<"0..." is the parent program and after the fork call, the whole program after that line executes twice.Hence the two "1..." output lines.
Now there is another fork call after this. This time there are 3 processes running( 1.The original parent you invoked, 2. The child it spawned 3. The child itself spawns another grandchild.)
Hence the 3 "2..." output lines.