Consider the solution to the bounded buffer producer/consumer problem by using…
2006
Consider the solution to the bounded buffer producer/consumer problem by using general semaphores S, F, and E. The semaphore S is the mutual exclusion semaphore initialized to 1. The semaphore F corresponds to the number of free slots in the buffer and is initialized to N. The semaphore E corresponds to the number of elements in the buffer and is initialized to 0.

Which of the following interchange operations may result in a deadlock?
Interchanging Wait (F) and Wait (S) in the Producer process
Interchanging Signal (S) and Signal (F) in the Consumer process
- A.
I Only
- B.
II Only
- C.
Neither I nor II
- D.
Both I and II
Attempted by 86 students.
Show answer & explanation
Correct answer: A
Answer: Interchanging Wait(F) and Wait(S) in the Producer process may result in deadlock; interchanging Signal(S) and Signal(F) in the Consumer process does not.
Key insight: do not hold the mutual-exclusion lock while waiting for a resource that only a thread holding that lock can free.
Why swapping the producer's Wait(F) and Wait(S) causes deadlock:
If the producer does Wait(S) first and the buffer is full (F = 0), it holds the mutex S and then blocks on Wait(F).
Consumers need to acquire S to remove an item and perform Signal(F) to free a slot, but they cannot acquire S because the producer is holding it. Neither producer nor consumer can make progress → deadlock.
Why swapping the consumer's Signal(S) and Signal(F) does not cause deadlock:
If the consumer signals the free-slot semaphore before releasing the mutex, a blocked producer may wake and then attempt to acquire the mutex S and block temporarily. However, the consumer still holds S only briefly and will release it immediately after; the producer will then acquire S and continue. There is no permanent circular wait, so no deadlock.
Conclusion: Only interchanging the producer's Wait operations can create a deadlock; interchanging the consumer's Signal operations may cause inefficiency but not deadlock.
A video solution is available for this question — log in and enroll to watch it.