Professional Documents
Culture Documents
Chapter 3
Overview
Resources
Why do deadlocks occur?
Dealing with deadlocks
Ignoring them: ostrich algorithm
Detecting & recovering from deadlock
Avoiding deadlock
Preventing deadlock
Chapter 3 2
Resources
Chapter 3 3
When do deadlocks happen?
Suppose
Process 1 holds resource A
and requests resource B Process 1 Process 2
Process 2 holds B and
requests A A
Both can be blocked, with
neither able to proceed B
Deadlocks occur when …
Processes are granted A
exclusive access to devices or
software constructs B
(resources)
Each deadlocked process
needs a resource held by
another deadlocked process
DEADLOCK!
Chapter 3 4
Using resources
Chapter 3 5
What is a deadlock?
Formal definition:
“A set of processes is deadlocked if each process in
the set is waiting for an event that only another
process in the set can cause.”
Usually, the event is release of a currently held
resource
In deadlock, none of the processes can
Run
Release resources
Be awakened
Chapter 3 6
Four conditions for deadlock
Mutual exclusion
Each resource is assigned to at most one process
Hold and wait
A process holding resources can request more resources
No preemption
Previously granted resources cannot be forcibly taken
away
Circular wait
There must be a circular chain of 2 or more processes
where each is waiting for a resource held by the next
member of the chain
Chapter 3 7
Resource allocation graphs: Introduction
Resource allocation
A B modeled by directed graphs
Example 1:
Resource R assigned to
R S process A
Example 2:
Process B is requesting /
waiting for resource S
T
Example 3:
Process C holds T, waiting
C D for U
Process D holds U, waiting
U for T
C and D are in deadlock!
Chapter 3 8
Dealing with deadlock
Chapter 3 9
Getting into deadlock
A B C
Acquire R Acquire S Acquire T
Acquire S Acquire T Acquire R
Release R Release S Release T
Release S Release T Release R
A B C A B C A B C
R S T R S T R S T
Acquire R Acquire S Acquire T
A B C A B C A B C
R S T R S T R S T
Deadlock!
Acquire S Acquire T
Acquire R
Chapter 3 10
Not getting into deadlock…
Chapter 3 11
The Ostrich Algorithm
Chapter 3 12
Detecting deadlocks using graphs
Chapter 3 14
Resources with multiple instances
Chapter 3 15
Basic Facts
Multiple instances
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
Chapter 3 26
Chapter 3 27
Example: P1 Request (1,0,2)
If Requesti Needi then goto step-2 else ERROR
Check that Request Available (that is, (1,0,2) (3,3,2)
true
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 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?
Resource-Request Algorithm for Process Pi
Detection algorithm
Recovery scheme
Several Instances of a Resource Type
current=avail;
A B C D
for (j = 0; j < N; j++) {
Avail 2 3 0 1 for (k=0; k<N; k++) {
if (finished[k])
continue;
Process A B C D if (want[k] < current) {
1 0 3 0 0 finished[k] = 1;
Hold
current += hold[k];
2 1 0 1 1 break;
3 0 2 1 0 }
4 2 2 3 0 if (k==N) {
printf “Deadlock!\n”;
Process A B C D // finished[k]==0 means process is in
// the deadlock
1 3 2 1 0 break;
Want
2 2 2 0 0 }
}
3 3 5 3 1
4 0 4 1 1 Note: want[j],hold[j],current,avail are arrays!
Chapter 3 37
Recovering from deadlock
Chapter 3 38
Recovery from Deadlock: Process Termination
A 3 9 A 3 9 A 3 9 A 3 9 A 3 9
B 2 4 B 4 4 B 0 - B 0 - B 0 -
C 2 7 C 2 7 C 2 7 C 7 7 C 0 -
Free: 3 Free: 1 Free: 5 Free: 0 Free: 7
Demonstration that the first state is safe
A 3 9 A 4 9 A 4 9 A 4 9
B 2 4 B 2 4 B 4 4 B 0 -
C 2 7 C 2 7 C 2 7 C 2 7
Free: 3 Free: 2 Free: 0 Free: 4
Demonstration that the second state is unsafe
Chapter 3 42
Banker's Algorithm for a single resource
Has Max Has Max Has Max
A 0 6 A 1 6 A 1 6
B 0 5 B 1 5 B 2 5
C 0 4 C 2 4 C 2 4
D 0 7 D 4 7 D 4 7
Free: 10 Free: 2 Free: 1
Any sequence finishes C,B,A,D finishes Deadlock (unsafe state)
Chapter 3 44
Preventing deadlock
Chapter 3 45
Eliminating mutual exclusion
Chapter 3 46
Attacking “hold and wait”
Chapter 3 47
Attacking “no preemption”
This is not usually a viable option
Consider a process given the printer
Halfway through its job, take away the printer
Confusion ensues!
May work for some resources
Forcibly take away memory pages, suspending the process
Process may be able to resume with no ill effects
Chapter 3 48
Attacking “circular wait”
Assign an order to
resources
Always acquire resources in D
numerical order
Need not acquire them all at
once! C
Circular wait is prevented 3 2
A process holding resource n
can’t wait for resource m B
if m < n
1
No way to complete a cycle
Place processes above the
highest resource they hold A
and below any they’re
requesting
All arrows point up!
Chapter 3 49
Deadlock prevention: summary
Mutual exclusion
Spool everything
Hold and wait
Request all resources initially
No preemption
Take resources away
Circular wait
Order resources numerically
Chapter 3 50
Example: two-phase locking
Phase One
Process tries to lock all data it needs, one at a time
If needed data found locked, start over
(no real work done in phase one)
Phase Two
Perform updates
Release locks
Note similarity to requesting all resources at once
This is often used in databases
It avoids deadlock by eliminating the “hold-and-
wait” deadlock condition
Chapter 3 51
“Non-resource” deadlocks
Chapter 3 52
Starvation
Chapter 3 53