GATE 2014 CS (CS2) – Question 41
Producer–consumer uses semaphores n = 0 and s = 1.
producer: repeat forever {
produce(); semWait(s); addToBuffer();
semSignal(s); semSignal(n);
}
consumer: repeat forever {
semWait(s); semWait(n); removeFromBuffer();
semSignal(s); consume();
}
Which is TRUE?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (C) Deadlock occurs if consumer acquires s while the buffer is empty.
Explanation
If the consumer first acquires mutex s while the buffer is empty, it then waits on n=0 without releasing s. The producer cannot acquire s to add an item and signal n. Therefore **C**, this execution deadlocks. Waiting on the item-count semaphore before taking the mutex avoids that circular wait.