1 / 79

Chapter 3: Deadlocks

Chapter 3: Deadlocks. Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity 求于至简,归于永恒. Content. Resource Deadlocks Deadlock modeling Deadlock detection and recovery Deadlock avoidance and prevention. Resources. Something needed by a thread A thread waits for resources

duard
Download Presentation

Chapter 3: Deadlocks

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. Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity求于至简,归于永恒

  2. Content • Resource • Deadlocks • Deadlock modeling • Deadlock detection and recovery • Deadlock avoidance and prevention

  3. Resources • Something needed by a thread • A thread waits for resources • Examples of computer resources • Printers, tape drives, tables, • locks, disk space, memory, CPU

  4. Resources • Processes need access to resources • in reasonable order • Preemptable resources • can be taken away from a process with no ill effects • Nonpreemptable resources • will cause the process to fail if taken away

  5. Resources • Suppose a process holds resource A • and requests resource B • At same time another process holds B • and requests A • both are blocked and remain so • Deadlock!

  6. Resources • Sequence of events required to use a resource • request the resource • use the resource • release the resource

  7. Resources • Must wait if request is denied • requesting process may be blocked • may fail with error code

  8. Deadlocks • A circular waiting for resources • leading to threads involved not being able to make progress • Deadlocks occur when… • processes are granted exclusive access to resources

  9. Deadlocks • 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 held resource

  10. Deadlocks • In a deadlock, none of the processes can … • run • release resources • be awakened

  11. Deadlocks • General structure of thread code • phase 1. while (not done) { • acquire some resources • work • } • phase 2. release all resources • Assume phase 1 has finite amount of work

  12. Deadlocks • Example thread A thread B lock(x) lock(y) lock(y) lock(x) ... ... unlock(y) unlock(x) unlock(x) unlock(y)

  13. Deadlocks • Can deadlock occur with code? • Will deadlock always occur with this code?

  14. Dining Philosophers Problem

  15. Dining Philosophers • 5 philosophers sitting around a round table • 1 chopstick in between each pair of philosophers • 5 chopsticks total • Each philosopher needs two chopsticks to eat • How to prevent deadlock

  16. Dining Philosophers • Algorithm for each philosopher • wait for chopstick on right to be free, then pick it up • wait for chopstick on left to be free, then pick it up • eat • put both chopsticks down • Can this deadlock?

  17. Conditions for Deadlock • 4 conditions must all be true for deadlock to occur • Limited resource: • not enough resources to serve all threads simultaneously • Hold and wait: • threads hold resources while waiting to acquire others

  18. Thread A Thread B Conditions for Deadlock • No preemption • can’t force thread to give up resource • Circular chain of requests Resource A Resource B

  19. Four Conditions for Deadlock • The first condition is sometimes called • Mutual exclusion condition • each resource assigned to 1 process or is available • Tanebaum, etc.

  20. Deadlock Modeling • Modeled with directed graphs

  21. Deadlock Modeling • Resource R assigned to process A • Process B is requesting/waiting for resource S • Process C and D are in deadlock • over resources T and U

  22. Deadlock Modeling

  23. How deadlock can be avoided (o) (p) (q)

  24. Strategies for dealing with Deadlocks • Ignore: just ignore the problem altogether • Detection and recovery • Dynamic avoidance: careful resource allocation • Prevention: • negating one of the four necessary conditions

  25. Ignore Strategy • Pretend there is no problem • Reasonable if • deadlocks occur very rarely • cost of prevention is high • UNIX and Windows takes this approach • a trade off between convenience & correctness

  26. Detect and Fix • Note the resource ownership and requests • Detect by looking for cycles in the wait-for graph • How to fix once detected?

  27. Detection with Wait-for Graph

  28. Detection with Wait-for Graph

  29. Detection with Wait-for Graph An example for the deadlock detection algorithm

  30. Recovery from Deadlock • Recovery through preemption • take a resource from some other process • depends on nature of the resource • Recovery through rollback • checkpoint a process periodically • use this saved state • restart the process if it is found deadlocked

  31. Recovery from Deadlock • Recovery through killing processes • crudest but simplest way to break a deadlock • kill one of the processes in the deadlock cycle • the other processes get its resources • choose process that can be rerun from the beginning

  32. Deadlock Avoidance • Don’t get in a position where • Deadlock becomes possible • Use: • Resource Trajectories

  33. Safe and Unsafe States • Demonstration that the state in (a) is safe Has Max Has Max Has Max Has Max (a) Free: 3 (b) Free: 1 (c) Free: 5 (d) Free: 0

  34. Safe and Unsafe States • Demonstration that the state in b is not safe Has Max Has Max Has Max Has Max (a) Free: 3 (b) Free: 2 (c) Free: 0 (d) Free: 4

  35. Deadlock Prevention • Idea is to eliminate one of the four necessary conditions • Increase resources to decrease waiting • this minimizes chance of deadlock

  36. Deadlock Prevention • Attacking the Mutual Exclusion Condition • Attacking the Hold and Wait Condition • Attacking the No Preemption Condition • Attacking the Circular Wait Condition

  37. Attacking Mutual Exclusion • Some devices (such as printer) can be spooled • only the printer daemon uses printer resource • thus deadlock for printer eliminated • Not all devices can be spooled

  38. Attacking Mutual Exclusion • Principle: • Avoid assigning resource when not absolutely necessary • As few processes as possible actually claim the resource

  39. Attacking Hold & Wait Condition • Processes to request resources before starting • a process never has to wait for what it needs • Eliminate hold and wait

  40. Attacking Hold & Wait Condition • phase 1a. acquire all resources • phase 1b. while (not done) { • acquire some resource • work • } • phase 2. release all resources

  41. Attacking Hold & Wait Condition • A. wait until all resources you’ll need are free, then grab them all at once • (or) B. if you find resource busy, release all acquired resources and go back to beginning

  42. Attacking Hold & Wait Condition • Problems? • May not know required resources at start of run • Ties up resources others could be using

  43. Attacking Hold & Wait Condition • Variation: • Process must give up all resources • Then request all immediately needed

  44. Attacking No Preemption Condition • Allow Preemption! • Can preempt CPU by saving its state to thread control block and resuming later • Can preempt memory by swapping memory out to disk and loading it back later • Can we preempt the holding of a lock?

  45. Attacking No Preemption Condition • Some resource cannot be preempted • Consider a process given the printer • halfway through its job • now forcibly take away printer • !!??

  46. Attacking Circular Wait Condition • Normally ordered resources • Imagesetter  scanner  plotter  printer • A resource graph

  47. Banker’s Algorithm • Derive from methods by bankers to grant loans • Similar to reserving all resources at beginning • but more efficient

  48. Banker’s Algorithm • State maximum resource needs in advance • but don’t actually acquire the resources • When thread later tries to acquire a resource, Banker’s algorithm determines: • when it’s safe to satisfy the request • and blocks the thread when it’s not safe

  49. Banker’s Algorithm • General structure of thread code • phase 1a. state maximum resource needed • phase 1b. while (not done) { • acquire some resources • work • } • phase 2. release all resources

  50. Banker’s Algorithm • Preventing deadlock by requesting all resources at beginning would block thread in phase 1a above • but phase 1b can proceed without waiting

More Related