You are on page 1of 38

Deadlocks

Chapter 6
The deadlock problem
A set of blocked processes each holding a
resource and waiting to acquire a resource
held by another process in the set.
Example
1. System has two tape drives, P and P each hold one
tape drive and each needs another one
2. Each section of a bridge can be viewed as a
resource if a deadlock occurs it can be resolved if
one car backs up (preempts resources and rollback)
Deadlock Characterization
Deadlocks arise if the following four
conditions hold simultaneously.
1. Mutual exclusion
2. Hold and Wait
3. No preemption
4. Circular wait
Mutual exclusion (Mutex)
Only one process can have a resource at
a time
Hold & Wait
A process holding at least one resource &
waiting to acquire additional resources
held by other processes
No preemption
A resource can only be released
voluntarily by the process holding it, after
that process has completed its task.
Circular wait
There exists a set {P0,P1,,Pn-1,Pn} of
processes such that P0 is waiting for a
resource 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 P n,
and Pn is waiting for a resource held by P0.
Resource Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types
P = {P0,P1,,Pn-1,Pn}, the set consisting of all the
processes in the system
R = {R0,R1,,Rm-1,Pm}, the set consisting of all
resource types in the system
Assignment edge directed edge Rj to Pi
Request edge directed edge from Pi to Rj
Symbols
Process

Resource type with four instances


Symbols
Pi request instance of Rj

Pi Rj

Pi is holding an instance of Rj
Pi R j
Examples of graphs
Graph with no cycles

R1

P1 P2 R3 P3

R2
Cyclic graph = existence of deadlock
One resource instance = definite, Several
resource instances = possibility
P2
R1

P1
P3

R2

P4
Handling Deadlocks
Prevention
Avoiding
Detection
Prevention
Ensure that the system does not enter a
deadlock state.
When a system makes a request for
resources it gets all or none
Example in the dining philosophers case each
gets two forks or none at all
Implications
No holding of partial requests
Automatic preemption & no deadlock
1. Mutual exclusion -not required for
sharable resources, must hold for non-
sharable resources.
2. Hold and wait- must guarantee that
whenever a process requests a resource
it does not hold any other resources.
Require process to request and be allocated
all its resources before it begins execution.
Low resource utilization may result in
starvation
3. No preemption- if a process that is
holding some resources and requests
another resource that it cannot be
immediately allocated, then should
release all resources it is currently
holding to resource pool.
4. Circular wait- avoid by imposing a total
ordering of all resource types and require
that each process requests resources in
an increasing order of enumeration.
Avoiding Deadlocks
Ensure that at least one of the necessary
conditions for deadlock cannot occur
Requires that
System has apriori information to allocate resources.
Simplest model requires that each process declares
the maximum number of resources of each type that it
may need.
The deadlock algorithm dynamically examines the
resource-allocation state to ensure that there can
never be a circular-wait condition.
Resource allocation state is defined by the number of
allocated resources and the maximum demands of the
processes.
Safe state
When a process requests an available resource, the
system must decide if immediate allocation leaves the
system in a safe state
Safe state means system can allocate resources to
each process in some order & avoid a deadlock.
There exists a safe sequence of all processes
Sequence < P1,P2,,Pn-1,Pn > is safe if for each Pi the
resources that Pi can still request can be satisfied y
currently available resources plus resources held by
all Pj with j < i
If Pi resource needs are not immediately available
then can wait until all Pj have finished.
Safe state = no deadlock, unsafe state = possibility of
Implications
Low device utilization & reduced system
throughput.
Holding of partial resources
System will never enter an unsafe state
Resource Allocation Graph
Algorithm
In addition to assignment and request
edges we have a claim edge
Claim edge Pi to Rj indicates that process
Pi may request resource Rj is represented
by a dashed line.
When a resource is released by a process
the assignment edge is reconverted to a
claim edge.
Resources must be claimed apriori
Bankers Algorithm
Multiple resource types
Each process must make an apriori claim
of maximum resource instances of each
type that it may need.
When a process gets all its resources it
must return them in a finite amount of
time.
Data Structures for Bankers

Let n be the number of processes in the system


