The following program consists of 3 concurrent processes and 3 binary…
2010
The following program consists of 3 concurrent processes and 3 binary semaphores. The semaphores are initialized as S0 = 1, S1 = 0, S2 = 0.

How many times will process P0 print ‘0’?
- A.
At least twice
- B.
Exactly twice
- C.
Exactly thrice
- D.
Exactly once
Attempted by 189 students.
Show answer & explanation
Correct answer: A
Answer: At least twice — explanation follows.
Initial state: S0 = 1, S1 = 0, S2 = 0. P0 can immediately perform Wait(S0), print '0', then Release(S1) and Release(S2).
After P0's first print, S1 and S2 become available (set to 1). This enables P1 and P2; at least one of them must run next under any fair scheduler.
When either P1 or P2 runs, it executes Release(S0), which makes S0 available again and thus allows P0 to perform Wait(S0) and print a second time.
Therefore P0 is guaranteed to print at least twice. Additional prints are possible but not guaranteed; the exact number beyond two depends on scheduling.
Example schedules (to illustrate possibility vs guarantee):
Schedule that yields exactly two prints: P0 runs (prints), then P1 runs (releases S0), then P0 runs again (prints), and the system stops or scheduler never schedules further releases.
Schedule that yields more prints: P0 runs (prints), P1 runs (releases S0), P0 runs (prints), P2 runs (releases S0), P0 runs again (prints), and so on — showing additional prints are possible.
A video solution is available for this question — log in and enroll to watch it.