GateAI
System Calls

GATE CSE 2024 Set 1 · Q57

NAT2 marksMedium

Time

Consider the following code snippet using the fork() and wait() system calls. Assume that the code compiles and runs correctly, and that the system calls run successfully without any errors.int x = 3; while(x > 0) { fork(); printf("hello"); wait(NULL); x--; }The total number of times the printf statement is executed is ________

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 14

System Calls

Let us trace the process creation and printf executions step-by-step:

  1. Iteration 1 (x=3x = 3):

    • Initially, there is 11 process (P0P_0).

    • P0P_0 executes fork(), creating 11 new child process (P1P_1).

    • There are now 22 active processes.

    • Both processes execute printf("hello")   ⟹  2\implies 2 times.

    • Both decrement xx to 22.

  2. Iteration 2 (x=2x = 2):

    • The 22 existing processes each call fork(), creating 22 new child processes.

    • There are now 2+2=42 + 2 = 4 active processes.

    • All 44 processes execute printf("hello")   ⟹  4\implies 4 times.

    • All processes decrement xx to 11.

  3. Iteration 3 (x=1x = 1):

    • The 44 existing processes each call fork(), creating 44 new child processes.

    • There are now 4+4=84 + 4 = 8 active processes.

    • All 88 processes execute printf("hello")   ⟹  8\implies 8 times.

    • All processes decrement xx to 00.

  4. Iteration 4 (x=0x = 0):

    • The condition x > 0 evaluates to false for all 88 processes, and the loop terminates.

Total number of times printf is executed:

2+4+8=142 + 4 + 8 = 14

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 System Calls previous year question is Q57 of the GATE CSE 2024 Set 1 paper. Find more GATE CSE PYQs on System Calls 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 4 related System Calls 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.

System Calls in GATE CSE

System Calls has 4 GATE CSE PYQs across 4 papers from 2011 to 2024. This is the only 2-mark NAT question among them, and it is the only one in GATE CSE 2024 Set 1.

How GATE tests System Calls

More from GATE CSE 2024 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2024 Set 1 · Q57 · NAT

Consider the following code snippet using the fork() and wait() system calls. Assume that the code compiles and runs correctly, and that the system calls run successfully without any errors. int x = 3; while(x > 0) { fork(); printf("hello"); wait(NULL); x--; } The total number of times the printf statement is executed is …

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.