The total number of child processes in the following code: for (i = 0; i < n;…

2022

The total number of child processes in the following code: for (i = 0; i < n; i++) fork();

  1. A.

    n

  2. B.

    2n−1

  3. C.

    2n

  4. D.

    2n+1−1

Attempted by 407 students.

Show answer & explanation

Correct answer: B

Start with one process. Each fork() call duplicates every existing process, so the number of processes doubles on each iteration.

  • Initial processes = 1.

  • After 1 fork() call: 2^1 = 2 processes.

  • After k fork() calls: 2^k processes.

  • After n fork() calls: total processes = 2^n.

  • Number of child processes (excluding the original parent) = 2^n − 1.

Example: if n = 3, total processes = 2^3 = 8, so child processes = 8 − 1 = 7.

Key points: fork() creates a new child process; both parent and child continue execution after fork().

  • fork() returns 0 in the child and the child PID in the parent.

  • Each existing process executes fork(), causing exponential growth in the number of processes.

  • Excessive use of fork() can exhaust system resources.

Explore the full course: Up Lt Grade Assistant Teacher 2025