1 15-410, s16 deadlock (1) dave eckhardt todd c. mowry roger dannenberg garth gibson bruce maggs...
DESCRIPTION
, S’16 Debugging Reminder We can't really help with queries like: We did x... then something strange happened... ...can you tell us why? You need to progress beyond “something happened” What happened, exactly? printf() is not always the right tool output correct only if run-time environment is right captures only what you told it to, only “C-level” stuff changes your code by its mere presence!!! We're serious about examining register dumps! Overall, maybe re-read “Debugging” lecture notesTRANSCRIPT
![Page 1: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/1.jpg)
1 15-410, S’16
Deadlock (1)
Dave EckhardtTodd C. Mowry
Roger DannenbergGarth GibsonBruce Maggs
Geoff Langdale
L12_Deadlock
![Page 2: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/2.jpg)
2 15-410, S’16
Synchronization – P2
You should really have, today: Figured out where wrappers belong, why Made some system calls Designed mutexes & condition variables Drawn pictures of thread stacks (even if not perfect)
Wednesday: Coded mutexes and condition variables Thoughtful design for thr_create(), maybe thr_join() Some code for thr_create(), and some “experience” The startle test running
![Page 3: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/3.jpg)
3 15-410, S’16
Debugging Reminder
We can't really help with queries like: We did x... then something strange happened... ...can you tell us why?
You need to progress beyond “something happened” What happened, exactly? printf() is not always the right tool
output correct only if run-time environment is right captures only what you told it to, only “C-level” stuff changes your code by its mere presence!!!
We're serious about examining register dumps! Overall, maybe re-read “Debugging” lecture notes
![Page 4: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/4.jpg)
4 15-410, S’16
Synchronization – Readings
Next three lectures OSC – Deadlock: 6.5.3, 6.6.3, Chapter 7 OS:P+P – Advanced Synchronization: Chapter 6
Reading ahead Virtual Memory (continued) Scheduling
Don't forget about reading list on web site
![Page 5: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/5.jpg)
5 15-410, S’16
Outline
Process resource graph What is deadlock? Deadlock prevention Next time
Deadlock avoidance Deadlock recovery
![Page 6: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/6.jpg)
6 15-410, S’16
Tape Drives
A word on “tape drives” Ancient computer resources Access is sequential, read/write Any tape can be mounted on any drive One tape at a time is mounted on a drive
Doesn't make sense for multiple processes to simultaneously access a drive
Reading/writing a tape takes a while Think “Blu-ray burner”...
IBM 3420 (1970-1987)www.ibm.com/ibm/historyNot for publication
Data General 6023wps.com/NOVA4
![Page 7: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/7.jpg)
7 15-410, S’16
Process/Resource graph
Tape 1
P1
Tape 2
P2
Tape 3
P3
Request
![Page 8: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/8.jpg)
8 15-410, S’16
Process/Resource graph
Tape 1
P1
Tape 2
P2
Tape 3
P3
Allocation
![Page 9: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/9.jpg)
9 15-410, S’16
Waiting
Tape 1
P1
Tape 2
P2
Tape 3
P3
![Page 10: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/10.jpg)
10 15-410, S’16
Release
Tape 1
P1
Tape 2
P2
Tape 3
P3
![Page 11: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/11.jpg)
11 15-410, S’16
Reallocation
Tape 1
P1
Tape 2
P2
Tape 3
P3
![Page 12: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/12.jpg)
12 15-410, S’16
Multi-instance Resources
P1 P2 P3
Tapes Disks
![Page 13: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/13.jpg)
13 15-410, S’16
Definition of Deadlock
A deadlock Set of N processes Each waiting for an event
...which can be caused only by another process in the set Every process will wait forever
![Page 14: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/14.jpg)
14 15-410, S’16
Deadlock Examples
Simplest form Process 1 owns printer, wants tape drive Process 2 owns tape drive, wants printer
Less-obvious Three tape drives Three processes
Each has one tape drive Each wants “just” one more
Can't blame anybody, but problem is still there
![Page 15: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/15.jpg)
15 15-410, S’16
Deadlock Requirements
Mutual Exclusion Hold & Wait No Preemption Circular Wait
![Page 16: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/16.jpg)
16 15-410, S’16
Mutual Exclusion
Resources aren't “thread-safe” (“reentrant”) Must be allocated to one process/thread at a time Can't be shared
Programmable Interrupt Timer Can't have a different reload value for each process
![Page 17: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/17.jpg)
17 15-410, S’16
Hold & Wait
Process holds some resources while waiting for moremutex_lock(&m1);mutex_lock(&m2);mutex_lock(&m3);
This locking behavior is typical
![Page 18: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/18.jpg)
18 15-410, S’16
No Preemption
Can't force a process to give up a resource Interrupting a CD-R burn creates a “coaster”
So don't do that Obvious solution
CD-R device driver forbids second simultaneous open() If you can't open it, you can't pre-empt it...
![Page 19: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/19.jpg)
19 15-410, S’16
Circular Wait
Process 0 needs something process 4 has Process 4 needs something process 7 has Process 7 needs something process 1 has Process 1 needs something process 0 has – uh-oh...
Described as “cycle in the resource graph”
![Page 20: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/20.jpg)
20 15-410, S’16
Cycle in Resource Graph
Tape 2
P1
Tape 1
P2
Tape 3
P3
![Page 21: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/21.jpg)
21 15-410, S’16
Deadlock Requirements
Mutual Exclusion Hold & Wait No Preemption Circular Wait Each deadlock requires all four
![Page 22: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/22.jpg)
22 15-410, S’16
Multi-Instance Cycle
P3P2P1
Tapes Disks
![Page 23: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/23.jpg)
23 15-410, S’16
Multi-Instance Cycle (With Rescuer!)
P3P2P1
Tapes Disks
![Page 24: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/24.jpg)
24 15-410, S’16
Cycle Broken
P3P2P1
Tapes Disks
![Page 25: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/25.jpg)
25 15-410, S’16
Dining Philosophers
The scene 410 staff members at a Chinese restaurant A little short on utensils
![Page 26: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/26.jpg)
26 15-410, S’16
Dining Philosophers
ALGH
DE
JLPD
![Page 27: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/27.jpg)
27 15-410, S’16
Dining Philosophers
Processes 5, one per person
Resources 5 bowls
(dedicated to a diner: no contention: ignore) 5 chopsticks
1 between every adjacent pair of diners Contrived example?
Illustrates contention, starvation, deadlock
![Page 28: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/28.jpg)
28 15-410, S’16
Dining Philosophers
A simple rule for eating Wait until the chopstick to your right is free; take it Wait until the chopstick to your left is free; take it Eat for a while Put chopsticks back down
![Page 29: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/29.jpg)
29 15-410, S’16
Dining Philosophers Deadlock
Everybody reaches right... ...at the same time?
![Page 30: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/30.jpg)
30 15-410, S’16
Reaching Right
ALGH
DE
JLPD
![Page 31: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/31.jpg)
31 15-410, S’16
Successful Acquisition
ALGH
DE
JLPD
![Page 32: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/32.jpg)
32 15-410, S’16
Deadlock!
ALGH
DE
JLDE
![Page 33: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/33.jpg)
33 15-410, S’16
Dining Philosophers – State
int stick[5] = { -1 }; /* owner */condition avail[5]; /* newly avail. */mutex table = { available };
/* Right-handed convention */right = diner; /* 3 ⇒ 3 */
left = (diner + 4) % 5; /* 3 ⇒ 7 ⇒ 2 */
![Page 34: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/34.jpg)
34 15-410, S’16
start_eating(int diner)
mutex_lock(table);
while (stick[right] != -1) condition_wait(avail[right], table);stick[right] = diner;
while (stick[left] != -1) condition_wait(avail[left], table);stick[left] = diner;
mutex_unlock(table);
![Page 35: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/35.jpg)
35 15-410, S’16
done_eating(int diner)
mutex_lock(table);
stick[left] = stick[right] = -1;condition_signal(avail[right]);condition_signal(avail[left]);
mutex_unlock(table);
![Page 36: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/36.jpg)
36 15-410, S’16
Can We Deadlock?
At first glance the table mutex protects us Can't have “everybody reaching right at same time”... ...mutex means only one person can access table... ...so allows only one reach at the same time, right?
![Page 37: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/37.jpg)
37 15-410, S’16
Can We Deadlock?
At first glance the table mutex protects us Can't have “everybody reaching right at same time”... ...mutex means only one person can access table... ...so allows only one reach at the same time, right?
Maybe we can! condition_wait() is a “reach” Can everybody end up in condition_wait()?
![Page 38: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/38.jpg)
38 15-410, S’16
First diner gets both chopsticks
![Page 39: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/39.jpg)
39 15-410, S’16
Next gets right, waits on left
![Page 40: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/40.jpg)
40 15-410, S’16
Next two get right, wait on left
![Page 41: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/41.jpg)
41 15-410, S’16
Last waits on right
![Page 42: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/42.jpg)
42 15-410, S’16
First diner stops eating - briefly
![Page 43: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/43.jpg)
43 15-410, S’16
First diner stops eating - briefly
signal()
![Page 44: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/44.jpg)
44 15-410, S’16
Next Step – One Possibility
“Natural” –longest-waiting diner progresses
![Page 45: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/45.jpg)
45 15-410, S’16
Next Step – Another Possibility
Or –somebody else!
![Page 46: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/46.jpg)
46 15-410, S’16
Last diner gets right, waits on left
![Page 47: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/47.jpg)
47 15-410, S’16
First diner gets right, waits on left
![Page 48: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/48.jpg)
48 15-410, S’16
Now things get boring
![Page 49: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/49.jpg)
49 15-410, S’16
Deadlock - What to do?
Prevention Avoidance Detection/Recovery Just reboot when it gets “too quiet”
![Page 50: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/50.jpg)
50 15-410, S’16
1: Prevention
Restrict behavior or resources Find a way to violate one of the 4 conditions
To wit...? What we will talk about today
4 conditions, 4 possible ways
![Page 51: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/51.jpg)
51 15-410, S’16
2: Avoidance
Processes pre-declare usage patterns Dynamically examine requests
Imagine what other processes could ask for Keep system in “safe state”
![Page 52: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/52.jpg)
52 15-410, S’16
3: Detection/Recovery
Maybe deadlock won't happen today... ...Hmm, it seems quiet... ...Oops, here is a cycle... Abort some process
Ouch!
![Page 53: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/53.jpg)
53 15-410, S’16
4: Reboot When It Gets “Too Quiet”
Which systems would be so simplistic?
![Page 54: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/54.jpg)
54 15-410, S’16
Four Ways to Forgiveness
Each deadlock requires all four Mutual Exclusion Hold & Wait No Preemption Circular Wait
“Deadlock Prevention” - this is a technical term Pass a law against one (pick one) Deadlock happens only if somebody transgresses!
![Page 55: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/55.jpg)
55 15-410, S’16
Outlaw Mutual Exclusion?
Approach: ban single-user resources Require all resources to “work in shared mode”
Problem Chopsticks??? Many resources don't work that way
![Page 56: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/56.jpg)
56 15-410, S’16
Outlaw Hold&Wait?
Acquire resources all-or-nonestart_eating(int diner)
mutex_lock(table);while (1) if (stick[lt] == stick[rt] == -1) stick[lt] = stick[rt] = diner mutex_unlock(table) return; condition_wait(released, table);
![Page 57: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/57.jpg)
57 15-410, S’16
Problems
“Starvation” Larger resource set makes grabbing everything harder
No guarantee a diner eats in bounded time Low utilization
Larger peak resource needs hurts whole system always Must allocate 2 chopsticks (and waiter!) Nobody else can use waiter while you eat
![Page 58: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/58.jpg)
58 15-410, S’16
Outlaw Non-preemption?
Steal resources from sleeping processes!start_eating(int diner)right = diner; rright = (diner+1)%5;mutex_lock(table);while (1) if (stick[right] == -1) stick[right] = diner else if (stick[rright] != rright) /* right person can't be eating: take! */ stick[right] = diner;...same for left...wait() if must...mutex_unlock(table);
![Page 59: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/59.jpg)
59 15-410, S’16
Problem
Some resources cannot be cleanly preempted CD burner
![Page 60: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/60.jpg)
60 15-410, S’16
Outlaw Circular Wait?
Impose total order on all resources Require acquisition in strictly increasing order
Static order may work: allocate memory, then files Dynamic – may need to “start over” sometimes
Traversing a graph lock(4), visit(4) /* 4 has an edge to 13 */ lock(13), visit(13) /* 13 has an edge to 0 */ lock(0)?
Nope! unlock(4), unlock(13) lock(0), lock(4), lock(13), ...
![Page 61: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/61.jpg)
61 15-410, S’16
Assigning Diners a Total Order
Lock order: 4, 3, 2, 1, 0 ≡ right chopstick, then left Diner 4 ⇒ lock(4); lock(3); Diner 3 ⇒ lock(3); lock(2);
![Page 62: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/62.jpg)
62 15-410, S’16
Assigning Diners a Total Order
Lock order: 4, 3, 2, 1, 0 ≡ right chopstick, then left Diner 4 ⇒ lock(4); lock(3); Diner 3 ⇒ lock(3); lock(2); Diner 0 ⇒ lock(0); lock(4); /* violates lock order! */
Requires special-case locking code to get order rightif diner == 0 right = (diner + 4) % 5; left = diner;else right = diner; left = (diner + 4) % 5;...
![Page 63: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/63.jpg)
63 15-410, S’16
Problem
May not be possible to force allocation order Some trains go east, some go west
“The Last Spike”reflectivelens.blogspot.com
2011-06-12
![Page 64: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/64.jpg)
64 15-410, S’16
Deadlock Prevention problems
Typical resources require mutual exclusion All-at-once allocation can be painful
Hurts efficiency May starve Resource needs may be unpredictable
Preemption may be impossible Or may lead to starvation
Ordering restrictions may be impractical
![Page 65: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/65.jpg)
65 15-410, S’16
Deadlock Prevention
Pass a law against one of the four ingredients Great if you can find a tolerable approach
Very tempting to just let processes try their luck
![Page 66: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/66.jpg)
66 15-410, S’16
Deadlock is not...
...a simple synchronization bug Deadlock remains even when those are cleaned up Deadlock is a resource usage design problem
...the same as starvation Deadlocked processes don't ever get resources Starved processes don't ever get resources Deadlock is a “progress” problem; starvation is a
“bounded waiting” problem ....that “after-you, sir” dance in the corridor
That's “livelock” – continuous changes of state without forward progress
![Page 67: 1 15-410, S16 Deadlock (1) Dave Eckhardt Todd C. Mowry Roger Dannenberg Garth Gibson Bruce Maggs Geoff Langdale L12_Deadlock](https://reader033.vdocuments.us/reader033/viewer/2022052711/5a4d1b8d7f8b9ab0599bfcf9/html5/thumbnails/67.jpg)
67 15-410, S’16
Next Time
Deadlock Avoidance Deadlock Recovery