GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 48

Common semaphore acquisition order

Operating Systems · Deadlocks · Full GATE CSE 2004 paper

GATE 2004, Q48 2 marks · MCQ
Report issue

Deadlocks

Question 48, GATE 2004: Shared X,Y are protected by binary semaphores Sx,Sy, initially 1. P waits/decrements and V signals/increments. Fill L1,L2,L3,L4 to avoid deadlock:
P1: while true {
  L1; L2; X=X+1; Y=Y-1; V(Sx); V(Sy);
}
P2: while true {
  L3; L4; Y=Y+1; X=X-1; V(Sy); V(Sx);
}
GateForum prints X=Y−1 in P2; that arithmetic variant does not change the locking question.
Options:
(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)
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Both processes must acquire both Sx and Sy, exactly once, in the same global order. Option D supplies P(Sx),P(Sy) to both processes.

2. A process that has acquired Sx prevents its competitor from holding Sy while it waits for Sx, so the circular-wait condition is impossible. Options A/B use opposite acquisition orders and admit deadlock; C acquires the same binary semaphore twice and can block itself.

3. Different release orders do not invalidate the common acquisition order.

4. Teaching check: Construct an opposite-order deadlock: P1 holds Sx and waits for Sy while P2 holds Sy and waits for Sx. Requiring the same acquisition order eliminates this cycle. A release-order difference is harmless here because neither process waits while executing V.

Original paper, page 7