Consider the following program snippet. Assume that the program compiles and…

2026

Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always successful in creating a process.

image.png

The total number of times that the printf statement gets executed is ________. (answer in integer)

Attempted by 10 students.

Show answer & explanation

Correct answer: 4

The correct answer is 4. Trace: 1. P0 (i=0): Forks. Parent (P0) breaks, prints (1). Child (C1) continues. 2. C1 (i=1): Forks. Parent (C1) breaks, prints (2). Child (C2) continues. 3. C2 (i=2): Forks. Parent (C2) breaks, prints (3). Child (C3) continues. 4. C3 (i=3): Loop condition (3 < 3) is false. Loop terminates. C3 prints (4). Total executions: 4.

Explore the full course: Gate Guidance By Sanchit Sir