Deadlock Prevention: Allowing Preemption
What you'll learn: How forcibly reclaiming resources from processes can break the "no preemption" condition and prevent deadlocks.
The Core Idea
Recall that one of the four Coffman conditions for deadlock is no preemption — resources cannot be forcibly taken away from a process holding them. If we allow preemption, we can break this condition and prevent deadlock from forming.
When preemption is enabled, the system can forcibly reclaim resources from processes that are waiting for additional resources. The preempted process releases what it holds, and those resources become available to others. The original process must later re-acquire everything it needs (from scratch) to continue.
How It Works
Consider two processes: Process A holds Resource 1 and waits for Resource 2, while Process B holds Resource 2 and waits for Resource 1 (a classic circular wait scenario). With preemption allowed:
- The system detects that Process A is blocked waiting
- The system preempts Resource 1 from Process A
- Resource 1 is now available for Process B
- Process B completes and releases Resource 2
- Process A can now acquire both resources and proceed
Real-World Applicability
This strategy works best with resources whose state can be easily saved and restored — like CPU registers or memory pages. It's poorly suited for resources like printers or mutex locks, where "undoing" partial work is impractical or impossible.
Analogy: Imagine someone borrowing your pencil while working on a math problem. If they get stuck waiting for an eraser, you might take back your pencil temporarily to finish your own work, then return it when they're ready to continue.
Key Takeaway: Allowing preemption breaks the "no preemption" Coffman condition by enabling the system to forcibly reclaim resources from waiting processes, though this only works well for resources with easily saved/restored state.