NOTE
Deadlock
Deadlock can occur when processes hold locks each other needs while waiting for one another. These notes organize its conditions and handling approaches.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is Deadlock?
A holds lock 1 and needs lock 2; B holds lock 2 and needs lock 1.
2. Necessary Conditions for Deadlock
- Mutual exclusion: resources cannot be shared. If I hold this resource at a given time, you cannot hold it.
- Hold and wait: after I hold resource A, I also want to acquire resource B.
- No preemption: while I hold this resource, you cannot preempt it.
- Circular wait: I hold resource A and wait for resource B; you hold resource B and wait for resource A. We are both waiting for each other’s resource.
3. Methods for Handling Deadlock
There are mainly four methods:
3.1. Ostrich Algorithm
Take no measures and simply ignore it. Use case: when deadlock does not have much impact on users, or the probability of deadlock is very low.
- Most systems use this strategy.
3.2. Deadlock Detection and Recovery
Do not try to prevent deadlock; instead, take recovery measures when deadlock is detected.
Detect deadlock through a resource-allocation graph, and recover through methods such as preemption, rollback, or killing processes.
3.3. Deadlock Prevention
Prevent deadlock by breaking any of the four deadlock conditions.
- Break mutual exclusion: allow resources to be shared.
- Break hold and wait: request all required resources at once instead of applying for them one by one.
- Break no preemption: make resources preemptible.
- Break circular wait: assign numbers to all resources, and processes can only request resources in numerical order.
3.4. Deadlock Avoidance
Banker’s algorithm.
4. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub