1 / 28

Deadlocks

Deadlocks. * Deadlock: definitions * Deadlock detection and recovery * Deadlock avoidance * Deadlock prevention. The Deadlock Problem.

ratana
Télécharger la présentation

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. Deadlocks * Deadlock: definitions * Deadlock detection and recovery * Deadlock avoidance * Deadlock prevention

  2. The Deadlock Problem • A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. • System has 2 disk drives. • P1 and P2 each hold one disk drive and each needs another one. • semaphores A and B, initialized to 1 P0P1 wait (A); wait (B) wait (B); wait (A)

  3. Deadlocks 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 currently held resource • Deadlock  None of the processes can … • run • release resources • be awakened

  4. Deadlock Characterization Deadlock can arise if four conditions hold simultaneously [Coffman ’71]. • Mutual exclusion: Each resource is either currently assigned to exactly one process or is available. • Hold and wait: A process holding at least one resource has requested and is waiting to acquire additional resources currently held by other processes. • No preemption: A resource cannot be acquired forcibly from a process holding it; can be released only voluntarily by the process holding it, after that process has completed its task. • Circular wait: A circular chain of >1 processes exits! There exists a set {P0, P1, …, Pn, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

  5. Deadlock Modeling • Modeled with directed graphs • resource R assigned to process A ( in-arrow to process) • process B is waiting (requesting) for resource S (out-arrow from process) • process C and D are in deadlock over resources T and U D has T, wants U C has U, wants T

  6. Deadlock Modeling A B C Deadlock occurrence: Strict Request Order

  7. Deadlock Modeling Deadlock avoided: Change of Request Order (o) (p) (q)

  8. Deadlock Issues Strategies for dealing with deadlocks • Just ignore the problem altogether (…and it will go away) • Detection and recovery (let it happen, detect and recover) • Dynamic avoidance (avoid, by design) - careful resource allocation • Prevention - negating one of the four necessary conditions

  9. The Windows (n’ Unix) Way  • Pretend there is no problem (and it will go away …)! • Reasonable if • cost of prevention is high • deadlocks occur very rarely (and may possibly go away with blind random back off for delayed services) • Trade off between • convenience • correctness (repeatable, deterministic)

  10. Detection with One Resource of Each Type • Note the resource ownership and requests • If a cycle can be found within the graph  deadlock • Set up DS (say DFS tree) where cycle is visible by node repetition in DS T

  11. Detection with Multiple Resource of Each Type m Residual Proc. 1 holds C1j resources and requests R1j resources Resource is either allocated or available

  12. Detection with Multiple Resource of Each Type Rowi ≤ Rowj iff each element(Rowi) ≤ element(Rowj) e.g., ( 2 1 0 0) ≤ (2 2 0 0) or (2 1 0 0) Find R (Rowi )≤ A  enough resources available to continue New A = (2 2 2 0) Q: How often to check for free resources? Expensive!

  13. 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 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 and does not hold up the alive processes!

  14. Deadlock Avoidance Requires that the system has some additional a priori information available. • Simplest and most useful model requires that each process a priori declare the maximum number of resources of each type that it may need. • The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition (safe versus unsafe scenarios). • Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

  15. Deadlock AvoidanceResource Trajectories Printer access by both Shaded area  ME condition prevents entry there. Thus, if trajectory goes into Box(I1,I2,I6,I5) it only leads to deadlock  Box @t indicates “unsafe” state. Plotter access by both Process B executing Process A executing

  16. Safe and Unsafe States Available Resources = 10(Maximal Concurrent Req: 20) (a) (b) (c) (d) (e) Safe access path starting at (a)

  17. Safe and Unsafe States Unsafe access starting from (b) (a) (b) (c) (d)

  18. The Banker's Algorithm for a Single Resource credit (a) safe (b) safe (c)

  19. Banker's Algorithm for Multiple Resources • Look for a row in R, whose unmet resource needs are all smaller than or equal to A (available resource vector). If no such row exists, the system will eventually deadlock since no process can run to completion. • Assume the process of the row chosen requests all the resources it needs (which is guaranteed to be possible) and finishes. Mark that process as terminated and add all its resources to the vector A. • Repeat steps 1 and 2 until either all processes are marked terminated, in which case the initial state was safe, or until a deadlock occurs, in which case it was not.

  20. Banker's Algorithm for Multiple Resources Is this algorithm in practice useful?

  21. Deadlock Prevention • Mutual Exclusion • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. • No Preemption • Circular Wait Restrain the ways request can be made.

  22. Deadlock PreventionAttacking the Mutual Exclusion Condition • Mutual Exclusion – not required for shareable resources (RO files); must hold for non-shareable resources. • Some devices (such as printer) can be spooled • only the (single) printer daemon uses printer resource (no contention!) • thus deadlock for printer eliminated • Con: not all devices can be spooled (spooler contention deadlock?) • Principle: • avoid assigning resource when not absolutely necessary • as few processes as possible actually claim the resource

  23. Attacking the Hold and Wait Condition • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. • Require process to request and be allocated all its resources before it begins execution (no wait), or allow process to request resources only when the process has none. • Low resource utilization; starvation possible. • Problems • may not know required resources at start of run • also ties up resources other processes could be using • Variation: • process must give up all resources • then request all immediately needed

  24. Attacking the No Preemption Condition • No Preemption – • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • Preempted resources are added to the list of resources for which the process is waiting. • Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. • Not always a viable option • Consider a process given the printer • halfway through its job • now forcibly take away printer • !!??

  25. Attacking the Circular Wait Condition • Option 1: Process entitled to only 1 resource at a time; Has to • give up resource if it needs another one. Say, process wants to • copy large file from tape to printer – so ??? Fails • Numerically ordered resources: All requests in numerical order •  No cycles: if i > j, then A cannot request j; if i < j, then B cannot request it: works, but difficult to find consistent global numbering across distributed systems!!!

  26. Attacking the Circular Wait Condition

  27. Other IssuesTwo-Phase Locking • Phase One • process tries to lock all records it needs, one at a time • if needed record found locked, start over • (no real work done in phase one) • If phase one succeeds, it starts second phase, • performing updates • releasing locks • Note similarity to requesting all resources at once • Algorithm works where programmer can arrange • program can be stopped, restarted

  28. Starvation • Algorithm to allocate a resource • may be to give to shortest job first • Works great for multiple short jobs in a system • May cause long job to be postponed indefinitely • even though not blocked • Solution: • First-come, first-serve policy

More Related