Skip to main content

GATE CSE 2026Operating System › Process Concepts, Threads & System CallsVerified

GATE CSE 2026 PYQ: Consider the following program snippet. Assume that the program compiles and runs successfully. Further,…

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.

  1. int main () {
  2. int i;
  3. for (i = 0; i < 3; i++) {
  4. if (fork() == 0) {
  5. continue;
  6. }
  7. break;
  8. }
  9. printf("Hello!");
  10. return 0;
  11. }

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

    Answer

    4

    Approach

    Trace every process created by the three fork() calls. In each iteration, the process that receives a nonzero return value from fork() takes the break and goes to printf, while the process that receives 0 takes continue and proceeds to the next loop iteration. Each created process eventually reaches printf exactly once.

    The loop creates one child process per iteration. The parent process breaks out and prints, while the child continues to the next iteration. Thus each of the 3 fork() calls produces exactly one additional process that prints, plus the original process prints once, giving 4 total printf executions.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.