You are on page 1of 28
Operating Systems Deadlocks Outline Introduction Deadlock Detection and Recovery Deadlock Prevention Deadlock Avoidance Introduction * Deadlock 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. — The event could be a resource => Resource deadlock * Resources — Anything that can be acquired, used, and released over the course of time Includes devices, data records, files, ... Preemptable * Possible to take away the resource before it finishes + E.g., Memory Non-preemptable + Resource cannot be taken away from its current owner without potentially causing failure * Usually involved in deadlocks [] | | | | ° Introduction... ¢ Resources... — Abstract sequence of events required to use a resource 1. Request the resource 2. Use the resource 3. Release the resource — If the requested resource is not available, requester * Could be forced to block * Could fail with an error code [] | | | | “ Introduction... * Condition for resource blocking (deadlock) — Mutual exclusion * Each resource is either currently assigned to exactly one process or is available — Hold and wait * Aprocess holding a resource is waiting to acquire additional resources held by other processes — No preemption * Aresource can be released only voluntarily by the process holding it, after that process has completed its task — Circular wait * There exists a set {PO, P1, ..., Pn} of waiting processes such that PO 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 PO [] | | | | . Introduction... * Deadlock modeling — (a) Process A is holding resource R —(b) Process B is requesting a resource S —(c) Deadlock © ©——2 Introduction... * Deadlock modeling... bam bam bod R09 FRE AN R T R] T Introduction... ¢ Strategies for dealing with deadlocks 1. 2. 3. Just ignore the problem Detection and recovery Dynamic avoidance by careful resource allocation Prevention, by structurally negating one of the four conditions Introduction... ¢ The Ostrich algorithm — Pretend there is no problem — Reasonable if * Deadlocks occur very rarely * Cost of prevention is high — Unix and Windows take this approach [] | | | | ° Deadlock detection and recovery * No attempt to prevent deadlocks from occurring * Tries to detect when deadlock happens, and take some action to recover * Deadlock detection — One resource of each type * There is only one resource of each type — E.g., one printer, one CD recorder, ... * Construct a resource graph * See if there is a cycle — One or more cycles indicate deadlock * There are many algorithms for detecting cycles in directed graphs [] | | | | * Deadlock detection and recovery... Deadlock detection... — One resource of each type... * Algorithm — Uses one dynamic data structure, L, a list of nodes and list of arcs 1. Take each node in turn as the root of a tree 2. Doa depth-first search 1. _ If the search finds a node that is already in the list, it has found a cycle 2. Ifit exhausts all the arcs from any given node, it backtracks to the previous node . If it backtracks to the root and cannot go further, the sub-graph reachable from the current node doesn’t contain any cycle [] | | | | * Deadlock detection and recovery... * Deadlock detection... — One resource of each type... * Example RT @) — Start from R » L={R, A, S} © ° ro s] — Start from B » L={B, 7, E, V,G, U, D, S} b b » L={B,T, E, V, G, U, D, T} BH Ww e * Deadlock Deadlock detection and recovery... * Deadlock detection... — Multiple resources of each type * Let m = the number of resource classes (e.g., printer, CD-ROM) E = existing resource vector (E, = resources of class 1, E, = resources of class 2; E, = 2 means there are 2 items of resource 1) A= available resource vector (A, = number of instances of resource i that are currently available) C= current allocation matrix (C,; = number of instances of resource j that are held by process i) R = request matrix (R= number of instances of resource j that process i wants) * Every resource is allocated or is available 3B Deadlock detection and recovery... * Deadlock detection... — Multiple resources of each type... Resources in existence Resources available (Ey, Ey, Egy 1 Em) OpAn Agena Ag Current allocation matrix Request matrix Cy Cy Cig 22+ Com Ry Big Rig °° Rim Coy Cy Cog °° * Com Poy Rog Pag °° Ram C Cor Cpe Cag Com Rat Bao Ras °° Rom Row n is current allocation Row 2 is what process 2 needs to process n u Cyt Aj =E; i [] | | | | “ Deadlock detection and recovery... * Deadlock detection... — Multiple resources of each type... * Algorithm for detection — Assumes all processes keep all acquired resources until they exit — Each process is initially said to be unmarked 1. Look for an unmarked process, P, , for which the i" row of R is less than or equal to A 2. If sucha process is found, add the it" row of C to A, mark the process, and go back to step 1 3. If no such process exists, the algorithm terminates [] | | | | * Deadlock detection and recovery... * Deadlock detection... — Multiple resources of each type... . Example — 1and 24 processes do not satisfy step 1 — 3° process (3° row < A), execute and return all resources in C =>A=(2,2,2,0) — Process 2 can now execute (2"¢ row < A) and return all resources in C (4,2,2,1) — Process 1 will then execute . Example: (Add) process 3 requires a Blue-ray drive Pdeadlock 6 LF ee FFF SF AB OL SS E=(4 2 3 1) A=(2 1 0 0) Current allocation matrix Request matrix 0010 2001 c=|2 001 R-|1 0 10 0120 2100 [] | | | | * Deadlock detection and recovery... * Deadlock detection... — Multiple resources of each type... * When to look for deadlocks? — Check every time a resource is requested » Advantage: Detects deadlock as early as possible » Disadvantage: Expensive in terms of CPU time — Check every k minutes — Check when CPU utilization has dropped v7 Deadlock detection and recovery... * Recovery from deadlock — Recovery through preemption * Temporarily take a resource away from its current owner and give it to another process * Depends on the nature of resource to be taken away — Identifying a process with such resource is difficult or impossible — Recovery through rollback * Processes are checkpointed periodically * Checkpoiniting — Save states of a process so that it can be restarted later * When a deadlock is detected, a process that holds a needed resource is rolled back to a point in time before it acquired that resource — Recovery through killing processes * Requires to properly select a process to be killed * Example: A process that can be rerun 18 Deadlock prevention * Based on conditions for deadlock ¢ Attacking the mutual-exclusion condition — No resource is assigned exclusively to a single process — Processes use the resource concurrently * Make resource read only * Using a spooler for printer — Avoid assigning a resource unless absolutely necessary Deadlock prevention... * Attacking the hold-and-wait condition — Prevent a process that hold a resource from waiting for more resources — Require all processes to request all their resources before starting execution — Problem * Processes may not know how many resources they will need until they start running — Solution * Programmers are required to list the resources they need for their program — Problem: Overhead to the programmer — Not optimal, might hold a resource that it doesn’t need until sometime later in the execution 20 Deadlock prevention... « Attacking the no-preemption condition — Allow 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 a Deadlock prevention... ¢ Attacking the circular wait condition « Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration ¢ In order to request resource Rj, a process must first release all Ri such that i<=j. * Example sutnappeonee GB 2. Printer 3. Plotter t 4. Tape drive : 5. Blu-ray drive ! [ i * Challenge — May be impossible to find an ordering that satisfies everyone [] | | | | * Deadlock avoidance * Is there an algorithm that can always avoid deadlock by making the right choice all the time? — Yes... but only if certain information is available in advance — Requires that each process declare the maximum number of resources of each type that it may need — The deadlock-avoidance algorithm ensure that there can never be a circular-wait condition — Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes — When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state * Safe and unsafe states — System is in safe state * If there is some scheduling order in which every process can run to completion even if all of them suddenly request their maximum number of resources [] | | | | * Deadlock avoidance... ¢ Safe and unsafe states... — System is in safe state ... * if there exists a sequence of ALLthe processes in the systems such that for each P,, the resources that Pjcan still request can be satisfied by currently available resources and resources held by all the P,, with j< i — That is: + If P, resource needs are not immediately available, then P, can wait until all P; have finished * When P;is finished, P; can obtain needed resources, execute, return allocated resources, and terminate * When P, terminates, P;,, can obtain its needed resources, and so on [] | | | | “ Deadlock avoidance... * Safe and unsafe states... — Example * State at (a) is safe. Why? — There is a sequence of allocation that allows all processes to complete BoC->A — Exercise * If Arequests and gets 1 resource (i.e., A Has 4 resources and Free is 2), can we find a sequence that is guaranteed to work? Has Max Has Max Has Max Has Max Has Max A] 3 9 A] 3 9 A] 3 9 Al 3 9 A 3 9 BI] 2 4 BI 4 4 B Oo} - B]O - B 0 - ci 2 7 Cc} 2 7 Cc} 2 7 c}|7 7 cjo - Free: 3 Free: 1 Free: 5 Free:0 Free: 7 (a) (b) (o) (d) (e) fH » Deadlock avoidance... ¢ Safe and unsafe states... — Unsafe state is not a deadlocked state — From unsafe state, there is no guarantee that all processes will finish [] | | | | * Deadlock avoidance... ¢ Banker’s algorithm for multiple resource — Combines the deadlock detection algorithm for multiple resources with the concept of safe and unsafe state — If a request leads to unsafe state, it must be deferred for a while — Limitation * Processes rarely know in advance what their maximum resource needs will be * Number of processes is not fixed * Resources which were available at one time, may not be available in another time [] | | | | 7 Deadlock avoidance... — Example Allocation Max Available Need ABC ABC ABC ABC Po o10 753 332 743 Ries 200) 322 122 Po 302 902 600 eet 222 011 Ps 002 433 431 * And now consider what happens if process P1 requests 4 instance of A and 2 instances of C. | Request 1] =(1,0,2)) Allocation Need Available ABC ABC ABC 010 743 230 302 020 302 600 21a: oll 002 431 * What about requests of ( 3, 3,0) by P4? or (0, 2, 0) by PO? Can these be safely granted? Why or why not?

You might also like