Skip to content
Official PYQ · 2026

GATE CS 2026 Set 2 Q41 — Concurrency and Synchronization

Explain with AI

Consider three processes P1, P2\text{P1, P2}, and P3\text{P3} running identical code, as shown in the pseudocode below. A\text{A} and B\text{B} are two binary semaphores initialized to 11 and 00, respectively. X\text{X} is a shared variable initialized to 00. 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?

Select all correct options
View worked solution

Answer: A;B;C

Let us analyze the execution of the three processes step-by-step with the initial values A=1A = 1, B=0B = 0, and X=0X = 0:

  1. First process (say P1P_1) enters the critical section:
    • Prints *, increments XX to 11.
    • Since X≠2X \neq 2, it skips the if block, signals AA (A=1A=1), and blocks at Wait(B).
  2. Second process (say P2P_2) enters the critical section:
    • Prints *, increments XX to 22.
    • Since X==2X == 2, it prints $, signals BB (B=1B=1), and then signals AA (A=1A=1).
    • At this point, the output prefix is strictly **$.

Since P3P_3 cannot enter the critical section until P2P_2 signals AA (which happens only after printing $</code>), the third <code>*</code> cannot be printed before <code>$. Thus, Option D (***$###) is impossible.

After P2P_2 signals AA and BB:

  • P3P_3 can enter the critical section to print the third *.
  • P1P_1 and P2P_2 can proceed past Wait(B) to print #.
  • P3P_3 can only print its # after it has printed its *. Thus, at most two #s (from P1P_1 and P2P_2) can be printed before the third *.

This allows the third * to appear in any of the following positions relative to the #s:

  • Before any #: **$*### (Option A)
  • After one #: **$#*## (Option B)
  • After two #s: **$##*# (Option C)

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions