The following is the 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
int x;
Producer:
while(true) do
P(S);
x=produce();
v(Q);
done for producer.
Consumer:
while(true) do
P(Q);
consume(x);
V(S);
done for the consumer.
which of the following statements is true about the program above?
- The process can deadlock.
- One of the threads can starve.
- Some of the produced items by the producer may lost.
- Values generated and stored in ‘x’ by the producer will always be consumed before the producer can generate a new value.