Consider the following threads, T1, T2, and T3 executing on a single…

2022

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….?

  1. A.

    S1 = 1; S2 = 1; S3 = 1

  2. B.

    S1 = 1; S2 = 1; S3 = 0

  3. C.

    S1 = 1; S2 = 0; S3 = 0

  4. D.

    S1 = 0; S2 = 1; S3 = 1

Attempted by 199 students.

Show answer & explanation

Correct answer: C

Correct initialization: S1 = 1; S2 = 0; S3 = 0

  • Step 1: Initially only the thread waiting on S1 can run, so it prints B and then signals S3.

  • Step 2: Signaling S3 allows the thread waiting on S3 to run next; it prints C and signals S2.

  • Step 3: Signaling S2 allows the thread waiting on S2 to run; it prints A and signals S1, returning control to the first thread.

  • Result: These three steps form a cycle B → C → A that repeats, producing BCABCABCA….

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir