a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process
System model:
system consists of resources with resource types R1,R2,...,Rm
ex: cpu, ram. I/O, …
Each resource type of Ri has Wi instances
each process utilize a resource as follows:
request: to utilize resource
use
release
Deadlock can arise if the following four conditions hold simultaneously:
Mutual Exclusion: only one thread at a time can use a resource
Hold and Wait: a thread holding at least one resource is waiting to acquire additional resources held by other threads
No Preemption: a resource can be released only voluntarily by the thread holding it, after it has completed its task
Circular Wait: T0 waits for T1, T1 waits for T2,…Tn waits for Tn+1, Tn waits for T0
if one instance request only 1 resource type each → deadlock
if several instances per resource type → possible deadlock
ex: meaning resource A assigned to a task A, which request another resource B; and resource B is assigned to another task B, while task B request resource A
or it can be involve with more pairs
No cycle → No deadlock
To handle deadlock:
ensure that the system will never enter a deadlock state:
Deadlock prevention
Deadlock avoidance
recover from deadlock state
deadlock detection & recovery
ignore deadlock
Deadlock prevention and avoidance:
Deadlock prevention: Invalidate one of the four necessary conditions for deadlock
Prevent “Mutual Exclusion”: allow processes to use same resource at same time → no deadlock. However, some resources (tape drive, printers, …) are inherently non-sharable
Prevent “Hold and Wait”: when process request a new resource, it does not hold any other resource (make other processes wait for this) or give all resources it needed at once. However, leads to low resource utilization and starvation
Prevent “No Preemption”: if a process have resources ABC, and requests resource D that is not immediately available, then all current resources ABC are released. Preempted resources, ABC, are added to the list of resources that the thread is waiting. Problem: Low resource utilization; starvation
Prevent “Circular Wait”: when a Cycle of two or more processes exists, where each process is waiting for a resource held by the next process in the cycle. To prevent this, we can impose priority systme for the resources. Problem: Complex, Inflexible
Deadlock avoidance:
Requires each thread declares the maximum number of resources of each type needed.
resource allocation state, based on:
the number of available & allocated resources
the maximum demands of the processes
Safe State: a state of processes and their requested resource that there exists a safe order/sequence, which the system could allocate needed resources to threads to be finished, i.e., no deadlock
Safe Sequence: an order of threads <T1,T2,...,Tn> that any request from Ti could be satisfied given the information:
Available resources
Allocated resources by other threads
Claim edge: Ti→Rj indicated that process Ti may request resource Rj; represented by a dashed line
The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph
let n be nb of processes and m be nb of resource types
Available (vector lengh m): if avai[j]=k meaning there are k instances of resource type Rj available
Max (n×m matrix): max[i,j]=k then process Ti may request at most k instances of resource type Rj
Allocation (n×m matrix): allo[i,j]=k then process Ti currently allocated k instances of resource type Rj
Need (n×m matrix): need[i,j]=k then process Ti may need k more instances of resource type Rj
need[i,j]=max[i,j]−allo[i,j]
Safe state check:
Let work and finished be vectors of length m and n
Work=avail
finished[i]=false for all i
Find a process i such that both
finished[i]=false
and need[i]≤work
if no such i exist, go to stop 4
work=work+allo[i]
finished[i]=true
if finished[i]==true for all i, then system is in safe state
Resource request check:
Requesti= request vector for process Ti. If Request[i][j]=k then process Ti wants k instances of resource type Rj
If Request[i]≤Need[i] go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim
If Request[i]≤Available, go to step 3. Otherwise Ti must wait, since resources are not available
Pretend to allocate requested resources to Ti as follows:
Available=Available–Request[i];
Allocation[i]=Allocation[i]+Request[i];
Need[i]=Need[i]–Request[i];
Safe state check: Safe ⇒ Allocate to Ti, else rollback, Ti must wait
Deadlock detection and recovery:
Dection with Wait-for graph: resources with single instance
Simplify the resource allocation graph to build the wait-for graph: Ti→Tj if Ti is waiting for Tj
worst-case O(N2)
Banker-like: resource with multiple instance
Let n= number of processes, and m= number of resources types.
Available (Vector of length m ): If available [j]=k, there are k instances of resource type Rj available
Max ( n×m matrix): If Max[i,j]=k, then process Ti may request at most k instances of resource type Rj
Allocation ( n×m matrix): If Allocation [i,j]=k then Ti is currently allocated k instances of Rj
Detection algorithm: Complexity: O(m×n2)
Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i]= false for i with Allocation [i]=0, Finish [i]= true otherwise
Find a process i such that both:
(a) Finish [i] = false
(b) Request[i] <= Work
If no such i exists, go to step 4
Work=Work+Allocation[i]
Finish [i]= true, then go to step 2
If Finish[i]==false, for some i, then the system is in a deadlock state. Moreover, if Finish [i]== false, then Ti is deadlocked
If a detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph, it is not easy to tell which of the many deadlocked threads “caused” the deadlock.
Recovery from Deadlock:
Process Termination
Abort all deadlocked threads
Abort one process at a time until the deadlock cycle is eliminated
In which order should we choose to abort?
Priority of the thread
How long has the thread computed, and to completion?
Resources that the thread has used
Resources that the thread needs to complete
How many threads will need to be terminated?
Is the thread interactive or batch?
Resource Preemption
Selecting a victim – minimize cost
Rollback – return to some safe state, restart the thread for that state
Starvation – same thread may always be picked as victim, include number of rollback in cost factor