Skip to content
Official PYQ · 2003

GATE CS 2003 Q80 — Concurrency and Synchronization

Explain with AI

Suppose we want to synchronize two concurrent processes PP and QQ using binary semaphores SS and TT. The code for the processes PP and QQ 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,W, X, Y, and ZZ

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

Choose one option
View worked solution

Answer: B

To obtain the output sequence 001100110011..., we need strict alternation between Process PP (which prints 00) and Process QQ (which prints 11), starting with Process PP.

Analyzing Option B:

  • Initial State: S=1S = 1, T=0T = 0.
  • Step 1: Process PP can proceed because S=1S = 1.
    • At WW, P(S)P(S) decrements SS to 00.
    • It prints 00.
    • At XX, V(T)V(T) increments TT to 11.
  • Step 2: Now S=0S = 0 and T=1T = 1.
    • If PP tries to run again, it blocks at WW because S=0S = 0.
    • Process QQ can now proceed because T=1T = 1.
    • At YY, P(T)P(T) decrements TT to 00.
    • It prints 11.
    • At ZZ, V(S)V(S) increments SS to 11.
  • Step 3: Now S=1S = 1 and T=0T = 0.
    • If QQ tries to run again, it blocks at YY because T=0T = 0.
    • Process PP can run again.

This creates a strict alternating sequence: P→Q→P→Q…P \rightarrow Q \rightarrow P \rightarrow Q \dots, which guarantees the output 001100110011....

Eliminating Other Options:

  • Option A: Since SS and TT are independent and both initialized to 11, PP can run repeatedly without letting QQ run, or vice versa, leading to outputs like 000000....
  • Option C: Since both SS and TT are initially 11, Process QQ could execute first, producing an output starting with 11... instead of 00....
  • Option D: Process PP only signals itself (V(S)V(S) at XX) and Process QQ only signals itself (V(T)V(T) at ZZ). Since TT starts at 00, QQ will block forever, and PP will run infinitely, producing 000000....

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions