Skip to main content

GATE CSE 2022Operating System › Process Synchronization & ConcurrencyVerified

GATE CSE 2022 PYQ: Consider the following threads, T 1 , T 2 , \text{T}_{1}, \text{T}_{2}, T 1 ​ , T 2 ​ , and T 3 \text{T}_{3}…

Consider the following threads, T1,T2,\text{T}_{1}, \text{T}_{2}, and T3\text{T}_{3} executing on a single processor, synchronized using three binary semaphore variables, S1,S2,\text{S}_{1}, \text{S}_{2}, and S3,\text{S}_{3}, operated upon using standard wait()\textsf{wait}() and signal().\textsf{signal}(). The threads can be context switched in any order and at any time.

T1T2T3while(true){while(true){while(true){wait(S3);wait(S1);wait(S2);print(“C”);print(“B”);print(“A”);signal(S2);}signal(S3);}signal(S1);}\begin{array}{|c|c|c|} \hline \text{T}_{1} & \text{T}_{2} & \text{T}_{3} \\\hline \text{while(true)} \{ & \text{while(true)} \{& \text{while(true)} \{ \\ \text{wait(S}_{3}); & \text{wait(S}_{1}); & \text{wait(S}_{2}); \\ \text{print(“C”)}; & \text{print(“B”)}; & \text{print(“A”)}; \\ \text{signal(S}_{2}); \} & \text{signal(S}_{3}); \} & \text{signal(S}_{1}); \} \\\hline \end{array}

Which initialization of the semaphores would print the sequence BCABCABCA…?\text{BCABCABCA} \dots ?

  1. A
    S1=1;S2=1;S3=1\text{S}_{1} = 1; \text{S}_{2} = 1; \text{S}_{3} = 1
  2. B
    S1=1;S2=1;S3=0\text{S}_{1} = 1; \text{S}_{2} = 1; \text{S}_{3} = 0
  3. C
    S1=1;S2=0;S3=0\text{S}_{1} = 1; \text{S}_{2} = 0; \text{S}_{3} = 0
    Correct answer
  4. D
    S1=0;S2=1;S3=1\text{S}_{1} = 0; \text{S}_{2} = 1; \text{S}_{3} = 1

Answer

(C) S 1 = 1 ; S 2 = 0 ; S 3 = 0 \text{S}_{1} = 1; \text{S}_{2} = 0; \text{S}_{3} = 0 S 1 ​ = 1 ; S 2 ​ = 0 ; S 3 ​ = 0

Approach

Determine which thread prints which letter, then trace the wake-up chain induced by the signal() calls. The printed sequence must be BCA repeated, so T2 must go first (it prints B), and T1 and T3 must both be blocked until they are released by their predecessors. The initial semaphore values must therefore allow exactly T2 to enter and block the other two.

The letters must be printed in the cyclic order B, C, A. Since T2 prints B and waits on S1, T2 needs S1 = 1 to start. T1 (prints C, waits on S3) and T3 (prints A, waits on S2) must both be blocked until released by their predecessors, so S3 = 0 and S2 = 0 initially. This gives S1 = 1, S2 = 0, S3 = 0, which yields BCA BCA BCA...

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.