GATE CSE 2026 Session 1 Q63: Number of printf calls with fork, continue and break
- Operating System
- 2 marks
- NAT
- No negative marking
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)
Try it: numerical answer: type the value
Show answer and explanation
Answer: 4 (official GATE 2026 final answer key)
Concept used: After fork, the parent (non-zero return) breaks out of the loop while only the child keeps looping.
First step: Trace: each iteration the current process forks once and then exits the loop, while its child carries on; count the processes created plus the original.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2026 Session 1 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Operating System PYQs
- 2026 Session 1 · Q29False statements about deadlocks
- 2026 Session 1 · Q35Minimum resource instances for deadlock freedom (5 processes, max 2)
- 2026 Session 1 · Q59Disk transfer time for random sectors on tracks 5, 12, 7
- 2026 Session 1 · Q64Average FCFS waiting time for alternating A and C processes
- 2026 Session 2 · Q23CPU scheduling algorithm that cannot be preemptive
- 2026 Session 2 · Q51Possible output patterns of three processes using two semaphores