Exercise 2: A Consistent Acquisition Order Prevents Deadlock Entirely — Possible Solution ==================================================================== THE TEST ------------------------------ # BOTH processes now acquire A then B (same order) -- unlike Finding 1, # where P1 went A-then-B and P2 went B-then-A programs[s1.pid] = [('ACQUIRE','A'), ('ACQUIRE','B'), ('RELEASE','B'), ('RELEASE','A')] programs[s2.pid] = [('ACQUIRE','A'), ('ACQUIRE','B'), ('RELEASE','B'), ('RELEASE','A')] RESULT ------------------------------ both processes acquire A THEN B (same order): completed in 14 real steps remaining ops: [0, 0] Both processes finish completely -- no deadlock forms, even though they're contending for the exact same two resources Finding 1 used. WHY A CONSISTENT ORDER MAKES THE CYCLE IMPOSSIBLE ------------------------------ Deadlock's circular wait requires a specific SHAPE: process P1 holds resource A while waiting for B, and process P2 holds resource B while waiting for A -- each one holding what the other wants. When every process acquires resources in the SAME fixed order (A always before B), there is no way to reach a state where anyone holds B while still waiting for A -- to hold B, a process must have already acquired A first, and once it has A, nothing else can be holding A instead (only one process can hold A at a time). So the moment any process successfully acquires A, it is GUARANTEED to eventually get B too, because nothing can simultaneously be "holding B while wanting A" -- that specific combination is structurally unreachable under a consistent order. Whichever process gets A first simply proceeds uncontested to B; the other process waits for A, gets it once the first releases it, and then proceeds to B in turn. WHY THIS IS PREVENTION, NOT DETECTION-AND-RECOVERY ------------------------------ Finding 4's own approach (build a wait-for-graph, detect a cycle, forcibly abort a victim) accepts that deadlock CAN happen and reacts to it after the fact -- real work is done, then thrown away when a process is aborted. A consistent acquisition order is a completely different strategy: it changes the RULES of how resources can be acquired so that the circular-wait shape itself can never occur in the first place, which means there's no detection step needed, no victim to choose, and no wasted work to discard. This is one of the four classical conditions for deadlock (circular wait) being directly denied, rather than the deadlock being allowed to happen and then cleaned up. WHY THIS WORKS AS AN ANSWER ------------------------------ Using the EXACT same two resources and the exact same real contention as Finding 1 -- changing only the ORDER of acquisition, nothing else -- isolates ordering as the single variable responsible for the different outcome, confirming the fix is about the acquisition DISCIPLINE, not about the resources or processes involved being any different.