Fork works like a binary tree. So its always 2^x number of processes on every x number of fork calls.
Lets understand with your example.
First fork() call:
When the first fork()
is called. The parent process creates a new process. So we have 2
threads.
Second fork() call:
At this point we have two processes(main process and a new created process).These two threads will call second fork individually and create 2 new processes each. so we have 4 threads.
You might have got the idea by now. Every time a fork() is encountered all the processes create their respective child processes(double themselves).