Professional Documents
Culture Documents
CS 241
University of Illinois
1
Announcement
2
Deadlock: definition
3
Deadlock in the real world
Which way
should I go?
4
Deadlock in the real world
I can almost
Drat!
get across
GRIDLOCK!
5
Deadlock: One-lane Bridge
6
Deadlock: One-lane Bridge
Deadlock
!
7
Deadlock: One-lane Bridge
Deadlock
!
! e.g., if the rule is that Westbound cars always go first when present
8
Deadlock: One-lane Bridge
I always have to
back up!
9
Deadlock solutions
Prevention
• Design system so that deadlock is impossible
Avoidance
• Steer around deadlock with smart scheduling
Do nothing
• Prevention, avoidance, and detection/recovery are expensive
• If deadlock is rare, is it worth the overhead?
• Manual intervention (kill processes, reboot) if needed
10
Deadlock Prevention
11
Deadlock prevention
12
Rule #1: No Mutual Exclusion
13
Rule #1: No Mutual Exclusion
14
Rule #2: Allow preemption
A lock can be taken away from current owner
• Let it go: If a process holding some resources is denied a further request,
that process must release its original resources
• Or take it all away: OS preempts current resource owner, gives resource to
new process/thread requesting it
15
Rule #3: No hold and wait
16
Rule #3: No hold and wait
17
Rule #4: request resources in order
18
Dining Philosophers solution with
unnumbered resources
Back to the trivial broken
“solution”...
# define N 5
# define N 5
# define N 5
# define N 5
# define N 5
# define N 5
# define N 5
Without numbering
Cycle!
26
Ordered resource requests prevent
deadlock
With numbering
4
3 8
7
Ordering violation:
Process holds 7,
is requesting 3
27
Proof by M.C. Escher
28
29
Summary: Deadlock prevention
methods
#1: No mutual exclusion
• Thank you, Captain Obvious
30
“Request In Order” is more permissive
All programs
Request in order
No hold
and wait
Definitely
No mutual deadlock
exclusion
31
Q: What’s the rule of the road?
I can almost
Drat!
get across
32
Summary
Deadlock prevention
• Imposes rules on what system can do
• These rules are conservative
• Most useful technique: ordered resources
• Application can do it; no special OS support
33
Deadlock Avoidance
34
Deadlock Avoidance
36
What is a state?
Pessimistic assumptions:
• A process might request its maximum resources at any time
• A process will never release its resources until it’s done
tes
All sta Unsafe
Safe Deadlocked
38
Computing safety
39
Computing safety
40
Inspiration
41
Playing Pickup Sticks with Processes
Remove stick
• = Process finshes & releases its resources
Repeat until...
• ...all processes have finished
# Answer: safe
• ...or we get stuck
# Answer: unsafe
42
Try it: is this state safe?
43
Try it: is this state safe?
44
Try it: is this state safe?
45
Try it: is this state safe?
46
Try it: is this state safe?
47
Try it: is this state safe?
48
Try it: is this state safe?
P2 need
49
Example 2: Is this state safe?
Buffer space
Free
Can P1 go first?
P1 alloc
Can P2 go first?
P2 alloc
Can P3 go first?
P3 alloc
P1 need
P2 need
P3 need
50
Example 2: Is this state safe?
Buffer space
Free
P1 alloc
P2 alloc
P3 alloc
P1 need
P2 need
P3 need
51
Example 2: Is this state safe?
Buffer space
Free
P1 alloc
P2 alloc
P3 alloc
P1 need
P2 need
P3 need
52
Example 2: Is this state safe?
Buffer space
Free
Can P1 go next?
P1 alloc
Can P2 go next?
P2 alloc
P3 alloc Unsafe!
P1 need
P2 need
P3 need
53