Question:

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.
int main () {
int i;
for (i = 0; i < 3; i++){
if (fork() == 0){
continue;
}
break;
}
printf("Hello!");
return 0;
}
The total number of times that the printf statement gets executed is ________.
(answer in integer)

Show Hint

At each fork(), the parent (nonzero return) always breaks out and prints immediately, while only the child (0 return) continues the loop and can fork again. So the forks form a straight chain, not a tree: 3 fork() calls give 3 children plus the original process = 4 processes, each printing once.
Updated On: Jul 7, 2026
Show Solution
collegedunia
Verified By Collegedunia

Correct Answer: 4

Solution and Explanation

Step 1: Recall the behaviour of fork()

The system call fork() creates an exact copy of the calling process. After fork() returns, both the parent and the child resume execution from the very next statement. The return value is what distinguishes them: the child receives \(0\), while the parent receives the child's positive process ID.

The loop under study is:

for (i = 0; i < 3; i++) { if (fork() == 0) { continue; } break; }

So whichever process gets a nonzero return (a parent) immediately executes break and leaves the loop. Whichever process gets \(0\) (a child) executes continue, which increments i and re-checks the loop condition.

Step 2: Trace i = 0 (original process P0)

P0 executes fork(), creating a child C1.

  • P0 (parent) gets a nonzero return, so the if-condition is false, and P0 hits break. P0 leaves the loop and falls through to printf(\text{Hello!}).
  • C1 (child) gets \(0\), so it executes continue. i becomes 1, and since \(1 < 3\), the loop body runs again, but now only inside C1.
Step 3: Trace i = 1 (inside C1 only)

C1 executes fork(), creating a child C2.

  • C1 (parent) gets a nonzero return, breaks out of the loop, and executes printf(\text{Hello!}).
  • C2 (child) gets \(0\), executes continue. i becomes 2, and since \(2 < 3\), the loop runs again, now only inside C2.
Step 4: Trace i = 2 (inside C2 only)

C2 executes fork(), creating a child C3.

  • C2 (parent) gets a nonzero return, breaks, and executes printf(\text{Hello!}).
  • C3 (child) gets \(0\), executes continue. i becomes 3, and now \(3 < 3\) is false, so the for loop terminates naturally (no more fork() calls). C3 falls through and executes printf(\text{Hello!}).
Step 5: Count the surviving processes

The processes that actually reach the printf() statement are P0, C1, C2 and C3 -- each one exactly once, because a process either breaks right after a fork() call or exits the loop when the condition fails, and in either case it goes straight to printf() without ever calling fork() again.

Total fork() calls \(=3\) (by P0, C1, C2), producing \(3\) new children. Together with the original process P0, there are \(3+1=4\) processes in total, and each executes printf() exactly once.

\[ \text{Total printf() executions} = 4 \]

Final Answer: 4

Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions