Discussion
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.
| T1 | T2 | T3 |
|---|---|---|
| while(true){ wait(S3); print("C"); signal(S2); } | while(true){ wait(S1); print("B"); signal(S3); } | while(true){ wait(S2); print("A"); signal(S1); } |
Which initialization of the semaphores would print the sequence BCABCABCA....?
Answer: C
The initialization is \(S_{1} = 1;\ S_{2} = 0;\ S_{3} = 0\).
The sequence must start with B, so only T2, which waits on \(S_{1}\), may run first: \(S_{1} = 1\) and the others 0. Then T2 prints B and signals \(S_{3}\), which lets only T1 print C; T1 signals \(S_{2}\), which lets only T3 print A; T3 signals \(S_{1}\) and the cycle repeats. At every moment exactly one semaphore is 1, so the order BCA is forced.
In the other options more than one semaphore starts at 1. With \(S_{2} = 1\), T3 could print A first; with \(S_{3} = 1\), T1 could print C first; and option D, with \(S_{1} = 0\), cannot print B first at all.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development