Skip to content
Official PYQ · 2004

GATE CS 2004 Q48 — Concurrency and Synchronization

Explain with AI

Consider two processes P1P_1 and P2P_2 accessing the shared variables XX and YY protected by two binary semaphores SXS_X and SYS_Y respectively, both initialized to 1. PP and VV denote the usual semaphore operators, where PP decrements the semaphore value, and VV increments the semaphore value. The pseudo-code of P1P_1 and P2P_2 is as follows:
P_1:P_2:While true do {While true do {L1:……L3:……L2:……L4:……X = X + 1;Y = Y + 1;Y=Y−1;X=Y−1;V(S_X);V(S_Y);V(S_Y);V(S_X);}}\begin{array}{|l|l|}\hline \text{$P\_1$:} & \text{$P\_2$:} \\ \hline \text{While true do \{} & \text{While true do \{} \\ L_1:\ldots\ldots & L_3:\ldots\ldots \\ L_2:\ldots\ldots & L_4:\ldots\ldots \\ \text{X = X + 1;} & \text{Y = Y + 1;} \\ Y=Y-1;&X = Y-1; \\ \text{$V(S\_X);$} & \text{$V(S\_Y);$} \\ \text{$V(S\_Y);$} & \text{$V(S\_X);$} \\\}&\} \\ \hline \end{array}
In order to avoid deadlock, the correct operators at L1L_1, L2L_2, L3L_3 and L4L_4 are respectively.

Choose one option
View worked solution

Answer: D

To prevent deadlock among competing processes, we must eliminate at least one of the four Coffman conditions. The most common way to prevent circular wait is to enforce a strict global ordering on resource acquisition.

Both processes P1P_1 and P2P_2 access the shared variables XX and YY, which are protected by the binary semaphores SXS_X and SYS_Y respectively. Since both processes modify both variables, each process must acquire both semaphores before entering its critical section.

Analyzing the Options:

  • Option A: P1P_1 requests SYS_Y then SXS_X, while P2P_2 requests SXS_X then SYS_Y.
    Deadlock scenario: P1P_1 executes P(SY)P(S_Y) and is preempted. P2P_2 executes P(SX)P(S_X). Now, P1P_1 is blocked waiting for SXS_X (held by P2P_2), and P2P_2 is blocked waiting for SYS_Y (held by P1P_1). This results in a deadlock.
  • Option B: P1P_1 requests SXS_X then SYS_Y, while P2P_2 requests SYS_Y then SXS_X.
    Deadlock scenario: P1P_1 executes P(SX)P(S_X) and is preempted. P2P_2 executes P(SY)P(S_Y). Similar to Option A, this leads to a deadlock.
  • Option C: P1P_1 requests SXS_X twice, and P2P_2 requests SYS_Y twice. This does not protect both shared variables for either process and is incorrect.
  • Option D: Both P1P_1 and P2P_2 request the semaphores in the exact same order: SXS_X first, followed by SYS_Y.
    Why this avoids deadlock: Whichever process successfully executes P(SX)P(S_X) first will proceed to request SYS_Y. The other process will be blocked at the very first step (P(SX)P(S_X)) and cannot hold any resource while waiting. Since no circular wait can form, deadlock is completely avoided.

Thus, the correct operators at L1,L2,L3,L_1, L_2, L_3, and L4L_4 are respectively:
P(SX),P(SY);P(SX),P(SY)P(S_X), P(S_Y); P(S_X), P(S_Y)

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions