Skip to content
Related Articles
GATE | GATE-CS-2004 | Question 48
• Last Updated : 17 Sep, 2014

Consider two processes P1 and P2 accessing the shared variables X and Y protected by two binary semaphores SX and SY respectively, both initialized to 1. P and V denote the usual semaphone operators, where P decrements the semaphore value, and V increments the semaphore value. The pseudo-code of P1 and P2 is as follows :

P1 :

``` While true do {
L1 : ................
L2 : ................
X = X + 1;
Y = Y - 1;
V(SX);
V(SY);
}```

P2 :

``` While true do {
L3 : ................
L4 : ................
Y = Y + 1;
X = Y - 1;
V(SY);
V(SX);
}```

In order to avoid deadlock, the correct operators at L1, L2, L3 and L4 are respectively
(A) P(SY), P(SX); P(SX), P(SY)
(B) P(SX), P(SY); P(SY), P(SX)
(C) P(SX), P(SX); P(SY), P(SY)
(D) P(SX), P(SY); P(SX), P(SY)

Answer: (D)

Explanation:

```Option A: In line L1 ( p(Sy) ) i.e. process p1 wants lock on Sy that is
held by process p2 and line L3 (p(Sx)) p2 wants lock on Sx which held by p1.
So here circular and wait condition exist means deadlock.
```
```Option B : In line L1 ( p(Sx) ) i.e. process p1 wants lock on Sx that is held
by process p2 and line L3 (p(Sy)) p2 wants lock on Sx which held by p1. So here
circular and wait condition exist means deadlock.
```
```Option C: In line L1 ( p(Sx) ) i.e. process p1 wants lock on Sx and line L3 (p(Sy))
p2 wants lock on Sx . But Sx and Sy can’t be released by its processes p1 and p2.
```

Please read the following to learn more about process synchronization and semaphores:
Process Synchronization Set 1

This explanation has been contributed by Dheerendra Singh.

Quiz of this Question

Attention reader! Don’t stop learning now. Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up