Multiple choice

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 semaphore operators, where P decrements the semaphore value and V increments the semaphore value. The pseudo-codes of P1 and P2 are as follows:

In order to avoid deadlock, the correct operators at L1,L2,L3 and 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)

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Here semaphores are required to obtain mutual exclusion since both access X & Y. So at L1 P (Sx) which means now S wait x = at L2 P (Sy) Sy wait, this prevents process P2 to start access X &Y. V(Sx) & V(Sy) in the end of P1 makes Sx & Sy signal so that at L3 & L4 P(Sx) & P(Sy) can start.