The GATE Grind

GATE 2022 CS – Question 19

Operating System · Concurrency and Synchronization · 1 mark · Multiple choice

Consider the following threads, T1, T2, and T3 executing on a single processor, synchronized using three binary semaphore variables, S1, S2, and S3, operated upon using standard wait() and signal(). The threads can be context switched in any order and at any time.

T1: while(true){ wait(S3); print("C"); signal(S2); }
T2: while(true){ wait(S1); print("B"); signal(S3); }
T3: while(true){ wait(S2); print("A"); signal(S1); }

Which initialization of the semaphores would print the sequence BCABCABCA….?

  1. S1 = 1; S2 = 1; S3 = 1
  2. S1 = 1; S2 = 1; S3 = 0
  3. S1 = 1; S2 = 0; S3 = 0
  4. S1 = 0; S2 = 1; S3 = 1

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (C) S1 = 1; S2 = 0; S3 = 0

Explanation

B is printed first by T2, which needs S1=1. T2 signals S3 so T1 prints C, then T1 signals S2 so T3 prints A, then T3 signals S1. Only S1 must start at 1, with S2=S3=0.