and m be the number of resource types.
Data structures:
Available: A vector of length m indicates the number
of available resources of each type. If Available [j] = k,
there are k instances of resource type Rj available
Max: An n x m matrix defines the maximum demand
of each process. If Max [i , j] = k, then process Pi may
request at most k instances of resource type Rj.
Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each
process. If Allocation [i ,j] = k, then process Pi is
currently allocated k instances of resource type Rj.
Need: An n x m matrix indicates the remaining
resource need of each process. If Need [I, j] = k,
then process Pi may need k more instances of
resources Rj to complete its task. Need [I, j] = Max
[I, j] Allocation [I ,j]
Request: An n x m matrix indicates the current
request of each process. If Request [i, j] = k then
process Pi is requesting k more instances of
resource type Rj.
Safety Algorithm
Used to find out whether system is in a
safe state or not
1. Let Work & Finish be vectors of length m &
n respectively.
Initialize Work := Available & Finish [i]:= false for i
= 1,2,,n.
2. Find an i such that both
Finish [i] = false
Needi=< Work
If no such i exists, go to step 4
3. Work := Work + Allocationi
Finish [i] := true
Go to step 2
4. If Finish [i] := true for all i, then the system is
in a safe state.
Resource- Request Algorithm

Let request be the request vector for process


Pi.
If Requesti [j] = k, then Pi wants k instances of
resource type Rj.
Actions performed when Pi requests for
resources
1. If Requesti =< Needi, go to step 2. otherwise
error
2. If Request i=< Available, go to step 3.
otherwise Pi must wait, since the resources
are not available.
3. Have the system pretend to allocate
requested resources to process P i as
follows:
Available := Available Requesti ;
Allocationi:= Allocation i+ Requesti ;
Needi := Needi - Requesti;
4. If the transaction is completed & Pi is
allocated all its resources then
resource- allocation is safe.
If the state is unsafe Pi must wait for
Requesti .
Example
Consider a system with five processes P0, P1 ,P2, P3, P4 and three resource types A,

B,C with instances of each as in the table below. The table below is what was reflecting

at time 0. is the system in a safe state?

Allocation Request Available


ABC ABC ABC

P0 010 753 332


P1 200 322
P2 302 902
P3 211 222
P4 002 433
Still Need = Max request Allocation

Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
Trace through the algorithm
P0 will wait, finish [0]:= false
P1 will finish, work = [5 3 2]
P2 will wait , finish [2]:= false
P3 will finish ,work := [7 4 3]
P4 will finish, work := [7 4 5]
P0 will wait, finish [0]:= false
P2 will finish , work = [10 4 5]
P0 will finish work = [10 5 5]
All processes will finish, system is in a safe sate
and the sequence will be <P1, P3, P4, P2, P0>
Deadlock detection
System can enter deadlocked state
Requirements
Detecting algorithm
Recovery scheme
Detection
Use wait- for graph if all resources have only
one instance & find cycles in the graph.
Periodic cycle detection to find process
responsible for deadlock
Exercise
Suppose now that process P1 requests one
additional instance of resource type A & two
instances of resource type C, request1 = [1 0 2].
Can this request be immediately granted? Does
it leave system in a safe state?
Check Request1 =< available, this is true we
pretend to allocate which means allocation1 = 3
0 2 and available is 2 3 0. Now apply the Safety
Algorithm, is it safe?
Make requests for p4 of 3 3 0 and see what
happens & request 0 2 0 for p0, is system safe?
Deadlock Detection Algorithm
for several instances of a resource type
Variables used by the algorithm
Available: a vector of length m indicates the number
of available resources of each type.
Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each
process.
Request: An n x m matrix indicates the current
request of each process. If Request [i , j] = k, then
process Pi is requesting k more instances of Rj.
1. Let work & Finish be vectors of length m & n, respectively.
Initialize Work:= Available. For i = 1,2,,n, if Allocationi != 0,
then Finish [i]:= false; otherwise Finish [i] := true

2. Find an index i such that both


1. Finish [i] = false
2. Requesti =< Work.
If no such i exists, go to step 4.
3. Work := Work + Allocationi
Finish [i] := true
Go to step 2.
4. If Finish [i] = false, for some i =< i =< n, then the system is in a
deadlock state. If Finish [i] = false then Pi is deadlocked.
Example
Given resource allocation state in the table at
time 0 will the system deadlock?
Allocation Request Available
ABC ABC ABC

P0 010 000 000


P1 200 202
P2 303 000
P3 211 100
P4 002 002
Trace trough detection algorithm
P0 will finish, work := [0 1 0]
P1 will wait
P2 will finish , work := [3 1 3]
P3 will finish ,work := [5 2 4]
P4 will finish, work := [5 2 6] orP1 will finish,
work := [7 2 4]
P1 will finish, work := [7 2 6] or P4 will finish,
work := [7 2 6]
All processes will finish and the sequence will be
<P0, P2, P3, P1, P4>
Homework
Modify the request of process P 2 to [0 0 1],
trace through the algorithm to see if there
is no deadlock. If a deadlock exists list the
processes that are involved in the
deadlock
Recovery
Terminate process causing deadlock
(priority, time to completion , needed/ used
resources, number required to remove
deadlock, type of process) or
Preempt resources of selected victim
process (should result in rollback to get to
safe state)

You might also like