GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Choose semaphore acquisition orders that prevent circular wait

2 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Both supply concurrent processes that hold several binary locks until their protected work finishes and ask for a deadlock-free acquisition order. Build the lock-order constraints for each option and reject self-acquisition or a cycle; a consistent order prevents circular wait.

1 mark · MCQ
Report issue

Deadlock-free semaphore acquisition

Deadlocks

Question 16, GATE 2013: Binary semaphores a, b, c, d start at 1. X acquires a, b, c; Y acquires b, c, d; Z acquires c, d, a, then each releases its three locks after work. Which acquisition ordering is deadlock-free? A X:abc, Y:bcd, Z:cda; B X:bac, Y:bcd, Z:acd; C X:bac, Y:cbd, Z:acd; D X:abc, Y:cbd, Z:cda.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Option B respects a single global order b<a<c<d in every process: X takes b, a, c; Y takes b, c, d; Z takes a, c, d. A circular wait would require strictly increasing lock ranks around a cycle, which is impossible. The other choices admit contradictory acquisition orders and circular-wait executions. Thus B.

Assign ranks b=1, a=2, c=3, d=4. In B every process acquires strictly increasing ranks, so a process holding one lock can wait only for a higher-ranked lock. A circular chain would require rank to increase all the way back to its starting value, which is impossible. For contrast, A permits X holding a while Z holds c, d and waits for a, with X eventually waiting for c. C permits X holding b and Y holding c, each waiting for the other. D permits X holding a, b and Z holding c, d, with X waiting for c and Z for a. These are concrete circular waits, not merely inconvenient orderings.

Original paper, page 4 · Answer key

2 marks · MCQ
Report issue

Common semaphore acquisition order

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