Notes on deadlocks in operating systems.
System Model
Notations:
- Threads T1, T2, …, Tn
- Resource types R1, R2, …, Rm
- e.g., CPU, memory space, I/O devices, mutex and semaphores
- Each resource type Ri has Wi instances.
Definition: a set of threads is in a deadlocked state when every thread in the set is waiting for an event that can only be caused by another thread in the set. These events are mainly the acquisition and release of resources.
e.g. two vehicles travelling in opposite directions meeting on a single-lane road
Deadlock Characterization
A deadlock arises when the following four conditions hold simultaneously:
- Mutual Exclusion: at that moment the resource is held exclusively by one thread
- Hold and Wait: a thread is waiting for another thread to release a resource
- No Preemption: a thread cannot preempt the resources of another thread
- Circular Wait: a set of threads forms a cycle, each waiting for another thread to release a resource
Resource-Allocation Graph
The Resource-Allocation Graph (RAG) is a directed graph used to represent the relationships of resource allocation and requests.
- Nodes represent processes and resources
- Edges represent processes’ requests for and releases of resources
Methods for Handling Deadlocks
- Never enters a deadlock
- Deadlock prevention
- Deadlock avoidance
- Detect and recover from deadlock
- Deadlock detection
- Recovery from deadlock
Deadlock Prevention
Break one of the four conditions:
- Mutual exclusion: remove the mutual exclusion of shared resources
- Hold and wait: request all resources at once / only request resources when holding none
- No preemption: allow resources to be preempted
- Circular wait: impose an ordering on resources
Deadlock Avoidance
Requires additional information:
- The maximum demand of each process for each type of resource
- The number of available instances of each type of resource
safe state: a sequence is safe if, for every process, it can complete in finite time and the resources it needs can be satisfied by the currently available resources plus the resources previously allocated
Resource allocation policy: allocate resources when a safe sequence exists
resource-allocation-graph algorithm
- Related concepts: a new concept is introduced, the claim edge, drawn as a dashed directed edge
Ti → Rj, meaning threadTimay request resourceRj. When the thread requests the resource, the claim edge is converted into a request edge; when the resource is allocated to the thread, the request edge is converted into an assignment edge; when the thread releases the resource, the assignment edge is converted back into a claim edge (if the thread terminates, the edge is removed). - Core decision of the algorithm: when thread
Tirequests resourceRj, the request can be granted only if converting the request edge into an assignment edge does not create a cycle in the resource-allocation graph. If a cycle would be formed, a deadlock may occur, so the request cannot be granted and the thread must wait. For example, if threadT1requests resourceR1and, starting from the current resource-allocation graph, converting the request edge fromT1toR1into an assignment edge would produce a cycle such asT1 → R1 → T2 → R2 → T1, thenT1‘s request forR1cannot be granted andT1has to wait
Banker’s Algorithm
The Banker’s Algorithm is a well-known deadlock avoidance algorithm. It gets its name because the algorithm was originally the strategy a bank uses to allocate its resources.
Basic Concepts
Data structures
Available: a vector of length m, the number of available instances of each resource typeMax: an n×m matrix, the maximum demand of each process for each resource typeAllocation: an n×m matrix, the number of instances of each resource type currently allocated to each processNeed: an n×m matrix, the number of instances of each resource type each process still needs
where n is the number of processes and m is the number of resource types
Safe state
The system is in a safe state if there exists a safe sequence such that all processes can complete in that order.
Algorithm Flow
1 |
|
Deadlock Detection
Single instance of each resource type: maintain a wait-for graph
Multiple instances of each resource type:
- available vector
- allocation matrix
- request matrix
1 |
|
Core of the algorithm: use the work vector and the finish vector to decide whether a deadlock exists, iterating in a loop to find processes that can complete
When using the detection algorithm, one has to consider overheads such as how often deadlocks occur and how many threads must be rolled back
Recovery from Deadlock
- Process termination: terminate one or more processes until the deadlock is broken
- Resource preemption: preempt resources from one or more processes until the deadlock is broken
Translated from the Chinese original.

