GATE | GATE-CS-2014-(Set-2) | Question 41

Consider the procedure below for the Producer-Consumer problem which uses semaphores:

GATECS2014Q30

Which one of the following is TRUE?
(A) The producer will be able to add an item to the buffer, but the consumer can never consume it.
(B) The consumer will remove no more than one item from the buffer.
(C) Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty.
(D) The starting value for the semaphore n must be 1 and not 0 for deadlock-free operation.


Answer: (C)

Explanation: Initially, there is no element in the buffer.
Semaphore s=1 and semaphore n=0.
We assume that initially control goes to the consumer when buffer is empty.

semWait(s) decrements the value of semaphore ‘s’ . Now,  s = 0 and semWait(n) decrements the value of semaphore ‘n’.
Since, the value of semaphore ‘n’ becomes less than 0 , the control stucks in while loop of function semWait() and a deadlock arises.
Thus, deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty.

Please comment below if you find anything wrong in the above post.

Quiz of this Question



My Personal Notes arrow_drop_up