Semaphore constraints on print order
Synchronization & Concurrency

Answer & solution
Official answer: A, B, C
Givens and goal
Three processes each execute: wait(A); print(*); X=X+1; if X==2 then {print($); signal(B);}; signal(A); wait(B); print(#); signal(B). Binary semaphores start A=1,B=0 and shared X=0. Each line is atomic. Which outputs are possible? A $*###; B **$#*##; C **$##*#; D *$###.
Approach
Find invariant ordering constraints, then exhibit schedules achieving each candidate.
Step-by-step solution
1. A protects the star and increment section. The first entrant prints a star and changes X from 0 to 1. The second prints another star and changes X to 2, then prints $ before releasing A.
2. The third entrant cannot print its star until that release. Therefore the dollar must occur immediately after the second star among these star/dollar events, and ***$### is impossible. Preempting the second process before $ does not help: it still holds A.
3. B initially blocks all hash printing. It is first signaled only after the dollar; each process that prints a hash then signals B for another. Thus no hash can precede $, while each process’s own star must precede its hash.
4. For A, let the first two processes complete their A sections, then let the third complete its A section before allowing any hash sections. For B, after the first two A sections allow one of those processes to print a hash, then allow the third star, then the remaining hashes.
5. For C, let both first and second processes print their hashes after $, delaying the third A section until afterward. The third then prints its star followed by its hash. These realize all A,B,C exactly.
6. Therefore select A,B,C. The invariant and the explicit schedules together establish both impossibility and feasibility; a single simulated scheduling order would not suffice.
