Your understanding is almost correct. However, you cannot be sure about parent of 4472
and 2288
, they could be either 2196
or 6560
.
To find out, you can change the code to the following :
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
cout << "0. I am process " << getpid() << " child of " << getppid() << endl;
(void) fork();
cout << "1. I am process " << getpid() << " child of " << getppid() <<endl;
(void) fork();
cout << "2. I am process " << getpid() << " child of " << getppid() <<endl;
}
Note
In general, with n
fork()
calls, you will end up with 2 ^ (n)
unique processes.
For example, n = 2
here, and thus you get 2 ^(2) = 4
unique processes.