edited by
568 views
5 votes
5 votes

The following program consists of $3$ concurrent processes and $3$ binary semaphores. The semaphores are initialized as $S_0=0, S_1=1$ and $S_2=1.$
$$\begin{array}{|l|l|l|}\hline \text{Process $P_0$} & \text{Process $P_1$} & \text{Process $P_2$}
\\\hline \text{while (true) \{} & \text{wait ($S_1$);} & \text{wait ($S_2$);}
\\ \quad\text{wait ($S_0$);} & \text{release ($S_0$);} & \text{release ($S_0$);}
\\ \quad\text{print ‘1';} & &
\\ \quad\text{release ($S_1$);} & &
\\ \quad\text{release ($S_2$);} & &
\\ \} & &
\\\hline \end{array}$$
How many times will process $P_0$ print '$1$'?

  1. At least twice
  2. Exactly once
  3. Exactly twice
  4. None of the above
edited by

1 Answer

Best answer
8 votes
8 votes
Initially $S_0$ is locked and it can be unlocked by either $P_1$ or $P_2.$ Say if $P_1$ proceeds first and then $P_2$ proceeds. In this case $S_0$ will be unlocked and process $P_0$ will print $’1’$ once. If process $P_0$ does wait(S0) before process $P_2$ does release $S_0$ it would have printed $1$ twice. So, correct answer here is at least $’1’$. Option D.
selected by
Answer:

Related questions

3 votes
3 votes
1 answer
2