Question 10 How many child process is created if below code is executed (Assume compilation...
IBM technical mcq question, verified with a worked answer. Free to practise - no sign-up.
How many child process is created if below code is executed (Assume compilation is successful).
for(i=0; i<n; i++) fork();
Pick ONE option
Show answer & explanation
Each fork() call doubles the number of processes (parent and child both continue executing). After n iterations, there are 2^n total processes. Since we count only child processes (excluding the original parent), the answer is (2^n) - 1. Options A, C, and D represent linear growth, which is incorrect for exponential process creation.
Step-by-step Derivation:
Trace through iterations:
- Before loop: 1 process (parent)
- After i=0: fork() creates 1 child → 2 processes total, 1 child created
- After i=1: Both 2 processes fork → 4 processes total, 2 new children created
- After i=2: All 4 processes fork → 8 processes total, 4 new children created
- After i=n-1: 2^(n-1) processes fork → 2^n total processes
Total child processes = 2^n (total) - 1 (original parent) = (2^n) - 1
General formula: After n fork() calls in sequence where each process forks, total processes = 2^n, and child processes = 2^n - 1.