Consider two processes P1 and P2accessing the shared variables Xand Y protected by two binary semaphores SX and SYrespectively.

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

P1: P2:

While true do {



X = X + 1;

Y = Y -1;


V(SY);      }

While true do {



Y = Y + 1;

X = Y -1;



In order to avoid deadlock, the correct operators at L1,L2, L3and L4 are respectively.

  1. P(SY),P(SX);P(SX),P(SY)

  2. P(SX),P(SY);P(SY),P(SX)

  3. P(SX),P(SX);P(SY),P(SY)

  4. P(SX),P(SY);P(SX),P(SY)

shivani @shivani1234
27 May 2017 01:44 am
  • P(SX),P(SY);P(SX),P(SY), why so? think of when deadlock happens.. , deadlock happens when two process fight for resources which is held by other process ex. p1 has r1 and requesting for r2 and p2 has r2 and requesting for r1 in that case deadlock is there , because none is going to back off and there will be deadlock.
  • similar cond. here if you analyse (d ) option then u will realise that process which puts down on (Sx)semaphore will only be able to put down on (Sy) semaphore, because of same ordering only one out of two process in C.S. at a time.