Transcript ppt

Chapter 7: Deadlocks
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Silberschatz, Galvin and Gagne ©2007
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
 Procs P1 and P2 each hold one disk drive, and
each needs the other drive.
 Example 2
 semaphores A and B, initialized to 1
P0
P1
A.wait ();
B.wait();
B.wait ();
A.wait();

Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.4
Silberschatz, Galvin and Gagne ©2007
Java Deadlock Example
Thread A
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Thread B
7.5
Silberschatz, Galvin and Gagne ©2007
Java Deadlock Example
Deadlock is possible if:
threadA -> lockY -> threadB -> lockX -> threadA
java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLo
ck.html
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.6
Silberschatz, Galvin and Gagne ©2007
Bridge Crossing Example





Traffic only in one direction.
Each section of a bridge can be viewed as a
resource.
If a deadlock occurs, it can be resolved if one car
backs up (preempt resources and rollback).
Several cars may have to be backed up if a deadlock
occurs.
Starvation is possible.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.7
Silberschatz, Galvin and Gagne ©2007
Necessary Conditions for Deadlock
Deadlock may only arise if four conditions hold:




Mutual exclusion: only one process at a time can
use a 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 is a set {P0, P1, …, P0} of waiting
processes such that P0 is waiting for a resource held
by P1; P1 is waiting for a resource held by P2; …; Pn–1
is waiting for a resource held by Pn; and P0 is waiting
for a resource held by P0.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.9
Silberschatz, Galvin and Gagne ©2007
Handling Deadlocks in Java
plain, simple lock
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.16
Silberschatz, Galvin and Gagne ©2007
Handling Deadlocks in Java
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.17
Silberschatz, Galvin and Gagne ©2007
Methods for Handling Deadlocks
Ensure the system will never deadlock.
 #1 Prevention (break one condition for
deadlock)
 #2 Avoidance (keep in safe state)
 #3 Allow system to enter deadlock and then
recover.
 #4 Ignore the problem and pretend that
deadlocks never occur; used by most operating
systems, including UNIX.
 For m resources and n procs, ##1, 3 take
O(mn2) time, #2 takes O(mn) time.

Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.18
Silberschatz, Galvin and Gagne ©2007
Safe, Unsafe Deadlock State
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.19
Silberschatz, Galvin and Gagne ©2007
End of April 4, 2007
Lecture
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Silberschatz, Galvin and Gagne ©2007
Suppose a bank has 5 customers
It has been loaning money. At this instant,
 the amount loaned, the max need and remaining
 available money are as follows (in billions of $$):
Allocation Max Available
$
$
$
C0
0
7
3
C1
2
3
C2
3
9
C3
2
2
C4
0
4
Question: Can the bank satisfy all the customers? How?

Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.21
Silberschatz, Galvin and Gagne ©2007
Suppose a bank has 5 customers
It loans money in three currencies: $, £, and ¥
 The amounts loaned, max need, and available currencies are
as follows:
Allocation Max Available
$£¥
$£¥
$£¥
C0 0 1 0
753
332
C1 2 0 0
322
C2 3 0 2
902
C3 2 1 1
222
C4 0 0 2
433
Question: Can the bank satisfy all the customers? How?

Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.22
Silberschatz, Galvin and Gagne ©2007
Deadlock Prevention
Restrain the ways request can be made.


Mutual Exclusion – (assumed to be needed, as
small as possible).
Hold and Wait – must guarantee that whenever a
process requests a resource, it does not hold any
other resources.
 Possible solution: Aquire all resources all at
once before proc begins execution.
 Problems: Low resource utilization; starvation
possible.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.23
Silberschatz, Galvin and Gagne ©2007
Deadlock Prevention (Cont.)


No Preemption – One possible solution:
 1 If a proc that is holding some resources
requests another that cannot be immediately
allocated to it, then release all resources.
 2 Requested resource is added to the list of
resources for which the proc is waiting.
 3 Proc will be restarted only when it can regain
