Producer: consumer: while (true) do while (true) do $1$ $P(S)$; $1$ $P(Q)$; $2$ $x =$ produce $()$; $2$ consume $(x)$; $3$ $V(Q)$; $3$ $V(S)$; done done
Lets explain the working of this code.
It is mentioned that $P$ and $C$ execute parallely.
$P:1 2 3$
- $S$ value is $1$, down on $1$ makes it $0$. Enters the statement $2$.
- Item produced.
- Up on $Q$ is done (Since the queue of $Q$ is empty, value of $Q$ up to $1$).
This being an infinite while loop should infinitely iterate.
In the next iteration of while loop $st 1$ is executed.
But $S$ is already $0$, further down on $0$ sends $P$ to blocked list of $S.P$ is blocked.
$C$ Consumer is scheduled.
Down on $Q.$ value makes $Q.$value$=0$;
Enters the statement $2$, consumes the item.
Up on $S$,now instead of changing the value of $S$. value to $1$, wakes up the blocked process on Q 's queue.Hence process P is awaken. $P$ resumes from statement $2$, 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.
(D) Answer
(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.