Deadlock Detection
Deadlock Detection
If a system does not use:
-
Deadlock Prevention, or
-
Deadlock Avoidance
then a deadlock may occur.
In such systems, the OS must provide:
-
A deadlock-detection algorithm
-
A recovery algorithm
However, detection and recovery introduce overhead:
-
Runtime cost of maintaining information
-
Cost of executing detection algorithm
-
Losses during recovery (e.g., terminating processes, rollback)
Single Instance of Each Resource Type
If each resource type has only one instance, deadlock detection is simpler.
In this case, we use a special graph called a:
Wait-For Graph
What is a Wait-For Graph?
It is derived from the Resource-Allocation Graph by:
-
Removing all resource nodes
-
Collapsing the edges
Meaning of Edges
In a wait-for graph:
An edge
means:
Thread Ti is waiting for thread Tj to release a resource.
More precisely:
An edge Ti → Tj exists in the wait-for graph
if and only if the resource-allocation graph contains:
for some resource Rq.
That means:
-
Ti is requesting resource Rq
-
Rq is currently allocated to Tj
-
Therefore, Ti is waiting for Tj
Deadlock Condition
A deadlock exists:
If and only if the wait-for graph contains a cycle.
So detection becomes:
✔ Construct wait-for graph
✔ Check if there is a cycle
If cycle exists → Deadlock
If no cycle → No deadlock
Cycle Detection Complexity
Detecting a cycle in a graph requires:
Where:
-
n = number of threads (vertices)
Practical Example (Linux – BCC Toolkit)
The BCC toolkit (mentioned in the text) provides a deadlock detection tool for:
-
Pthreads mutex locks
-
Linux systems
How it works:
-
Inserts probes into:
-
pthread_mutex_lock() -
pthread_mutex_unlock()
-
-
Builds a wait-for graph dynamically
-
Reports possible deadlock if a cycle is detected
Summary (Single Instance Case)
| Feature | Description |
|---|---|
| Resource instances | One per type |
| Graph used | Wait-for graph |
| Deadlock condition | Cycle in graph |
| Time complexity | O(n²) |
| Used in practice | Mutex deadlock detection |
Deadlock Detection: Several Instances of a Resource Type
When a system has multiple instances of each resource type, the wait-for graph method cannot be used.
Instead, we use a detection algorithm that is similar to the Banker’s Algorithm, but it is used for detection, not avoidance.
Data Structures Used
Let:
-
n = number of threads
-
m = number of resource types
The algorithm uses the following time-varying data structures:
Available (Vector of size m)
Allocation (n × m Matrix)
Request (n × m Matrix)
Idea of the Algorithm
The detection algorithm:
Tries to find a possible completion sequence for all threads.
If such a sequence exists → No deadlock
If some threads cannot complete → Deadlock exists
This is similar to the Safety Algorithm in Banker’s Algorithm, but here we check current requests instead of maximum needs.
Deadlock Detection Algorithm
Let Work and Finish be vectors of length m and n, respectively. Initialize
Work = Available. For i = 0, 1, ..., n − 1, if Allocationᵢ ≠ 0, then Finish[i] = false. Otherwise, Finish[i] = true.Find an index i such that both
a. Finish[i] == false
b. Requestᵢ ≤ Work
If no such i exists, go to step 4.Work = Work + Allocationᵢ
Finish[i] = true
Go to step 2.If Finish[i] == false for some i, 0 ≤ i < n, then the system is in a deadlocked state. Moreover, if Finish[i] == false, then thread Tᵢ is deadlocked.
Explanation
Step 1: Initialization
Let:
-
Work = Available
-
Finish[i] =
-
false, if Allocationᵢ ≠ 0
-
true, if Allocationᵢ = 0
-
This means:
-
Threads holding no resources are not part of a deadlock.
Step 2: Find a Thread That Can Finish
Find an index i such that:
-
Finish[i] == false
-
Requestᵢ ≤ Work
If no such i exists → go to Step 4.
Step 3: Assume Thread i Finishes
Then go back to Step 2.
Step 4: Deadlock Check
If:
Then:
The system is in a deadlocked state.
Those threads with Finish[i] == false are deadlocked.
Time Complexity
The algorithm requires:
Where:
-
m = number of resource types
-
n = number of threads
Why Do We Reclaim Resources in Step 3?
When:
We assume optimistically that:
-
Thread Ti will complete
-
It will release its resources
-
Those resources can be used by others
If this assumption is wrong, the next execution of the detection algorithm will detect the deadlock.
Example
Initial State:
System:
-
5 threads (T0–T4)
-
3 resource types (A, B, C)
-
Resource type A has seven instances, resource type B has two instances, and resource type C has six instances. The following snapshot represents the current state of the system:
Suppose now that thread T2 makes one additional request for an instance of type C. The Request matrix is modified as follows:
Key Difference from Banker’s Algorithm
| Banker’s Algorithm | Detection Algorithm |
|---|---|
| Used for avoidance | Used after deadlock may occur |
| Uses Need matrix | Uses Request matrix |
| Ensures safe state | Detects deadlocked threads |
| Prevents deadlock | Identifies deadlock |
Final Summary
When multiple instances exist:
-
Wait-for graph cannot be used.
-
Use matrix-based detection algorithm.
-
Try to simulate completion.
-
If some threads cannot finish → deadlock exists.
-
Time complexity: O(m × n²)



Comments
Post a Comment