1 / 26

Computer Systems Principles Deadlock

Computer Systems Principles Deadlock. Emery Berger and Mark Corner University of Massachusetts Amherst. Deadlocks. Deadlock = condition where two threads/processes wait on each other. thread A printer->wait(); disk->wait();. thread B disk->wait(); printer->wait();. 2.

abie
Download Presentation

Computer Systems Principles Deadlock

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Systems PrinciplesDeadlock Emery Berger and Mark Corner University of Massachusetts Amherst

  2. Deadlocks • Deadlock = condition where two threads/processes wait on each other thread A printer->wait(); disk->wait(); thread B disk->wait(); printer->wait(); 2

  3. Example: Dining Philosophers • Another gift from Dijkstra

  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 4

  5. Rules for Deadlock • All necessary and none sufficient 5

  6. Rules for Deadlock • All necessary and none sufficient • Finite resource • Resource can be exhausted causing waiting 6

  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 7

  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 8

  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 9

  10. Circular Waiting • If no way to free resources (preemption) 10

  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 11

  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 12

  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 13

  14. Resource Allocation Graph • Draw a graph for the following event: 14

  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 15

  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 16

  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 17

  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! 18

  19. Deadlock Prevention, continued • Circular wait • Impose ordering (numbering) on resources and request them in order • Most important trick to correct programming with locks! 19

  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 20

  21. Avoiding Deadlock • Avoiding deadlock: is this ok? lock (a); lock (b); unlock (b); unlock (a); lock (b); lock (a); unlock (a); unlock (b); 21

  22. Avoiding Deadlock • Not ok – may deadlock. • Solution: impose canonical order (acyclic) 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); 22

  23. Deadlocks, Example II 24

  24. 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 25

  25. Deadlock Detection Deadlock or not? 26

More Related