its old resources as well as the new ones that
it needs.
Circular Wait – One possible solution: Impose a
total ordering of all resource types; require that
each proc request resources in increasing order.
Another possible solution: Dijkstra
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.24
Silberschatz, Galvin and Gagne ©2007
Edsgar Dijkstra’s Deadlock Avoidance
Requires that the system has a priori information.



E.g. each proc declare the max number of
resources of each type that it may need.
The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to ensure
that there can never be a circular wait.
Resource-allocation state is defined by the number
of available and allocated resources, and the
maximum demands of the procs.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.25
Silberschatz, Galvin and Gagne ©2007
Deadlock Avoidance: Safe State


When a proc requests an available resource, system must
decide if immediate allocation leaves the system in a safe
state.
System is in safe state if there exists a sequence <P1, P2,
…, Pn> of ALL the procs 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.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.26
Silberschatz, Galvin and Gagne ©2007
Safe, Unsafe Deadlock State
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.27
Silberschatz, Galvin and Gagne ©2007
Deadlock Avoidance: 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.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.28
Silberschatz, Galvin and Gagne ©2007
Dijkstra’s Banker’s Algorithm


5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), 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
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
433
7.38
Silberschatz, Galvin and Gagne ©2007
Dijkstra’s Banker’s Algorithm (Cont.)

The content of the matrix Need is defined to be
Max – Allocation.
P0
P1
P2
P3
P4

Need
ABC
743
122
600
011
431
The system is in a safe state since the sequence
< P1, P3, P4, P2, P0> satisfies safety criteria.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.39
Silberschatz, Galvin and Gagne ©2007
Example: P1 Request (1,0,2)




Check that Request  Available (that is, (1,0,2)  (3,3,2)  true.
Allocation
Need
Available
ABC
ABC
ABC
P0
010
743
230
P1
302
020
P2
301
600
P3
211
011
P4
002
431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.40
Silberschatz, Galvin and Gagne ©2007
Deadlock Detection



Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.41
Silberschatz, Galvin and Gagne ©2007
Single Instance of Each Resource Type



Maintain wait-for graph
 Nodes are processes.
 Pi  Pj if Pi is waiting for Pj.
Periodically invoke an algorithm that
searches for a cycle in the graph. If there is
a cycle, there exists a deadlock.
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.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.42
Silberschatz, Galvin and Gagne ©2007
Detection Algorithm



Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
Snapshot at time T0:
Allocation Request Available
ABC
ABC
ABC
P0 0 1 0
000
000
P1 2 0 0
202
P2 3 0 3
000
P3 2 1 1
100
P4 0 0 2
002
Sequence <P0, P2, P3, P1, P4> shows that this is a safe
state [will result in Finish[i] = true for all i in the Banker’s
algorithm].
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.47
Silberschatz, Galvin and Gagne ©2007
Detection Algorithm (Cont.)


But: P2 requests an additional instance of type C:
Allocation Request
Available
ABC
ABC
ABC
P0
010
000
000
P1
200
202
P2
303
001
P3
211
100
P4
002
002
State of system?
 Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests.
 Deadlock! Procs P1, P2, P3, and P4.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.48
Silberschatz, Galvin and Gagne ©2007
Detection-Algorithm Usage


When, and how often, to invoke depends on:
 How often a deadlock is likely to occur?
 How many processes will need to be rolled
back?
 one for each disjoint cycle
There may be many cycles in the resource
graph, so we don’t expect to be able to tell which
of the many deadlocked procs “caused” the
deadlock.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.49
Silberschatz, Galvin and Gagne ©2007
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 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 with Java – 7th Edition, Nov 15, 2006
7.50
Silberschatz, Galvin and Gagne ©2007
Recovery from Deadlock: Resource Preemption



Selecting a victim – minimize some “cost.”
Rollback – return to some safe state, restart
proc for that state.
Starvation – same process may always be
picked as victim, so include number of
rollbacks in the cost factor.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
7.51
Silberschatz, Galvin and Gagne ©2007
End of Chapter 7
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Silberschatz, Galvin and Gagne ©2007