Synchronization & Concurrency





Answer & solution
Answer: Both processes can read mutex=0 before either sets it, so both enter. (no official key was published)
Initially mutex=0. Split TEST_AND_SET into the source steps A1: y=*x; A2: *x=1; A3: return y. Interleave two processes as follows:
1. P executes A1 and stores local yP=0.
2. Q executes A1 and stores local yQ=0.
3. P executes A2, writing mutex=1.
4. Q executes A2, also writing mutex=1.
5. P executes A3 and returns its saved zero; its while condition is false and it enters the critical section.
6. Q executes A3 and returns its saved zero; it also enters before P leaves.
The final shared value one does not distinguish the two prior reads. Atomicity must cover the read and write as one indivisible operation.