GateAI
Processes, Threads and IPC

GATE CSE 2026 Set 1 · Q63

NAT2 marksMedium

Time

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)

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 4

Processes, Threads and IPC

Let us trace the execution of the loop step by step:

  1. Iteration i=0i = 0:

    • The original parent process calls fork().

    • In the parent, fork() returns a non-zero value (the child's PID). Thus, fork() == 0 is false, the parent executes break;, exits the loop, and reaches printf("Hello!");.

    • In the child (C1C_1), fork() returns 0. The condition fork() == 0 is true, so it executes continue; and advances to i=1i = 1.

  2. Iteration i=1i = 1:

    • Process C1C_1 calls fork().

    • In the parent (C1C_1), fork() returns a non-zero value, it executes break;, exits the loop, and reaches printf("Hello!");.

    • In the new child (C2C_2), fork() returns 0, executes continue;, and advances to i=2i = 2.

  3. Iteration i=2i = 2:

    • Process C2C_2 calls fork().

    • In the parent (C2C_2), fork() returns a non-zero value, executes break;, exits the loop, and reaches printf("Hello!");.

    • In the new child (C3C_3), fork() returns 0, executes continue;, and increments to i=3i = 3.

  4. Loop Termination:

    • Process C3C_3 checks the loop condition i<3i < 3, which is now false (3<33 < 3 is false). It terminates the loop and executes printf("Hello!");.

Hence, exactly 4 distinct processes (the original parent, C1C_1, C2C_2, and C3C_3) reach the printf statement.

Therefore, printf is executed 4 times.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Processes, Threads and IPC previous year question is Q63 of the GATE CSE 2026 Set 1 paper. Find more GATE CSE PYQs on Processes, Threads and IPC in Operating Systems, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Processes, Threads and IPC PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Processes, Threads and IPC in GATE CSE

Processes, Threads and IPC has 15 GATE CSE PYQs across 13 papers from 2011 to 2026. This is the only 2-mark NAT question among them, and it is the only one in GATE CSE 2026 Set 1.

How GATE tests Processes, Threads and IPC

More from GATE CSE 2026 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2026 Set 1 · Q63 · NAT

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 The total number of times that the printf statement gets executed is … (answer in integer)

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.