Question:

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. 

Show Hint

In synchronization problems involving semaphores, the key is to initialize the semaphores in such a way that allows the threads to execute in the correct order while preventing race conditions. Here, the semaphores control which thread runs next by using wait and signal operations.
Updated On: Jan 30, 2026
  • S1 = 1; S2 = 1; S3 = 1
  • S1 = 1; S2 = 1; S3 = 0
  • S1 = 1; S2 = 0; S3 = 0
  • S1 = 0; S2 = 1; S3 = 1
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

The given code describes three threads \(T_1\), \(T_2\), and \(T_3\) that are synchronized using three binary semaphores \(S_1\), \(S_2\), and \(S_3\). The goal is to print the sequence BCABCABC... repeatedly.
Each thread performs a sequence of operations with `wait()` and `signal()` on the semaphores. These operations control the execution order and synchronization of the threads.
- Thread \(T_1\) waits for semaphore \(S_3\), prints "C", and then signals semaphore \(S_2\).
- Thread \(T_2\) waits for semaphore \(S_1\), prints "B", and then signals semaphore \(S_3\).
- Thread \(T_3\) waits for semaphore \(S_2\), prints "A", and then signals semaphore \(S_1\).


The sequence that needs to be printed is BCABCABC.... To achieve this, we need to ensure that the threads are executed in the correct order. The order of execution is controlled by the semaphores.

- Initialization (Option C): \(S_1 = 1\), \(S_2 = 0\), \(S_3 = 0\) works as follows:
- Initially, \(S_1\) is 1, allowing \(T_3\) to run and print "A".
- Then \(T_2\) runs, as \(S_1\) is signaled, and prints "B".
- \(T_1\) waits for \(S_3\) (which is 0 initially) but will be allowed to run after \(T_2\) signals \(S_3\) and prints "C".
- This cycle continues, ensuring the sequence BCABCABC... is followed.

Thus, the correct initialization of semaphores to achieve the desired sequence is \(S_1 = 1\), \(S_2 = 0\), and \(S_3 = 0\), corresponding to Option (C).
Was this answer helpful?
0
0

Top Questions on Synchronization