Consider the following threads, and executing on a single processor, synchronized using three binary semaphore variables, and operated upon using standard and The threads can be context switched in any order and at any time.
Which initialization of the semaphores would print the sequence
View worked solution
Answer: C
To print the sequence , the first character printed must be B, followed by C, and then A.
- First print (B): Thread prints "B". For to execute first, its initial
wait(S_1)must succeed. Thus, we must initialize . - Blocking other threads initially:
- (which prints "C") must not run first, so
wait(S_3)must block. Thus, . - (which prints "A") must not run first, so
wait(S_2)must block. Thus, .
- (which prints "C") must not run first, so
Let us trace the execution with the initialization :
- executes:
wait(S_1)succeeds prints "B"signal(S_3)sets . - executes:
wait(S_3)succeeds prints "C"signal(S_2)sets . - executes:
wait(S_2)succeeds prints "A"signal(S_1)sets .
This cycle repeats indefinitely, producing the desired sequence .
Free practice · Your answers on this page are not saved.
