edited by
6,716 views
30 votes
30 votes

Suppose we want to synchronize two concurrent processes $P$ and $Q$ using binary semaphores $S$ and $T$. The code for the processes $P$ and $Q$ is shown below.

Process P: Process Q:
while(1){
W:
    print '0';
    print '0';
X:
}
while(1){
Y:
    print '1';
    print '1';
Z:
}

 

Synchronization statements can be inserted only at points $W, X, Y,$ and $Z$

Which of the following will always lead to an output staring with $\text{‘}001100110011\text{’}$?

  1. $P(S)$ at $W, V(S)$ at $X, P(T)$ at $Y, V(T)$ at $Z, S$ and $T$ initially $1$

  2. $P(S)$ at $W, V(T)$ at $X, P(T)$ at $Y, V(S)$ at $Z, S$ initially $1,$ and $T$ initially $0$

  3. $P(S)$ at $W, V(T)$ at $X, P(T)$ at $Y, V(S)$ at $Z, S$ and $T$ initially $1$

  4. $P(S)$ at $W, V(S)$ at $X, P(T)$ at $Y, V(T)$ at $Z, S$ initially $1$ , and $T$ initially $0$

edited by

3 Answers

Best answer
36 votes
36 votes

To get pattern $001100110011$

Process P should be executed first followed by Process Q.

So, at Process $P$ :  $\mathbf{W}$ $P(S)$   $\mathbf{X}$  $V(T)$ 

And at Process $Q$ : $\mathbf{Y}$ $P(T)$    $\mathbf{Z}$ $V(S)$

With $\mathbf{S=1}$ and  $\mathbf{T=0}$ initially ( only $\mathbf{P}$ has to be run first then only $\mathbf{Q}$ is run. Both processes run on alternate way starting with $\mathbf{P}$)

So, answer is (B).

edited by
6 votes
6 votes
starting with  001100110011 means alternative sequence of process P and Q..
Process P should start execution so at W, P(s) where S=1..
to get alternate sequence X and Y are operation on same semaphore i.e. T.

option B or C.
bt process Q shouldn't start execution before process P ..
means Initial value T=0

W : P(s)         X : V(T)           Y : P(S)             Z : V(S)
S =  1    T = 0
3 votes
3 votes
Answer is B)

It cant be C) because If T = 1 then P(T) at Y for process Q can be executed before process P by decrementing T by 1 and print 11... which is not required.
edited by
Answer:

Related questions

55 votes
55 votes
7 answers
1
go_editor asked Apr 24, 2016
14,694 views
Suppose we want to synchronize two concurrent processes $P$ and $Q$ using binary semaphores $S$ and $T$. The code for the processes $P$ and $Q$ is shown below.$$\begin{ar...