# GATE - 2008 | OS | The following is a code with two threads, producer and consumer

## Question:

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

## Solution:

### Option (D) is Correct.

(A) Deadlock cannot happen as both producer and consumer are operating on different semaphores (No hold and wait).

(B) No starvation happen because there is alteration between Producer and Consumer.

(C) No items is lost.

Thank You