790 likes | 924 Views
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
E N D
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 • Examples of computer resources • Printers, tape drives, tables, • locks, disk space, memory, CPU
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
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!
Resources • Sequence of events required to use a resource • request the resource • use the resource • release the resource
Resources • Must wait if request is denied • requesting process may be blocked • may fail with error code
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
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
Deadlocks • In a deadlock, none of the processes can … • run • release resources • be awakened
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
Deadlocks • Example thread A thread B lock(x) lock(y) lock(y) lock(x) ... ... unlock(y) unlock(x) unlock(x) unlock(y)
Deadlocks • Can deadlock occur with code? • Will deadlock always occur with this code?
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
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?
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
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
Four Conditions for Deadlock • The first condition is sometimes called • Mutual exclusion condition • each resource assigned to 1 process or is available • Tanebaum, etc.
Deadlock Modeling • Modeled with directed graphs
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
How deadlock can be avoided (o) (p) (q)
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
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
Detect and Fix • Note the resource ownership and requests • Detect by looking for cycles in the wait-for graph • How to fix once detected?
Detection with Wait-for Graph An example for the deadlock detection algorithm
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
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
Deadlock Avoidance • Don’t get in a position where • Deadlock becomes possible • Use: • Resource Trajectories
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
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
Deadlock Prevention • Idea is to eliminate one of the four necessary conditions • Increase resources to decrease waiting • this minimizes chance of deadlock
Deadlock Prevention • Attacking the Mutual Exclusion Condition • Attacking the Hold and Wait Condition • Attacking the No Preemption Condition • Attacking the Circular Wait Condition
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
Attacking Mutual Exclusion • Principle: • Avoid assigning resource when not absolutely necessary • As few processes as possible actually claim the resource
Attacking Hold & Wait Condition • Processes to request resources before starting • a process never has to wait for what it needs • Eliminate hold and wait
Attacking Hold & Wait Condition • phase 1a. acquire all resources • phase 1b. while (not done) { • acquire some resource • work • } • phase 2. release all resources
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
Attacking Hold & Wait Condition • Problems? • May not know required resources at start of run • Ties up resources others could be using
Attacking Hold & Wait Condition • Variation: • Process must give up all resources • Then request all immediately needed
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?
Attacking No Preemption Condition • Some resource cannot be preempted • Consider a process given the printer • halfway through its job • now forcibly take away printer • !!??
Attacking Circular Wait Condition • Normally ordered resources • Imagesetter scanner plotter printer • A resource graph
Banker’s Algorithm • Derive from methods by bankers to grant loans • Similar to reserving all resources at beginning • but more efficient
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
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
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