Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock
The Deadlock Problem Deadlock: A process requests resources; if the resources are not available at that time, the process enters a waiting state. Sometimes, a waiting process can not change its state, because the resources it has requested are held by other waiting processes. Example of deadlocks: A system with CD1 and CD2 drives allocated to P1 and P2. P1 requested CD2 and P2 requested CD1. Each is waiting for the event CD is released, which can be caused only by one of the other waiting processes(same resource type deadlock). A system with one printer and one DVD. Suppose that process Pi is holding the DVD and process Pj is holding the printer. If Pi requests the printer and Pj requests the DVD drive, a deadlock occurs. Semaphores A and B, initialized to 1 P0 P1 wait (A); wait(b) wait (B); wait(a)
System Model A system have a finite number of resources R1,R2,R3, Rm divided into several resource types/classes (CPU cycles, files, and I/O devices) each consisting of some number of identical instances (CPU1 & CPU2). These resources can requested by a number of processes P1,P2,., Pn. If a process requests an instance of a resource type, any instance of the type should satisfy the request. Else they are not identical. A process may utilize a resource in only the following sequence: Request: The process requests the resource. If it cannot be granted immediately, the process must wait until it can acquire the resource. Use: The process can operate on the resource. Release: The process releases the resource.
System Model The request and release of resources may be system calls: request(),release() device open(),close() file, and allocate() free() memory system calls. Resource allocation table: is a system table that records whether each resource is free or allocated by which process. If a process requests a resource type that is currently allocated to another process, it can be added to a queue of processes waiting for this resource. Each process of a set of processes is in a deadlocked state is waiting for an event that can be caused only by another process in the set. This event is resource acquisition and release.
Bridge Crossing Example Traffic only in one direction Each section of a bridge can be viewed as a resource If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback) Several cars may have to be backed up if a deadlock occurs Starvation is possible Note Most OS do not prevent or deal with deadlocks
Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource, If another process requests that resource, the requesting process must be delayed until the resource has been released. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task Circular wait: there exists a set {P 0, P 1,, P n } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2,, P n 1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0.
Resource Allocation Graph This graph consists of: A set of vertices V (all the active processes in the system Process P = {P1, P2,..., Pn }, and all resource types in the system Resource R = {R1, R2,..., Rm}). A set of edges E: Request Edge (Pi Rj): Process Pi has requested an instance of resource type Rj and is currently waiting for that resource. points to only the rectangle Rj assignment edge (Rj Pi ): An instance of resource type Rj has been allocated to process Pi. One dot in the rectangular. Work Sequence: Each process Pi as a circle Each resource type Rj as a rectangle (each instance as a dot within the rectangle). A request edge is inserted in the resource-allocation graph When process Pi requests an instance of resource type Rj. The request edge is instantaneously transformed to an assignment edge When this request can be fulfilled. When the process no longer needs the resource, it releases the resource. Process Resource Type of 4 instants P i P i R j P i requests instance of R j P i is holding an instance of R j Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock Given the resource-allocation graph: If the graph contains no cycles, no deadlock. If the graph does contains a cycle, then a deadlock may exist. If each resource type has exactly one instance, then a cycle means that a deadlock has occurred. If the cycle involves only a set of resource types, each has only a single instance, then a deadlock has occurred. Each process involved in the cycle is deadlocked. In this case, a cycle in the graph is both a necessary and a sufficient condition for the existence of deadlock. If each resource type has several instances, then a cycle does not necessarily imply that a deadlock has occurred. A cycle in the graph is a necessary but not a sufficient condition for the existence of deadlock. Graph With A Cycle But No Deadlock
Resource Allocation Graph
Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state, the system can use either: Deadlock prevention: a set of methods to ensure that at least one of the necessary conditions cannot hold. Deadlock avoidance requires that the OS be given additional information in advance concerning which resources a process will request and use during its lifetime. the OS can decide for each request whether or not the process should wait. Allow the system to enter a deadlock state and then recover, by provide an algorithm that examines the state of the system to determine whether a deadlock has occurred and an algorithm to recover from the deadlock. Ignore the problem and suppose that deadlocks never occur in the system; used by most OS, including UNIX and windows, the application developer to write programs that handle deadlocks
Deadlock Prevention the ways request can be made Mutual Exclusion not required for sharable resources; must hold for non-sharable resources 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 Allow process to request resources only when the process has none Low resource utilization; starvation possible 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
Deadlock Avoidance Requires that the system has some additional a priori information available Simplest and most useful model requires that each process 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 Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes
Safe State When a process requests an available resource, system must decide if this allocation leaves the system in a safe state System is in safe state That is: If P 0 requested resource are not immediately available. P 0 can wait until all P 1 have finished, When P 1 is finished, P 0 can obtain requested resources, execute, return allocated resources, and terminate When P i terminates, P i +1 can obtain its needed resources, and so on Basic Facts: If a system is in safe state no deadlocks If a system is in unsafe state possibility of deadlock Avoidance ensure that a system will never enter an unsafe state. Single instance of a resource type Use a resource-allocation graph Multiple instances of a resource type Use the banker s algorithm
Resource-Allocation Graph Scheme Used If we have a resource-allocation system with only one instance of each resource type. A new type of edge called Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line Claim edge converts to request edge when a process requests a resource Request edge converted to an assignment edge when the resource is allocated to the process When a resource is released by a process, assignment edge reconverts to a claim edge Resources must be claimed a priori in the system before starting execution of the process all its claim edges must appear on the resource allocation graph.
Multiple instances per each resource. Banker s Algorithm Each process must a priori claim maximum use When a process requests a resource it may have to wait When a process gets all its resources it must return them in a finite amount of time. Data Structures for the Banker s Algorithm Let n = number of processes, and m = number of resources types. Available[i]: Vector of length m. If available [j] = k, there are k instances of resource type R j available Max[I,j]: n x m matrix. If Max [i,j] = k, then process P i may request at most k instances of type R j. Allocation[I,j]: n x m matrix. If Allocation[i,j] = k then P i is currently allocated k instances of R j Need[I,j] n x m matrix. If Need[i,j] = k, then P i may need k more instances of R j to complete its task Need [i,j] = Max[i,j] Allocation [i,j]
Safety Algorithm
Resource-Request Algorithm for Process Pi
Example of Bankers Algorithm Consider a system with five processes P0 through P4 and three resource types A (10 instances), B (5 instances), and C (7 instances). Suppose that, at time T0, the following snapshot of the system: Available = System resources Allocated by all process Max is max required for execute process Need = Max(P) Allocated (P) IF P1 requests additional (A(1),B(0),C(2)). To decide whether this request can be immediately granted, we first check that Request t1 Available (1,0,2) (3,3,2).
Deadlock Detection If a system does not employ either a deadlockprevention or a deadlock avoidance algorithm, then a deadlock situation may occur. In this environment, the system may provide: An algorithm that examines the state of the system to determine whether a deadlock has occurred An algorithm to recover from the deadlock
Single Instance of Each Resource Type Make a small modification on the Resource Allocation Graph ( Resulted Wait-For Graph) Maintain wait-for graph An edge from Pi to Pj in a wait-for graph implies that process Pi is waiting for process Pj to release a resource that Pi needs. An edge Pi Pj exists in a wait-for graph if and only if the corresponding resource allocation graph contains two edges Pi Rq and Rq Pj for some resource Nodes are processes P i P j if P i is waiting for P j Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph. Resource-Allocation Graph Corresponding wait-for graph
Several Instances of a Resource Type Available: A vector of length m indicates the number of available resources of each type. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. Request: An n x m matrix indicates the current request of each process. If Request [i][j] = k, process P i is requesting k more instances of resource type. R j.
Several Instances of a Resource Type
Several Instances of a Resource Type
Example of Detection Algorithm Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances) The snapshot of the system at time T0 is: P2 request(0,0,1) We claim that the system is not in a deadlocked state. Indeed, if we execute our algorithm, we will find that the sequence <P0, P2, P3, P1, P4> results in Finish[i] == true for all i. Deadlock have occurred because the requested is less than the available
Recovery from Deadlock 1. Process Termination Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated In which order should we choose to abort? Priority of the process How long process has computed, and how much longer to completion Resources the process has used Resources process needs to complete How many processes will need to be terminated Is process interactive or batch? 2. Resource Preemption Selecting a victim minimize cost Rollback return to some safe state, restart process for that state Starvation same process may always be picked as victim, include number of rollback in cost factor
End of Chapter 7