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 − 1ConceptA successful fork() call creates one new child for every process that executes it. If all existing processes reach the next fork() call, the total…

  1. A.

    n

  2. B.

    2n − 1

  3. C.

    2n

  4. D.

    2n+1 − 1

Attempted by 790 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

  1. Let P0 be the initial number of processes. Before the loop starts, P0 = 1.

  2. At iteration k, every one of the Pk processes creates one child, so Pk+1 = 2Pk.

  3. Applying this recurrence n times gives Pn = 2n.

  4. 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.

Explore the full course: Rssb Senior Computer Instructor

Loading lesson…