U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles...
-
Upload
javen-mullis -
Category
Documents
-
view
214 -
download
0
Transcript of U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles...
![Page 1: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/1.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Computer Systems PrinciplesDeadlock
Emery Berger and Mark CornerUniversity of Massachusetts
Amherst
![Page 2: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/2.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 2
thread A
printer->wait();
disk->wait();
thread B
disk->wait();
printer->wait();
Deadlocks Deadlock = condition where two
threads/processes wait on each other
![Page 3: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/3.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Example: Dining Philosophers Another gift from Dijkstra
![Page 4: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/4.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 4
Deadlocks - Terminology Deadlock
Deadlock prevention algorithms– Check resource requests & availability
Deadlock detection– Finds instances of deadlock when threads stop
making progress– Tries to recover
![Page 5: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/5.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 5
Rules for Deadlock All necessary and none sufficient
![Page 6: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/6.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 6
Rules for Deadlock All necessary and none sufficient Finite resource
– Resource can be exhausted causing waiting
![Page 7: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/7.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 7
Rules for Deadlock All necessary and none sufficient Finite resource
– Resource can be exhausted causing waiting Hold and wait
– Hold resource while waiting for another
![Page 8: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/8.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 8
Rules for Deadlock All necessary and none sufficient Finite resource
– Resource can be exhausted causing waiting Hold and wait
– Hold resource while waiting for another No preemption
– Thread can only release resource voluntarily– No other thread or OS can force thread to release
![Page 9: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/9.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 9
Rules for Deadlock All necessary and none sufficient Finite resource
– Resource can be exhausted causing waiting Hold and wait
– Hold resource while waiting for another No preemption
– Thread can only release resource voluntarily– No other thread or OS can force thread to release
Circular wait– Circular chain of waiting threads
![Page 10: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/10.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 10
Circular Waiting If no way to free resources (preemption)
![Page 11: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/11.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 11
Deadlock Detection Define graph with vertices:
– Resources = {r1, …, rm}– Threads = {t1, …, tn}
Request edge from thread to resource– (ti → rj)
Assignment edge from resource to thread – (rj → ti)– OS has allocated resource to thread
Result:– No cycles no deadlock– Cycle may deadlock
![Page 12: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/12.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 12
Example Deadlock or not? Request edge from
thread to resource ti -> rj– Thread: requested
resource but not acquired it (waiting)
Assignment edge from resource to thread rj -> ti– OS has allocated resource
to thread
![Page 13: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/13.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 13
Quick Exercise Draw a graph for the
following event: Request edge from
thread to resource – ti -> rj
Assignment edge from resource to thread – rj -> ti
![Page 14: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/14.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 14
Resource Allocation Graph Draw a graph for the following event:
![Page 15: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/15.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 15
Detecting Deadlock Scan resource allocation graph for cycles
– Then break them! Different ways to break cycles:
– Kill all threads in cycle– Kill threads one at a time
• Force to give up resources– Preempt resources one at a time
• Roll back thread state to before acquiring resource• Common in database transactions
![Page 16: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/16.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 16
Deadlock Prevention Instead of detection, ensure at least one of
necessary conditions doesn’t hold– Mutual exclusion– Hold and wait– No preemption– Circular wait
![Page 17: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/17.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 17
Deadlock Prevention Mutual exclusion
– Make resources shareable (but not all resources can be shared)
Hold and wait– Guarantee that thread cannot hold one
resource when it requests another– Make threads request all resources they need
first and release all before requesting more
![Page 18: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/18.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 18
Deadlock Prevention, continued No preemption
– If thread requests resource that cannot be immediately allocated to it
• OS preempts (releases) all resources thread currently holds
– When all resources available:• OS restarts thread
Not all resources can be preempted!
![Page 19: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/19.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 19
Deadlock Prevention, continued Circular wait
– Impose ordering (numbering) on resources and request them in order
– Most important trick to correct programming with locks!
![Page 20: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/20.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 20
Avoiding Deadlock Cycle in locking graph = deadlock Typical solution: canonical order for locks
– Acquire in increasing order• E.g., lock_1, lock_2, lock_3
– Release in decreasing order
Ensures deadlock-freedom– but not always easy to do
![Page 21: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/21.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 21
lock (a);lock (b);unlock (b);unlock (a);
lock (b);lock (a);unlock (a);unlock (b);
Avoiding Deadlock Avoiding deadlock: is this ok?
![Page 22: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/22.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 22
lock (a);lock (b);unlock (b);unlock (a);
lock (b);lock (a);unlock (a);unlock (b);
lock (a);lock (b);unlock (b);unlock (a);
lock (a);lock (b);unlock (b);unlock (a);
Avoiding Deadlock Not ok – may deadlock.
Solution: impose canonical order (acyclic)
![Page 23: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/23.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
![Page 24: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/24.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 24
Deadlocks, Example II
![Page 25: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/25.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 25
Multiple Resources What if there are multiple interchangeable
instances of a resource?– Cycle deadlock might exist– If any instance held by thread outside cycle,
progress possible when thread releases resource
![Page 26: U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.](https://reader030.fdocuments.us/reader030/viewer/2022032517/56649ca35503460f94963223/html5/thumbnails/26.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 26
Deadlock Detection Deadlock or not?