Exams › GATE › Technical
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....?
- S1 = 1; S2 = 1; S3 = 1
- S1 = 1; S2 = 1; S3 = 0
- S1 = 1; S2 = 0; S3 = 0
- S1 = 0; S2 = 1; S3 = 1
Correct answer: S1 = 1; S2 = 0; S3 = 0
Solution
The correct initialization S1 = 1, S2 = 0, S3 = 0 allows T1 to execute first, producing 'C' and signaling T2. T2 can then execute and print 'B', signaling T3, which can subsequently print 'A'. This cycle continues, producing the desired sequence BCABCABCA...
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →