Skip to content
Official PYQ · 2026

GATE CS 2026 Set 1 Q53 — System Calls

Explain with AI

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)

View worked solution

Answer: 4 : 4

Let us trace the execution of the program step-by-step. Let the initial process be P0P_0.

  • Iteration i=0i = 0: P0P_0 calls fork(), creating a child process P1P_1.
    • For the parent P0P_0, fork() returns non-zero. It executes the break statement, exits the loop, and prints "Hello!".
    • For the child P1P_1, fork() returns 00. It executes continue and proceeds to the next iteration (i=1i = 1).
  • Iteration i=1i = 1: P1P_1 calls fork(), creating a child process P2P_2.
    • For the parent P1P_1, fork() returns non-zero. It executes break, exits the loop, and prints "Hello!".
    • For the child P2P_2, fork() returns 00. It executes continue and proceeds to the next iteration (i=2i = 2).
  • Iteration i=2i = 2: P2P_2 calls fork(), creating a child process P3P_3.
    • For the parent P2P_2, fork() returns non-zero. It executes break, exits the loop, and prints "Hello!".
    • For the child P3P_3, fork() returns 00. It executes continue. The loop variable ii is incremented to 33, making the loop condition i<3i < 3 false. P3P_3 exits the loop and prints "Hello!".

Thus, exactly 44 processes (P0,P1,P2,P_0, P_1, P_2, and P3P_3) exit the loop and execute the printf statement once each.

Therefore, the total number of times the printf statement gets executed is 4.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore System Calls questions