Professional Documents
Culture Documents
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
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
B,C with instances of each as in the table below. The table below is what was reflecting
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