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.
/* Thread T1 */
while(true){
wait(S3);
print("C");
signal(S2);
}
/* Thread T2 */
while(true){
wait(S1);
print("B");
signal(S3);
}
/* Thread T3 */
while(true){
wait(S2);
print("A");
signal(S1);
}
Which initialization of the semaphores would print the sequence BCABCABCA....?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2022 answer key
Solution
Trace the semaphore dependencies to establish the required cyclic print sequence B -> C -> A -> B.
To print the sequence :
- The very first character to be printed must be 'B'. In the code, thread prints 'B' after
wait(S1). Hence, must be enabled initially, requiring . - To ensure that neither 'C' nor 'A' is printed before 'B', threads (which waits on ) and (which waits on ) must be blocked initially. Hence, and .
- Once executes, it prints 'B' and signals , unblocking .
- prints 'C' and signals , unblocking .
- prints 'A' and signals , unblocking to print the next 'B'.
Thus, the unique initial values that strictly enforce the sequence are .
Option (C)
Initializing multiple semaphores to 1, which introduces race conditions allowing 'A' or 'C' to print first.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2022 Computer Science and Information Technology (CS)
- Question
- 19 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.