• edited by
46,736 views
94 94 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

7 Answers

Best answer
95 95 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
36 36 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
7 7 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 0 votes
The answer is A.
1 flag:
✌ Low quality (saismrutiranjan18)
0 0 votes
a is most appropriate ans here
0 0 votes
Initial condition : S0=1, S1=0, S2=0,

so process P1 and P2 have to wait and only P0 can execute first.

After completing one iteration of the while loop, 0 is printed once.

Now the semaphore variables are – S0=0, S1=1, S2=1

In the minimalist case – P1 and P2 both will be incrementing S0 in one go, so after both P1 and P2 complete execution,

S0=1, S1=0, S2=0

now again P0 will be executed and print 0.

After that it will set S0=0.

So in the worst case S0 will be printed twice.

S0 willbe printed atleast twice.
Answer:
Position:
Show:

Related questions

79 79 votes
10 answers 10 answers
31.3k
31.3k views
go_editor asked Sep 29, 2014
31,322 views
Consider the methods used by processes $P1$ and $P2$ for accessing their critical sections whenever needed, as given below. The initial values of shared boolean variables...
222 222 votes
10 answers 10 answers
44.4k
44.4k views
go_editor asked Sep 30, 2014
44,396 views
A system has $n$ resources $R_0, \dots,R_{n-1}$, and $k$ processes $P_0, \dots, P_{k-1}$. The implementation of the resource request logic of each process $P_i$ is as fol...
37 37 votes
3 answers 3 answers
18.7k
18.7k views
go_editor asked Sep 29, 2014
18,702 views
A system uses FIFO policy for system replacement. It has $4$ page frames with no pages loaded to begin with. The system first accesses $100$ distinct pages in some order ...
97 97 votes
10 answers 10 answers
40.9k
40.9k views
go_editor asked Apr 21, 2016
40,870 views
A computer system has an $L1$ cache, an $L2$ cache, and a main memory unit connected as shown below. The block size in $L1$ cache is $4$ words. The block size in $L2$ cac...