Open In App

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

Like Article
Like
Save
Share
Report

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.

Watch GeeksforGeeks Video Explanation :

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

Quiz of this Question


Last Updated : 28 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads