Open In App

GATE | GATE-CS-2003 | Question 80

Like Article
Like
Save
Share
Report

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 always lead to an output starting with ‘001100110011’ ?

(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 initially 1, and T initially 0
(C) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S and T initially 1
(D) P(S) at W, V(S) at X, P(T) at Y, V(T) at Z, S initially 1, and T initially 0


Answer: (B)

Explanation: P(S) means wait on semaphore ‘S’ and V(S) means signal on semaphore ‘S’.




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


Initially, we assume S = 1 and T = 0 to support mutual exclusion in process P and Q.
Since S = 1, only process P will be executed and wait(S) will decrement the value of S. Therefore, S = 0.
At the same instant, in process Q, value of 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 the function V(T).
While the control is in process Q, semaphore S = 0 and process P would be stuck in while loop and would not execute till the time process Q prints 11 and makes the value of S = 1 by calling the function V(S).
This whole process will repeat to give the output 00 11 00 11 … .

 
Thus, B is the correct choice.

Watch GeeksforGeeks Video Explanation :

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


Quiz of this Question


Last Updated : 27 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads