The GATE Grind

GATE 2014 CS (CS2) – Question 41

Operating System · Concurrency and Synchronization · 2 marks · Multiple choice

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?

  1. Producer can add an item but consumer can never consume it.
  2. Consumer removes no more than one item.
  3. Deadlock occurs if consumer acquires s while the buffer is empty.
  4. n must start at 1 rather than 0 for deadlock-free operation.

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.