Chapter8 Deadlocks PDF
Chapter8 Deadlocks PDF
Chapter8 Deadlocks PDF
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
1
System Model
A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set.
Example of a deadlock problem:
System has 2 tape drives.
Process P1 and process P2 each hold one tape drive and each
needs another one.
A system consists of a finite number of resources to be distributed
among a number of competing processes.
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
The resources may be either physical (printers, tape drives, memory
space, and CPU cycles) or logical (files and semaphores).
A process must request a resource before using it, and must release
the resource after using it.
System Model
A process may utilize a resource in only the following
sequence:
1. Request: If the request cannot be granted immediately,
then the requesting process must wait until it can
acquire the resource.
2. Use: The process can operate on the resource (ex., print
on printer).
3. Release: The process releases the resource.
The request and release of resources are system calls
(examples: request and release device, open and close file,
and allocate and free memory system calls).
Request and release of other resources can be
accomplished through the wait and signal operations on
semaphores.
2
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a
resource. If another process requests that resource, the
requesting process must be delayed until the resource has
been released.
Hold and wait: a process is holding at least one resource and
is waiting to acquire additional resources held by other
processes (i.e., you hold a resource and wait for another one).
No preemption: a resource can be released only voluntarily
by the process holding it, after that process has completed its
task (ex., in FCFS a process use the CPU until it terminates).
Circular wait: there exists a set {P0, P1, , P0} 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, , Pn1 is
waiting for a resource that is held by Pn, and P0 is waiting for a
resource that is held by P0.
Operating System Concepts
8.5
Resource-Allocation Graph
3
Resource-Allocation Graph (Cont.)
Process:
Resource type with 4 instances of same type:
For example four printers
Pi requests instance of Rj Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
4
Resource Allocation Graph With A Deadlock
Two minimal cycles exist in the
system:
P1 R1 P2 R3 P3 R2 P1
P2 R3 P3 R2 P2
Processes P1, P2, P3 are
deadlocked.
P2 is waiting for R3, which is held
by P3.
P3 is waiting for P1 or P2 to
release R2.
P1 is waiting for P2 to release R1.
We have a cycle:
P1 R1 P3 R2 P1
There is no deadlock.
P4 may release its instance of
R2 and that resource can then
be allocated to P3 breaking the
cycle.
If graph contains no cycles
no deadlock.
If graph contains a cycle
if only one instance per
resource type, then deadlock.
if several instances per
resource type, possibility of
deadlock.
Operating System Concepts
8.10
5
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock
state.
The system can use either a deadlock prevention
or avoidance.
Allow the system to enter a deadlock state and then
recover.
Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems,
including UNIX (need to restart your computer if a
deadlock occur).
Deadlock Prevention
For a deadlock to occur, each of the four necessary conditions
must hold.
By ensuring that at least one of these conditions cannot hold, we
can prevent the occurrence of a deadlock.
1. Mutual Exclusion not required for sharable resources; must
hold for non-sharable resources.
For example, a printer cannot be simultaneously shared by
several processes.
A process never needs to wait for a sharable resource.
2. Hold and Wait must guarantee that whenever a process
requests a resource, it does not hold any other resources.
One protocol requires each process to request and be
allocated all its resources before it begins execution,
Or another protocol allows a process to request resources
only when the process has none. So, before it can request
any additional resources, it must release all the resources
that it is currently allocated.
Operating System Concepts
8.12
6
Deadlock Prevention (Cont.)
7
Deadlock Prevention (Cont.)
3. No Preemption To ensure that this condition does
not hold, we can use the following protocol:
If a process that is holding some resources requests
another resource that cannot be immediately
allocated to it, then all resources currently being
held are released.
Preempted resources are added to the list of
resources for which the process is waiting.
Process will be restarted only when it can regain its
old resources, as well as the new ones that it is
requesting.
8
Deadlock Prevention (Cont.)
A protocol to prevent deadlocks: Each process can
request resources only in an increasing order of
enumeration.
A process can initially request any number of instances
of a resource type Ri.
1. That process can request instances of resource type
Rj if and only if F(Rj)>F(Ri).
From the previous example; a process wants to use
the tape drive and printer at the same time, must first
request the tape drive and then the printer.
2. When a process requests an instance of resource type
Rj, it has released any resources Ri such that
F(Ri)>=F(Rj).
By applying 1 and 2 then the circular-wait condition
cannot hold.
Deadlock Avoidance
9
Safe State
A state is safe if the system can allocate resources to each process
(up to its maximum) in some order and still avoid a deadlock.
System is in safe state if there exists a safe sequence of all
processes.
Sequence <P1, P2, , Pn> is safe if for each Pi, the resources that
Pi can still request can be satisfied by currently available resources
+ resources held by all the Pj, with j<i.
If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished.
When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its needed resources, and
so on.
P1 P0 P2
10
Safe State Example (Cont.)
Basic Facts
If a system is in safe state
no deadlocks.
If a system is in unsafe
state possibility of
deadlock.
Avoidance ensure that
a system will never enter
an unsafe state.
Safe, unsafe , deadlock
state spaces.
11
Resource-Allocation Graph Algorithm
Claim edge Pi Rj indicated that process Pi may request
resource Rj; represented by a dashed line.
Claim edge converts to request edge when a process
requests a resource.
When a resource is released by a process, assignment edge
reconverts to a claim edge.
Resources must be claimed a priori in the system.
If a cycle is found in a graph, then the allocation will put the
system in an unsafe state.
If no cycle exists, then the allocation of the resource will
leave the system in a safe state.
Figure 1: Figure 2:
12
Bankers Algorithm
Bankers algorithm is applicable to a resource-allocation
system with multiple instances of each type.
From its name, it could be used in a banking system to
ensure that the bank never allocates its available cash
such that it can no longer satisfy the needs of all
customers.
Each process must a priori claim maximum use.
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.
13
Safety Algorithm (Bankers Algorithm)
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work := Available
Finish [i] = false for i - 1,3, , n.
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i (no process) 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.
14
Example of Bankers Algorithm
5 processes P0 through P4; 3 resource types A (10 instances),
B (5 instances, and C (7 instances).
Snapshot at time T0: (Need= Max - Allocation)
The system is in a safe state since the sequence
< P1, P3, P4, P0, P2> satisfies safety criteria.
Process Allocation Max Available Need Available after
Release
A B C A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3 7 5 5
P1 2 0 0 3 2 2 1 2 2 5 3 2
P2 3 0 2 9 0 2 6 0 0 10 5 7
P3 2 1 1 2 2 2 0 1 1 7 4 3
P4 0 0 2 4 3 3 4 3 1 7 4 5
15
Example (Cont.)
Can request for (3,3,0) by P4 be granted?
Check Request Need (that is, (3,3,0) (4,3,1)) is true).
Check Request Available (that is, (3,3,0) (2,3,0) is
not true).
So, the request for (3,3,0) by P4 can not be granted.
Can request for (0,2,0) by P0 be granted?
Check Request Need (that is, (0,2,0) (7,4,3) is true).
Check Request Available (that is, (0,2,0) (2,3,0) is
true).
However, since Available becomes 2 for A, 1 for B, and 0
for C, we can not find a sequence to satisfy the safety
requirements. Therefore, the resulting state is unsafe
and the request for (0,2,0) by P0 can not be granted.
Deadlock Detection
If a system does not employ a deadlock prevention algorithm or
a deadlock avoidance algorithm, then a deadlock situation may
occur.
Therefore, the system must provide:
Detection: an algorithm that examines the state of the
system to determine whether a deadlock has occurred.
Recovery: an algorithm to recover from the deadlock.
A detection and recovery scheme requires overhead that
includes:
Run-time costs of maintaining the necessary information.
Executing the detection algorithm.
The potential losses inherent in recovering from a deadlock.
16
Single Instance of Each Resource Type
A deadlock detection algorithm uses a variant of the resource-
allocation graph called wait-for graph.
An edge from Pi to Pj in a wait-for graph implies that process Pi is
waiting for process Pj to release a resource that Pi needs.
An edge Pi Pj exists in a wait-for graph if and only if the
corresponding resource-allocation graph contains two edges
Pi Rq and Rq Pj for some resource Rq.
A deadlock exists in the system if and only if the wait-for graph
contains a cycle.
To detect deadlocks, the system needs to:
Maintain the wait-for graph.
Periodically invoke an algorithm that searches for a cycle in the
graph.
An algorithm to detect a cycle in a graph requires an order of n2
operations, where n is the number of vertices in the graph.
17
Several Instances of a Resource Type
Detection Algorithm
18
Detection Algorithm (Cont.)
3. Work := Work + Allocationi
Finish[i] := true (Assume Pi will require no more
resources to complete).
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.
Algorithm requires an order of m x n2 operations to
detect whether the system is in deadlocked state.
19
Example (Cont.)
Suppose P2 requests an additional instance of type C.
The Request matrix is modified as shown in the
previous slide (New Request).
State of system?
Can reclaim resources held by process P0, but
insufficient resources to fulfill other processes;
requests.
Deadlock exists, consisting of processes P1, P2, P3,
and P4.
20
Recovery from Deadlock: Process Termination
Two methods to eliminate deadlocks by aborting a process. In both
methods, the system reclaims all resources allocated to the
terminated processes:
Abort all deadlocked processes: It will break the deadlock
cycle, but a great expense.
Abort one process at a time until the deadlock cycle is
eliminated: Overhead, since, after each process aborted a
deadlock-detection algorithm must be invoked to determine
whether any processes are still deadlocked.
In which order should we choose to abort?
Priority of the process.
How long process has computed, and how much longer to
completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be terminated.
Is process interactive or batch?
Operating System Concepts
8.41
21
Combined Approach to Deadlock Handling
Combine the three basic approaches
prevention
avoidance
detection
allowing the use of the optimal approach for each of
resources in the system.
Partition resources into hierarchically ordered classes.
Use most appropriate technique for handling deadlocks
within each class.
22