Assume that every fork() call succeeds and every process continues the loop.…
2021
Assume that every fork() call succeeds and every process continues the loop. How many child processes are created by the following code?
for (i = 0; i < n; i++) fork();Answer: B. 2n − 1 — ConceptA successful fork() call creates one new child for every process that executes it. If all existing processes reach the next fork() call, the total…
- A.
n
- B.
2n − 1
- C.
2n
- D.
2n+1 − 1
Attempted by 793 students.
Show answer & explanation
Correct answer: B
Concept
A successful fork() call creates one new child for every process that executes it. If all existing processes reach the next fork() call, the total process count doubles after each call.
Application
Let P0 be the initial number of processes. Before the loop starts, P0 = 1.
At iteration k, every one of the Pk processes creates one child, so Pk+1 = 2Pk.
Applying this recurrence n times gives Pn = 2n.
The original process is not a child. Therefore, created children = Pn − P0 = 2n − 1.
Cross-check
For n = 3, the total process count follows 1 → 2 → 4 → 8. Thus 8 − 1 = 7 new child processes.
Result: 2n − 1 child processes.