Skip to content
GATEiit

GATE CSE 2022 Q19: Semaphore initialisation that prints BCABCA...

Question

Consider the following threads, T1T_1, T2T_2, and T3T_3 executing on a single processor, synchronized using three binary semaphore variables, S1S_1, S2S_2, and S3S_3, operated upon using standard wait() and signal(). The threads can be context switched in any order and at any time.

T1T2T3
while(true){while(true){while(true){
wait(S3); wait(S1); wait(S2);
print("C"); print("B"); print("A");
signal(S2); } signal(S3); } signal(S1); }

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

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 1/3 mark in GATE.
Show answer and explanation

Answer: C (official GATE 2022 final answer key)

Concept used: To force a strict order, exactly the semaphore guarding the first thread to run should start at 1.

First step: B must print first, so only T2T_2 may pass its wait initially: which semaphore does T2T_2 wait on?

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2022 CS question paper and final answer key, organised by IIT Kharagpur. Question paper · Answer key