Open In App

GATE | CS 2022 | Question 19

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. 



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

(A)



S1 = 1; S2 = 1; S3 = 1

(B)

S1 = 1; S2 = 1; S3 = 0

(C)

S1 = 1; S2 = 0; S3 = 0

(D)

S1 = 0; S2 = 1; S3 = 1

Answer: (C)
Explanation:

Initially if S1 = 1, S2 = 0, S3 = 0 ,

Process T2 can successfully execute wait(S1); while T1 and T3 remain stuck at wait(S3); and wait(S2); respectively.

After process T2 prints B it executes signal(S3), and gets stuck at wait(S1);

( B gets printed in this process.)

After this Process T1 can successfully execute wait(S3); and then it executes print(“C”);, after which it executes signal(S2); and then gets stuck at wait(S3);

(C gets printed in this process.)

After this Process T3 can successfully execute wait(S2); and then it executes print(“A”);, after which it executes signal(S1); and then gets stuck at wait(S2);

(A gets printed in this process.)

After this Process T2 can execute wait(S1); successfully.

The process thus keeps repeating and the pattern printed is BCABCABCA…

So, option C is the correct answer. 

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :
Uncategorized