OPERATING SYSTEMS
DEADLOCKS
K.ARIVUSELVAN
Associate Professor – (SITE)
VIT University
The Deadlock Problem
Processes each holding a resource and waiting to
acquire a resource held by another process.
Example1
System has 2 tape drives
P1 and P2 each hold one tape drive and each needs
another one
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously:
(1) Mutual exclusion: only one process at a time can
use a resource (i.e. resource is non-sharable).
(2) Hold and wait: a process holding at least one
resource is waiting to acquire additional resources held
by other processes.
Deadlock Characterization
(3) No preemption: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task.
(4) 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.
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Prevention
=> Ensure atleast one of the necessary conditions cannot hold.
Deadlock Avoidance
=> Given additional information in advance, concerning which
resources a process will request and use during its life time
NOTE:
=> A System does not employ either a deadlock prevention or
deadlock avoidance algorithm , it leads to a deadlock situation.
Deadlock Prevention
(1) Mutual Exclusion:
=> Hold for non-sharable resource (Printer can’t share
simultaneously)
=> Sharable resource mutual exclusion not required ( Read only file
access by several process simultaneously). No dead lock
=> In general it is not possible to prevent deadlock by denying the
mutual exclusion condition.
Deadlock Prevention
(2) Hold & Wait:
1st Protocol :
=> Allot all requested resource before begins execution
Example:
Copying data from Tape drive to Disk file, Sort Disk file, Print
Drawback:
Hold printer entire execution even printer need at last
Deadlock Prevention
(2) Hold & Wait:
2nd Protocol :
=> Request some resource and use them
=> Before request, release additional resource currently
allotted.
Example:
Request Tape drive & Disk
Release
Request Disk & Printer
Deadlock Prevention
(3) No Preemption:
Protocol :
=> Check for resource availability
if (available)
{ allocate resource}
Else
{Check other process hold resource and request additional
resource }
If (Yes) then PREEMPT
Deadlock Prevention
(4) Circular Wait:
Protocol :
=>Assign unique integer to resource type
Example:
Tape Drive = 1, Disk = 4, Printer = 10
Constraint :
=> Process request the resource only in increasing order
=> Process that want to use both Tape & Printer at the same time
must first request Tape & then Printer
Safe State
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
Systemis in safe state if there exists a safe sequence of all
processes.
Deadlock Avoidance
Banker’s Algorithm
A resource allocation system with Multiple instances
Each process must a priori claim maximum use.
Constraint:
When a process requests a resource it may have to wait.
When a process gets all its resources it must return them in a
finite amount of time.
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
Available: Number of available resources. If Available [j] = k,
there are k instances of resource type Rj available.
Max: Maximum demand of each process. If Max [i,j] = k, then
process Pi may request at most k instances of resource type Rj.
Allocation: Number of resource currently allocated to each
process. If Allocation[i,j] = k then Pi is currently allocated k
instances of Rj.
Need: Remaining resource need of each process. If Need[i,j] = k,
then Pi may need k more instances of Rj to complete its task.
Need [i,j] = Max[i,j] – Allocation [i,j]
Example
5 processes P0 through P4;
3 resource types A (10 instances), B (5 instances) and
C (7 instances).
Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Example (Cont.)
The content of the matrix Need is defined to be Max – Allocation.
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
Safety Algorithm
Finding out whether or not a system is in safe state
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 1,2,3, …, n.
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.b
4. If Finish [i] == true for all i, then the system is in a safe state.
Resource-Request Algorithm for Process Pi
Request i = request vector for process Pi.
If Request [ j ] = k then process Pi wants k instances of
i
resource type Rj.
ACTION TAKEN:
1. If Requesti Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi must wait,
since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:`
How?
Check if
this new
state is
Safe.
i.e.if a safe
Available = Available –Requesti ;
sequence
exists
Allocationi = Allocationi + Requesti ;
Needi = Needi – Requesti ;
If safe the resources are allocated to Pi.
If unsafe Pi must wait, and the old resource-allocation
state is restored
Resource-allocation Graph
A visual way to determine if a deadlock has, or may occur.
RAG consist a set of vertices V and a set of edges E
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes in
the system.
R = {R1, R2, …, Rm}, the set consisting of all resource
types in the system
Request edge – directed edge Pi Rj
Assignment edge – directed edge Rj Pi
If graph contains cycles deadlock
Resource Allocation Graph
Wait-For Graph (Single Instance)
Nodes are processes
Example:
Pi Pj, if Pi is waiting for Pj
Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Graph Corresponding wait-for graph
Example-1
Consider the following snapshot of a system in which five
resources A, B, C, D and E are available. Convert this
matrix representation to a resource allocation graph.
* System is in Safe State
Example-2
Consider the following snapshot of a system in which four
resources R1, R2, R3, R4 are available. Convert this matrix
representation to a resource allocation graph.
Resource Allocation Graph
Example-1
Consider the following resource allocation graph.
Convert it to the matrix representation (i.e., Allocation,
Request and Available).
Matrix representation:
Example-2
Consider the following resource allocation graph.
Convert it to the matrix representation (i.e., Allocation,
Request and Available).
Matrix representation:
Deadlock Detection
Allow system to enter deadlock state
(1) Detection algorithm
(2) Recovery scheme
Detection Algorithm
Multiple Instances of a Resource Type
Data Structures:
Available: Number of available resources of each type.
Allocation: The number of resources of each type currently
allocated to each process.
Request: The current request of each process.
If Request [i,j] = k, then process Pi is requesting k more instances
of resource type. Rj.
Detection Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
(a) Work = Available
(b) For i = 1,2, …, n,
if Allocationi 0, then Finish[i] = false;
otherwise, Finish[i] = true (i.e..Allocationi = 0).
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
If no such i exists, go to step 4.
Detection Algorithm (Cont.)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish[i] == false, for some i , 1 i n, then the system is in
deadlock state.
Moreover, if Finish[i] == false, then Pi is deadlocked.
Deadlock Detection - Example
Sequence One: P0,P2,P3,P1,P4
No Deadlock
Sequence Two: P2,P0,P4,P3,P1
No Deadlock
Sequence: P0,P2,P3,P1,P4
Deadlock
Recovery from Deadlock: Process Termination
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is eliminated.
In which order should we choose to abort?
Priority of the process
How long process has computed, and how much longer to
completion
Resources process needs to complete