edited by
25,745 views
61 votes
61 votes

The following program consists of $3$ concurrent processes and $3$ binary semaphores. The semaphores are initialized as $S0=1, S1=0$ and $S2=0.$
$$\begin{array}{|l|l|}\hline \text{Process P0}  &  \text{Process P1} & \text{Process P2} \\ \hline  \text{while (true) \{} & \text{wait (S1);} & \text{wait (S2);} \\  \text{    wait (S0);} & \text{release (S0);} & \text{release (S0);} \\  \text{   print ‘0';} & &  \\  \text{   release (S1);} & & \\  \text{   release (S2);} & \text{} & \text{} \\ \text{\}}  &  \text{}   \\\hline \end{array}$$
How many times will process $P0$ print '$0$'?

  1. At least twice
  2. Exactly twice
  3. Exactly thrice
  4. Exactly once
edited by

6 Answers

Best answer
68 votes
68 votes
First $P_0$ will enter the while loop as $S_0$ is $1.$ Now, it releases both $S_1$ and $S_2$ and one of them must execute next. Let that be $P_1.$ Now, $P_0$ will be waiting for $P_1$ to finish. But in the mean time $P_2$ can also start execution. So, there is a chance that before $P_0$ enters the second iteration both $P_1$ and $P_2$ would have done release $(S_0)$ which would make $S_1$ $1$ only (as it is a binary semaphore). So, $P_0$ can do only one more iteration printing $'0'$ two times.

If $P_2$ does release $(S_0)$ only after $P_0$ starts its second iteration, then $P_0$ would do three iterations printing $'0'$ three times.

If the semaphore had $3$ values possible (an integer semaphore and not a binary one), exactly three $'0's$ would have been printed.  

Correct Answer: A, at least twice
selected by
24 votes
24 votes

Option A is True.
Initially P0 will execute because only S0=1. It will print single 0.

Now when S1 and S2 are releases by P0 then any one of them can be executed.

Let us suppose P1 executes and releases S0(Now value of S0 is 1).

Now there are two possibilities either P0 or P2 can execute.

Let us take P2 executes and releases S0, so at the end P0 execute and print 0 (means two 0's) but if P0 executes before P2 then total of 3 0's will print(one at the time of P0 and then P2 which releases S0 so P0 executes again).
So the perfect answer is at least two 0's.


ref@ http://stackoverflow.com/questions/12069305/how-many-times-will-process-p0-print-0

reshown by
6 votes
6 votes
Minimum no. of time 0 printed  is twice when execute in this order (p0 p1 p2 p0)

Maximum no. of time 0 printed is thrice when execute in this order (p0 p1 p0 p2 p0)
reshown by
0 votes
0 votes
The answer is A.
Answer:

Related questions

23 votes
23 votes
2 answers
3
41 votes
41 votes
2 answers
4