Counting child processes across repeated forks
Processes, Threads & System Calls

Answer & solution
Official answer: 31
Step through it (33 steps)
Method: count the branching rounds; every process alive at a fork round executes that fork and then continues the same loop.
1. The loop values are 0 through 9. The even values 0,2,4,6,8 cause five fork rounds; odd iterations create no processes.
2. A successful fork creates one new child from each currently executing process. Both parent and child continue from immediately after the fork with their own copy of i, so both participate in later rounds.
3. Starting from one process, the total after successive rounds is 2,4,8,16,32. The numbers of new children produced at those rounds are respectively 1,2,4,8,16.
4. Adding the created children gives 1+2+4+8+16 = 31. Equivalently, subtract the original from the final total: 25−1 = 31.
Result: 31 child processes in total, including descendants created by children. Counting only the five calls made by the original parent would omit later generations. The result assumes no fork failure or external process termination.

