First fork:
After the first fork, there are now two processes: the original parent process and its child process (n=1).
Each of these processes continues to the next line (Second fork).
Second fork:
In the original parent process (from the first fork), another fork system call creates a new child process, resulting in two processes at this point (n=2).
Simultaneously, in the child process (from the first fork), another fork system call creates another child process, resulting in two processes at this point (n=2).
So, for n=2 (two fork system calls), the total number of processes created is indeed 22 = 4.
Each fork call doubles the number of processes because each process splits into two (parent and child) at that point in the execution flow. Therefore, there are a total of four processes spawned as a result of the two fork
calls in the code snippet. Each fork
call doubles the number of processes at that point in the execution flow.