GateAI
Concurrency and Synchronization

GATE CSE 2026 Set 2 · Q51

MSQ2 marksMedium

Time

Consider three processes P1, P2, and P3 running identical code, as shown in the pseudocode below. A and B are two binary semaphores initialized to 1 and 0, respectively. X is a shared variable initialized to 0. Each line in the pseudocode is executed atomically. Pseudocode of P1, P2, and P3Wait(A); Print(*); X = X+1; If (X == 2) { Print($); Signal(B); } Signal(A); Wait(B); Print(#); Signal(B);Assume that any of the three processes can start to execute first and context switching can happen between these processes at any arbitrary time and in any arbitrary order. Which of the following patterns is/are possible to be generated as an outcome of the execution of these three processes?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: A) **$*###; B) **$#*##; C) **$##*#

Concurrency and Synchronization

Let us trace the execution of the three processes:

  1. First Process (e.g., P1):

    • Executes Wait(A) (AA becomes 0).

    • Prints * (output: *).

    • Increments XX to 1 (X=1X = 1).

    • Condition $X == 2$ is false.

    • Executes Signal(A) (AA becomes 1).

    • Executes Wait(B): Since B=0B = 0, P1 blocks.

  2. Second Process (e.g., P2):

    • Executes Wait(A) (AA becomes 0).

    • Prints * (output: **).

    • Increments XX to 2 (X=2X = 2).

    • Condition $X == 2$ is true, so it executes inside the block:

      • Prints $ (output: **$).

      • Executes Signal(B) (BB becomes 1, unblocking a waiting process).

    • Executes Signal(A) (AA becomes 1).

  3. Remaining Executions:

    • Mutual exclusion on the first block is enforced by semaphore AA. Because the second process prints $ before releasing AA, the third process cannot enter to print the third * before $ is printed. Thus, the prefix `*$` is impossible (ruling out option D**).

    • At this point, the prefix is **$, A=1A = 1, and B=1B = 1.

    • Option A (`$*###):** The third process (P3) executes its first part immediately after P2 releases $A$, printing * before any # is printed. Then all three processes execute Print(#). This yields **$*###`.

    • Option B (`$#*##):** One blocked process (P1 or P2) executes Wait(B), prints #, and signals $B$. Next, P3 executes its first block and prints *. Finally, the remaining two processes print #. This yields **$#*##`.

    • Option C (`$##*#):** Both P1 and P2 complete their execution and each prints #. Then P3 executes, prints *, and proceeds to print #. This yields **$##*#`.

Hence, patterns (A), (B), and (C) are possible.

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 Concurrency and Synchronization previous year question is Q51 of the GATE CSE 2026 Set 2 paper. Find more GATE CSE PYQs on Concurrency and Synchronization 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 Concurrency and Synchronization 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.

Concurrency and Synchronization in GATE CSE

Concurrency and Synchronization has 18 GATE CSE PYQs across 15 papers from 2012 to 2026. 4 of them are 2-mark MSQs like this one, and it is the only one in GATE CSE 2026 Set 2.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2026 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2026 Set 2 · Q51 · MSQ

Consider three processes P1, P2, and P3 running identical code, as shown in the pseudocode below. A and B are two binary semaphores initialized to 1 and 0, respectively. X is a shared variable initialized to 0. Each line in the pseudocode is executed atomically. Pseudocode of P1, P2, and P3 Wait(A); Print(*); X = X+1; If (X == 2) { Print($); Signal(B); } Signal(A); Wait(B); Print(#); Signal(B); Assume that any of the three processes can start to execute first and context switching can happen between these processes at any arbitrary time and in any arbitrary order. Which of the following patterns is/are possible to be generated as an outcome of the execution of these three processes?

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.