Related Articles
GATE | GATE-CS-2003 | Question 81
• Difficulty Level : Hard
• Last Updated : 30 Oct, 2017

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:
while (1) {
W:
print '0';
print '0';
X:
}

Process Q:
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 ensure that the output string never contains a substring of the form 01^n0 or 10^n1 where n is odd?
(A) P(S) at W, V(S) at X, P(T) at Y, V(T) at Z, S and T initially 1
(B) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S and T initially 1
(C) P(S) at W, V(S) at X, P(S) at Y, V(S) at Z, S initially 1
(D) V(S) at W, V(T) at X, P(S) at Y, P(T) at Z, S and T initially 1

Explanation: P(S) means wait on semaphore ’S’ and V(S) means signal on semaphore ‘S’. The definition of these functions are :

```Wait(S)  {
while (i <= 0) ;
S-- ;
}

Signal(S)  {
S++ ;
}```

Initially S = 1 and T = 0 to support mutual exclusion in process ‘P’ and ‘Q’.

Since, S = 1 , process ‘P’ will be executed and function Wait(S) will decrement the value of ‘S’. So, S = 0 now.

Simultaneously, in process ‘Q’ , T = 0 . Therefore, in process ‘Q’ control will be stuck in while loop till the time process ‘P’ prints ‘00’ and increments the value of ‘T’ by calling function V(T).

While the control is in process ‘Q’, S = 0 and process ‘P’ will be stuck in while loop. Process ‘P’ will not execute till the time process ‘Q’ prints ‘11’ and makes S = 1 by calling function V(S).

Thus, process ‘P’ and ‘Q’ will keep on repeating to give the output ‘00110011 …… ‘ .

Please comment below if you find anything wrong in the above post.

Quiz of this Question

My Personal Notes arrow_drop_up