GATE - 2004 | OS | Consider two processes P1 and P2 accessing the shared variables X

GATE - 2004 | OS | Consider two processes P1 and P2 accessing the shared variables X
Posted on 21-02-2022

GATE - 2004 [Operating System]

Question

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)

Solution:

Option (D) is Correct.

Option D:
P1 : line 1
P2 : line 3 (block require Sx)
P1 : line 2
P2 : line 4 (still in block state)
P1 : execute CS, the push up the value of Sx.
P2 : line 3 line 4 (block require Sy)
P1 : push up Sy
P2 : line 4 get Sy and enter into CS
P2 : start execution.
So option D is Answer.

Thank You