Two concurrent processes use binary semaphores S,T. Process P loops forever: W: [synchronization]; print '0'; print '0'; X: [synchronization]. Process Q loops forever: Y: [synchronization]; print '1'; print '1'; Z: [synchronization]. Synchronization can be placed only at W,X,Y,Z. P(S) means wait/decrement and V(S) means signal. Which always gives an output starting 001100110011...?
A. W=P(S), X=V(S), Y=P(T), Z=V(T); S=T=1 initially
B. W=P(S), X=V(T), Y=P(T), Z=V(S); S=1,T=0 initially
C. W=P(S), X=V(T), Y=P(T), Z=V(S); S=T=1 initially
D. W=P(S), X=V(S), Y=P(T), Z=V(T); S=1,T=0 initially
Answer & solution
Answer: B (no official key was published)
1. The required output demands both pairwise exclusion and alternation starting with P's zeros. Initially only P should have permission: S=1 and T=0.
2. P passes wait(S), prints both zeros, then signals T. Q cannot print any one until this signal; once released it consumes T, prints both ones and signals S.
3. P's next wait(S) cannot pass before Q completes its pair. Inductively the execution consists of 00,11,00,11,..., so B works.
4. In A independent locks allow arbitrary interleaving. C initially lets both processes enter, so even the first pair need not be 00. In D Q waits forever on T because no other process signals T.
Solution: Initially give P permission to run by setting S=1 and block Q with T=0. P waits on S at W, prints both zeroes, and signals T at X. Q waits on T at Y, prints both ones, and signals S at Z. Each process therefore hands permission to the other only after completing its pair, forcing 00,11,00,11,… starting with 00. This is B. Separate locks do not coordinate the processes, and initializing both cross-signalling semaphores to one would permit initial interleaving.
Original paper, page 12