Deadlocks
Resource deadlock: When processes are infinitely waiting for each other for a resource to be released
Conditions for resource deadlocks:
- Mutual exclusion: each resource can be used by at most one process
- Hold and wait: processes holding resources can request multiple resources
- No preemption: resources granted earlier can not be taken away
- Circular chain of requests: One process is waiting for another which is waiting on another etc...
Resource Allocation Graph: processes circles and resources square. Holding a resource: edge from resource to process. Requesting a resource: edge from process to resource. Cycles in this graphs are deadlocks.
Algorithms:
- Graph cycle detection using DFS
- Detection with multiple resources of each type
- Look for unmarked processes $i$ where $R_i \leq A$
- Add $C_i$ to $A$ and mark processes
- Terminate if so process found
- Banker's algorithm: checks if the next state is safe. With multiple resources it
Safe sate: there exists an scheduling order in which every process can run to completion (even if all of them request their maximum number of resources at once)
Deadlock recovery #
- Ignoring the problem (used by Windows/Linux)
- Preemption
- Rollback
- Killing processes
Other things #
Two-phase locking: useful in databases. A process tries to lock all the records it needs first. If a record is already locked it releases all locks and tries again after some time. Livelock: when processes try to to get the same lock at the same time, no process is blocked so it is not a deadlock Communication deadlock: in networked systems, solved by resending a message after a timeout