08 Deadlock
Types of Resources¶
| Preemptable Resource | Non-Preemptable Resource | |
|---|---|---|
| Can be removed from process without causing computation fail | Once allotted to a process, it cannot be removed from a process unless the process relinquishes the resource by itself | |
| Example | Primary Memory can be swapped out | Printers |
Necessary conditions for Deadlocks¶
| Condition | Meaning |
|---|---|
| Mutual-exclusion | Only one process can use a resource at a time |
| Hold & Wait | A process holding at least one resource is waiting to acquire additional resources held by other processes (hence causing a wait) |
| Non-Preemptive resources | A resource can be released only voluntarily by the process holding it, after that process has completed its task. |
| Circular Wait | There exists a set {P0, P1, …, Pn} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0. |
RAG¶
Resource Allocation Graph
Directed graph that how processes and resources are related
| Symbol | Meaning | |
|---|---|---|
| Circle | Process | |
| Rectangle with circles | Resource Type with instances | |
| Edges | Request Edge (process \(P_i\) requests resource \(R_j\)) | \(P_i \to R_j\) |
| Assignment Edge (resource \(R_j\) is alloted to \(P_i\)) | \(R_j \to P_i\) |
Checking deadlock¶
| Cycle exists? | Instances of each resource type | \(\implies\) | Deadlock exists? |
|---|---|---|---|
| ❌ | N/A | ❌ | |
| âś… | 1 | âś… | |
| âś… | Multiple | Inconclusive |
Disadvantage¶
Inconclusive for the 3rd case
Banker’s Algo for Deadlock Detection¶
It is an algorithm which is implemented as a system process in the OS
In this section, processes are referring to user processes
Let
- \(n =\) No of processes
- \(m =\) No of resources
| Dimension | Meaning | Initial value | |
|---|---|---|---|
| Max Matrix | \(n \times m\) | Maximum resource requirement of each type by every process | |
| Allocation Matrix | \(n \times m\) | Total number of resources of each type that is currently allocated to each process | |
| Need Matrix | \(n \times m\) | Denotes the number of resources of each resource type that are yet to be allocated to a process | |
| Available Vector | \(m\) | Total no of instances of each resource type that is available | |
| Work Vector | \(m\) | No of instances of each resource type that is currently available | Work = Available |
| Finish Vector | \(n\) | Denotes the completion status of each process | False |
| Request Vector for process \(i\) | \(m\) | No of instances of each type of resource that process \(i\) requests |
Vector Comparison¶
Let \(X, Y\) be 2 vectors
Every element of \(X\) should be smaller than/equal to every corresponding element of \(Y\)
Algorithm¶
-
Initialize all vectors
-
Find process \(i\) with
-
Finish[i] =
False - Need[i] \(\le\) Work
If we can’t find, go to step 4
-
- Work = Work + Allocation[i]
- Finish[i] = True
- Go to step 2
-
If finish[i]==True \(\forall i\), the system is in a safe state
Safe Sequence¶
You may get multiple valid safe sequences for the same list of process
Resource-Request Algorithm¶
Consider a Request[i] vector for process \(P_i\)
-
Check if Request[i] \(\le\) Need[i]
- if true, got to step 2
- else, raise error condition that \(P_i\) has requested more than it needs
-
Check if Request[i] \(\le\) Available[i]
-
if true, go to step 3
-
else, \(P_i\) must wait, as resources are not available
-
Pretend to allocate requested resources to \(P_i\), by modifying the states as follows
-
Run the safety algo to check if the system is in a safe state
- If safe, resources are allocated to \(P_i\)
- else, \(P_i\) must wait and the old resource-allocated state is restored
Deadlock Handling¶
| Deadlock Avoidance | |
| Deadlock Prevention | |
| Deadlock Detection & Recovery |
Deadlock Detection¶
Almost exactly the same as Banker’s; just that this has request instead of need.
-
Initialization
-
Let
work = available -
Check if \(\text{allocation}[i] \ne 0\)
- true \(\implies\) set finish[i] = false
- false \(\implies\) set finish[i] = true
-
Find \(i\) such that
-
Finish[i] = false
- request[i] \(\le\) work
If not found, go to step 4
-
Work = Work + Allocation Finish[i] = true Go to step 2
-
If finish[i] == false, for some \(i \implies\) system is in deadlock state or, in other words, there is no deadlock if
- all the finish[i] = false \(\forall i\)
- we are able to derive a safe sequence with all the processes, then there is no deadlock