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.
Producer Process:
Produce an item;
Wait(F);
Wait(S);
Append the item to the buffer;
Signal(S);
Signal(E);
Consumer Process:
Wait(E);
Wait(S);
Remove an item from the buffer;
Signal(S);
Signal(F);
Consume the item;
Which of the following interchange operations in a process may result in a deadlock?
I. Interchanging Wait(F) and Wait(S) in the Producer process
II. 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)
GATE 2006 · Operating System · Process Synchronization · medium
Answer: Only interchange (i) causes a deadlock. Answer: A. (i) only
Analyze interchange I: swap Wait(F) and Wait(S) in Producer: Original Producer: Wait(F) then Wait(S). Modified: Wait(S) then Wait(F). Scenario: Suppose buffer is completely full (F=0). Producer executes Wait(S) — succeeds (S becomes 0, Producer holds mutex). Producer then executes Wait(F) — F=0, so Producer BLOCKS while holding S. Now Consumer tries to run: Wait(E) succeeds (E>0), then Wait(S) — but S=0 (held by Producer) so Consumer BLOCKS. Neither can proceed: Producer waits for Consumer to free a slot (needs S), Consumer waits for S (held by Producer). DEADLOCK.
Analyze interchange II: swap Signal(S) and Signal(F) in Consumer: Original Consumer: Signal(S) then Signal(F). Modified: Signal(F) then Signal(S). In both orderings, the Consumer releases both semaphores. The order of releasing does not cause a deadlock — the Producer is not holding any resource that it needs to give up to receive S or F. Both signals are eventually executed, so no circular wait occurs.
Conclusion: Only interchange I (swapping Wait(F) and Wait(S) in the Producer) can cause a deadlock. Interchange II (swapping Signal(S) and Signal(F) in the Consumer) does not cause a deadlock.