Here\'s a code where I use 2 fork() system calls one after another - How does it actually work?
#include
#include
usi
when a fork() is called, a child process is created and ran. So you are getting the following statement executed once in the child process:
cout << "1. I am process " << getpid() << endl;
Further, when another fork is called, another child process is created which runs the next 'cout' statement. However, the parent process is also ran. This happens for the third fork() also.
All this takes place in the child process of the first fork(). After this the parent process for the 1st fork runs as well to show your output.