GATE | Gate IT 2008 | Question 51

The following is a code with two threads, producer and consumer, that can run in parallel. Further, S and Q are binary semaphores equipped with the standard P and V operations.

semaphore S = 1, Q = 0;
integer x;

producer:                            consumer:
while (true) do                    while (true) do
P(S);                                      P(Q);
x = produce ();                    consume (x);
V(Q);                                     V(S);
done                                       done

Which of the following is TRUE about the program above?
(A) The process can deadlock
(B) One of the threads can starve
(C) Some of the items produced by the producer may be lost
(D) Values generated and stored in ‘x’ by the producer will always be consumed before the producer can generate a new value

Answer: (D)

Explanation: A semaphore is hardware or a software tag variable whose value indicates the status of a common resource. Its purpose is to lock the resource being used. A process which needs the resource will check the semaphore for determining the status of the resource followed by the decision for proceeding. In multitasking operating systems, the activities are synchronized by using the semaphore techniques.
wait and signal are defined on the semaphore. Entry to the critical section is controlled by the wait operation and exit from a critical region is taken care by signal operation. The wait, signal operations are also called P and V operations. The manipulation of semaphore (S) takes place as following:

1. The wait command P(S) decrements the semaphore value by 1. If the resulting value becomes negative then P command is delayed until the condition is satisfied.
2. The V(S) i.e. signals operation increments the semaphore value by 1.


Consumer can consume only once the producer has produced the item, and producer can produce(except the first time) only once the consumer has consumed the item.

Let’s explain the working of this code.
It is mentioned that Producer and Consumer execute parallely.


st1 – S value is 1, P(S) on S makes it 0 and
st2 – and then x item is produced.
st3 – Q value is 0. V(Q) on Q makes it 1.

this being an infinite while loop should infinitely iterate. In the next iteration of while loop, S is already 0 ,further P(S) on 0 sends P to blocked list of S. So producer is blocked.


P(Q) on Q makes Q =0 and then consumes the item.
V(S) on S,
now instead of changing the value of S to 1,consumer wakes up the blocked process on Q ‘s queue. Hence process P is awaken. P resumes from st2,since it was blocked at statement 1. So P now produces the next item. So consumer consumes an item before producer produces the next item.
Correct Option is (D).

Choice of this question:

A) deadlock cannot happen has both producer and consumer are operating on different semaphores (no hold and wait )
B) No starvation happen because there is alteration between P and Consumer Which also makes them have bounded waiting.
(C) Some of the items produced by the producer may be lost but it can’t.


This solution is contributed by Nitika Bansal

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.