Deadlocks
Deadlocks
Chapter 7: Deadlocks
Operating System Concepts - 7th Edition, Feb 14, 2005 7.2 Silberschatz, Galvin and Gagne ©2005
Chapter Objectives
Operating System Concepts - 7th Edition, Feb 14, 2005 7.3 Silberschatz, Galvin and Gagne ©2005
The Deadlock Problem
Operating System Concepts - 7th Edition, Feb 14, 2005 7.4 Silberschatz, Galvin and Gagne ©2005
Bridge Crossing Example
Operating System Concepts - 7th Edition, Feb 14, 2005 7.5 Silberschatz, Galvin and Gagne ©2005
System Model
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
Each resource type Ri has Wi instances (ex: if we have two CPU’s in the
computer then we have two instances from the CPU).
Each process utilizes a resource as follows (mode of operation):
Request : if the request cannot be granted immediately (for example
the resource is being used by another process), then the requesting
process must wait until it can acquire the resource.
Use:
Release
Both release and request of resources are system calls.
A system table used to record weather each resource is free or allocated,
and to which process the resource is allocated.
Operating System Concepts - 7th Edition, Feb 14, 2005 7.6 Silberschatz, Galvin and Gagne ©2005
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously (all of them together).
Mutual exclusion: only one process at a time can use a resource (the
resource have one instance or no sharing is allowed for that resource
(one process at a time can use that resource)).
Hold and wait: a process holding at least one resource is waiting to
acquire additional resources held by other processes.
No preemption: a resource can be released only voluntarily by the
process holding it, after that process has completed its task.
Circular wait: there exists a set {P , P , …, P } of waiting processes
0 1 0
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 P0 is waiting for a resource that is held by P0.
Operating System Concepts - 7th Edition, Feb 14, 2005 7.7 Silberschatz, Galvin and Gagne ©2005
Resource-Allocation Graph
Operating System Concepts - 7th Edition, Feb 14, 2005 7.8 Silberschatz, Galvin and Gagne ©2005
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Operating System Concepts - 7th Edition, Feb 14, 2005 7.9 Silberschatz, Galvin and Gagne ©2005
Example of a Resource Allocation Graph
(no deadlock) (p3 could release R3)
Operating System Concepts - 7th Edition, Feb 14, 2005 7.10 Silberschatz, Galvin and Gagne ©2005
Resource Allocation Graph With A Deadlock
Operating System Concepts - 7th Edition, Feb 14, 2005 7.11 Silberschatz, Galvin and Gagne ©2005
Graph With A Cycle But No Deadlock
because P2 and P4 could release the instances of R1 and
R2; then P1 will use R1 and P3 will use R2 (no dead lock)
Operating System Concepts - 7th Edition, Feb 14, 2005 7.12 Silberschatz, Galvin and Gagne ©2005
Graph With A Cycle But No Deadlock
because R1 have two instances and one of them is free;
then P1 will use R1 instance and it have R2 already to
finish its execution (no dead lock)
Operating System Concepts - 7th Edition, Feb 14, 2005 7.13 Silberschatz, Galvin and Gagne ©2005
Basic Facts
Operating System Concepts - 7th Edition, Feb 14, 2005 7.14 Silberschatz, Galvin and Gagne ©2005
Methods for Handling Deadlocks
Operating System Concepts - 7th Edition, Feb 14, 2005 7.15 Silberschatz, Galvin and Gagne ©2005
Deadlock Prevention
Prevent at least one condition from the conditions that must
be hold for the deadlock to occur. The possible side effects for this method (prevention)
are: low device utilization and the reduction of system throughput.
Mutual Exclusion – it is not possible to prevent deadlock by denying this
condition because:
Mutual exclusion is not required for sharable resources (like read-only
files; these files can be shared with no problems).
Mutual exclusion must hold for nonsharable resources (like printers, Such
resources are instinctively non-sharable ).
Hold and Wait – must guarantee- using a protocol- 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, or allow process to request resources only when the
process has none.
This have disadvantages such as: low resource utilization and
starvation.
– Starvation could happen for processes that needs many recourses,
in this case this process may wait for long time periods until all its
requested resources are available together.
Operating System Concepts - 7th Edition, Feb 14, 2005 7.16 Silberschatz, Galvin and Gagne ©2005
Deadlock Prevention (Cont.)
Operating System Concepts - 7th Edition, Feb 14, 2005 7.17 Silberschatz, Galvin and Gagne ©2005
Deadlock Prevention (Cont.)
Circular Wait – to prevent this condition from happening we can impose
a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration.
Ex: we will give each resource a number, such as
tape drive =1 , disk drive =2, printer =3.
To prevent the circular wait condition implement the following protocol
which composed from two conditions:
If a process A needs two resources (tape drive and disk drive) then it
should order the tape drive first, then it order the disk drive.
If a process A holds the printer and needs the tape drive, it must first
release the printer, then order the tape drive, after this it can order
the printer.
The previous protocol can be proved (not required), for more details see
book page 255.
Operating System Concepts - 7th Edition, Feb 14, 2005 7.18 Silberschatz, Galvin and Gagne ©2005
Deadlock Avoidance
Requires that the system has some additional a priori information --to help in
Avoiding the dead lock from happening-- to be available, such information may
Include the previous knowledge about a process Q which will need the tape drive then
the printer. Other process W will need the Printer then the tape drive, and etc……
Operating System Concepts - 7th Edition, Feb 14, 2005 7.20 Silberschatz, Galvin and Gagne ©2005
Safe State : it is an algorithm for deadlock
avoidance
System is in safe state if there exists a sequence <P1, P2, …, Pn> of
ALL the processes in the systems such that 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.
That is:
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.
Disadvantage: low resource utilization because we have waiting
processes.
Operating System Concepts - 7th Edition, Feb 14, 2005 7.21 Silberschatz, Galvin and Gagne ©2005
Basic Facts
Operating System Concepts - 7th Edition, Feb 14, 2005 7.22 Silberschatz, Galvin and Gagne ©2005
Safe, Unsafe , Deadlock State
possibility of
deadlock.
Not guaranteed
to enter a
deadlock if you
are in the
unsafe area
Operating System Concepts - 7th Edition, Feb 14, 2005 7.23 Silberschatz, Galvin and Gagne ©2005
Avoidance algorithms
Operating System Concepts - 7th Edition, Feb 14, 2005 7.24 Silberschatz, Galvin and Gagne ©2005
Resource-Allocation Graph Scheme
Operating System Concepts - 7th Edition, Feb 14, 2005 7.25 Silberschatz, Galvin and Gagne ©2005
Resource-Allocation Graph
Case A: if we assign R2 to P1 then no circle and no dead lock
Case B: if we assign R2 to P2 then we will have circle and a deadlock
Operating System Concepts - 7th Edition, Feb 14, 2005 7.26 Silberschatz, Galvin and Gagne ©2005
Unsafe State (circle exists in the graph) In
Resource-Allocation Graph
Operating System Concepts - 7th Edition, Feb 14, 2005 7.27 Silberschatz, Galvin and Gagne ©2005
Resource-Allocation Graph Algorithm
Operating System Concepts - 7th Edition, Feb 14, 2005 7.28 Silberschatz, Galvin and Gagne ©2005
Banker’s Algorithm
Operating System Concepts - 7th Edition, Feb 14, 2005 7.29 Silberschatz, Galvin and Gagne ©2005
Data Structures for the Banker’s Algorithm
Operating System Concepts - 7th Edition, Feb 14, 2005 7.30 Silberschatz, Galvin and Gagne ©2005
Safety Algorithm
Operating System Concepts - 7th Edition, Feb 14, 2005 7.31 Silberschatz, Galvin and Gagne ©2005
Resource-Request Algorithm for Process Pi
Operating System Concepts - 7th Edition, Feb 14, 2005 7.32 Silberschatz, Galvin and Gagne ©2005
Example of Banker’s Algorithm
3 resource types:
A (10 instances), B (5instances), and C (7 instances).
Snapshot at time T0:
Operating System Concepts - 7th Edition, Feb 14, 2005 7.33 Silberschatz, Galvin and Gagne ©2005
Example (Cont.)
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P2,
P0> satisfies safety criteria.
can you create another sequence that satisfies the safe state? yes
Operating System Concepts - 7th Edition, Feb 14, 2005 7.34 Silberschatz, Galvin and Gagne ©2005
Example: P1 Request (1,0,2)
Check that Request Available (that is, (1,0,2) (3,3,2) true.
Detection algorithm
Recovery scheme
Operating System Concepts - 7th Edition, Feb 14, 2005 7.36 Silberschatz, Galvin and Gagne ©2005
Single Instance of Each Resource Type
Operating System Concepts - 7th Edition, Feb 14, 2005 7.37 Silberschatz, Galvin and Gagne ©2005
Resource-Allocation Graph and Wait-for Graph
Operating System Concepts - 7th Edition, Feb 14, 2005 7.38 Silberschatz, Galvin and Gagne ©2005
Several Instances of a Resource Type
Operating System Concepts - 7th Edition, Feb 14, 2005 7.39 Silberschatz, Galvin and Gagne ©2005
Detection Algorithm
Operating System Concepts - 7th Edition, Feb 14, 2005 7.40 Silberschatz, Galvin and Gagne ©2005
Detection Algorithm (Cont.)
Operating System Concepts - 7th Edition, Feb 14, 2005 7.41 Silberschatz, Galvin and Gagne ©2005
Example of Detection Algorithm
Operating System Concepts - 7th Edition, Feb 14, 2005 7.42 Silberschatz, Galvin and Gagne ©2005
Example (Cont.)
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
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.
Operating System Concepts - 7th Edition, Feb 14, 2005 7.43 Silberschatz, Galvin and Gagne ©2005
Detection-Algorithm Usage
When, and how often, to invoke the detection algorithm? The answer
depends on two factors:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
If we invoke the detection algorithm every time a request for allocation of
a resource can not be granted, then we can find the process which
cause the dead lock to occur directly (it will be the last one so we role it
back).
Disadvantage: the cost for using the detection algorithm frequently is
high.
If detection algorithm is invoked in intervals (ex: every 60 seconds) or
when the CPU utilization drops under 40 percent then we will have the
following disadvantage:
there may be many cycles in the resource graph and so we
would not be able to tell which of the many deadlocked processes
“caused” the deadlock so we have –maybe- to role back them all.
Operating System Concepts - 7th Edition, Feb 14, 2005 7.44 Silberschatz, Galvin and Gagne ©2005
Recovery from Deadlock: Process Termination
To recover from the deadlock we have three main methods:
Process termination: we have two options:
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is eliminated.
Tell the operator (user) to deal with it manually. (ex: restart the
computer).
Resource preemption: we successively preempt some resources from
processes and give these resources to other processes until the
deadlock is terminated.
In which order should we choose to abort? It depends on:
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? 7.45
Operating System Concepts - 7th Edition, Feb 14, 2005 Silberschatz, Galvin and Gagne ©2005
Recovery from Deadlock: Resource Preemption
Operating System Concepts - 7th Edition, Feb 14, 2005 7.46 Silberschatz, Galvin and Gagne ©2005
End of Chapter 7