Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money
Introduction to Deadlocks Deadlocks are formally defined by Deadlocks are formally defined by –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 Since they are all waiting, none of them will wake up Since they are all waiting, none of them will wake up Assumption of no interrupts and single threads Assumption of no interrupts and single threads
Conditions for Deadlock 1. Mutual Exclusion – each resource is either currently assigned to one process or is available 2. Hold and Wait – processes currently holding resources can request new resources 3. No preemption – Resources previously granted cannot forcibly be taken away from the process. They must be released by the process 4. Circular Wait – there must be a circular chain of 2+ processes, each whom is waiting for a resource held by the next member of the chain
Conditions for Deadlock All four conditions must exist for a deadlock to occur All four conditions must exist for a deadlock to occur If one is absent, deadlock cannot occur If one is absent, deadlock cannot occur Many of these are related to system policy choices Many of these are related to system policy choices
Dealing with Deadlocks Ignore the problem, maybe it will ignore you? Ignore the problem, maybe it will ignore you? –Used by UNIX and Windows Detection and Recovery Detection and Recovery Dynamic avoidance by careful resource allocation Dynamic avoidance by careful resource allocation Prevention by structurally negating one of the four conditions for deadlocks Prevention by structurally negating one of the four conditions for deadlocks
Deadlock Avoidance In deadlock detection, we assumed all resources were requested simultaneously In deadlock detection, we assumed all resources were requested simultaneously However, in reality, we request them one at a time However, in reality, we request them one at a time The system must decide if granting the resource is safe or not The system must decide if granting the resource is safe or not We consider careful resource allocation now We consider careful resource allocation now
Resource Trajectories The main algorithm is based on the idea of safe states The main algorithm is based on the idea of safe states We first consider a graphic version of this model first We first consider a graphic version of this model first The does not immediately turn into an algorithm, but provide a good intuition into the problem The does not immediately turn into an algorithm, but provide a good intuition into the problem
Resource Trajectories The following slide shows a model for dealing with two processes and two resources The following slide shows a model for dealing with two processes and two resources The horizontal axis represents number of instructions executed for process A The horizontal axis represents number of instructions executed for process A The vertical axis represents number of instructions executed for process B The vertical axis represents number of instructions executed for process B
Resource Trajectories At I 1, A requests a printer and at I 2, A requests a plotter At I 1, A requests a printer and at I 2, A requests a plotter The printer and plotter are released at I 3 and I 4, respectively The printer and plotter are released at I 3 and I 4, respectively Process B needs the plotter from I 5 to I 7 and the printer from I 6 to I 8 Process B needs the plotter from I 5 to I 7 and the printer from I 6 to I 8
Resource Trajectories
Every point in the picture represents a joint state of the two processes Every point in the picture represents a joint state of the two processes Initially, the state is p, with nothing having been executed Initially, the state is p, with nothing having been executed If the scheduler runs A first, then we get to point q If the scheduler runs A first, then we get to point q Then process B runs, and we get to r Then process B runs, and we get to r
Resource Trajectories When A cross the line for I 1, it requests and it granted the printer When A cross the line for I 1, it requests and it granted the printer When B reaches t, it requests the plotter When B reaches t, it requests the plotter The shaded regions are of particular interest for deadlocks The shaded regions are of particular interest for deadlocks
Resource Trajectories The slanted lines from southwest to northeast is when both processes have the printer The slanted lines from southwest to northeast is when both processes have the printer The slanted lines from northwest to southeast is when both processes have the plotter The slanted lines from northwest to southeast is when both processes have the plotter Both of these are deadlock regions because of mutual exclusion Both of these are deadlock regions because of mutual exclusion
Resource Trajectories If the system enters the box bounded by I 1, I 2, I 5, and I 6, it will eventually deadlock when it reaches the intersection of I 2 and I 6 If the system enters the box bounded by I 1, I 2, I 5, and I 6, it will eventually deadlock when it reaches the intersection of I 2 and I 6 The entire box is unsafe The entire box is unsafe At point t, the only safe course of action is to run process A until it gets to I 4 At point t, the only safe course of action is to run process A until it gets to I 4 Any trajectory outside of this box to u will do Any trajectory outside of this box to u will do
Resource Trajectories The important thing to notice at point t, is that process B is requesting a resource The important thing to notice at point t, is that process B is requesting a resource The system must decide to grant it or not The system must decide to grant it or not If it is granted, it enters an unsafe region and a possible deadlock If it is granted, it enters an unsafe region and a possible deadlock To avoid this, we should suspect process A until is requests and releases the plotter To avoid this, we should suspect process A until is requests and releases the plotter
Safe and Unsafe States We will use the vectors and matrices from deadlock detection We will use the vectors and matrices from deadlock detection A state is said to be safe if it is not deadlocked and there is some scheduling order so that each process can run to completion even if they requests their maximum number of resources immediately A state is said to be safe if it is not deadlocked and there is some scheduling order so that each process can run to completion even if they requests their maximum number of resources immediately
Safe and Unsafe States
The prior example is safe since there is a sequence of allocations that allows the processes to complete The prior example is safe since there is a sequence of allocations that allows the processes to complete Now, let us consider an unsafe example Now, let us consider an unsafe example
Safe and Unsafe States
So, the decision to go from (a) to (b) in the prior slide moves us from a safe state to an unsafe state So, the decision to go from (a) to (b) in the prior slide moves us from a safe state to an unsafe state We should have not granted process A’s request to prevent a possible deadlock We should have not granted process A’s request to prevent a possible deadlock Note: An unsafe state is not necessarily a deadlock! Note: An unsafe state is not necessarily a deadlock! Only a safe state guarantees all processes will finish Only a safe state guarantees all processes will finish In an unsafe state, it may or may not finish In an unsafe state, it may or may not finish
Banker’s Algorithm for Single Resource The scheduling algorithm for handling single resources is due to Dijkstra(1965) and is known as the banker’s algorithm The scheduling algorithm for handling single resources is due to Dijkstra(1965) and is known as the banker’s algorithm It is an extension of the deadlock detection algorithm It is an extension of the deadlock detection algorithm It is modeled similar to the way a small town banker deals with customers whom he has given a line of credit It is modeled similar to the way a small town banker deals with customers whom he has given a line of credit
Banker’s Algorithm for Single Resource The algorithm checks to see if granting a resource leads to a safe or unsafe state The algorithm checks to see if granting a resource leads to a safe or unsafe state The banker gives out the various credit limits, which add up to 22 The banker gives out the various credit limits, which add up to 22 However, s/he can only lend out 10 units at a time However, s/he can only lend out 10 units at a time The units can be tape drives, the customers are processes and the banker is the OS The units can be tape drives, the customers are processes and the banker is the OS
Banker’s Algorithm for Single Resource
In (b), the state is safe In (b), the state is safe In (c) is unsafe In (c) is unsafe In (b), if anyone but C requests a resource, it can be delayed until C is finished In (b), if anyone but C requests a resource, it can be delayed until C is finished (c) does not have to result in a deadlock, but we want to avoid this state (c) does not have to result in a deadlock, but we want to avoid this state
Banker’s Algorithm for Single Resource The algorithm considers each request as it occurs and checks to see if it leads to a safe state The algorithm considers each request as it occurs and checks to see if it leads to a safe state If it does, the request is granted If it does, the request is granted If it does not, the request is postponed If it does not, the request is postponed To check safety, we see if we have enough resources to satisfy some process To check safety, we see if we have enough resources to satisfy some process The resources are released, and the next closest customer of the limit is checked, and so on The resources are released, and the next closest customer of the limit is checked, and so on All processes must be able to finish to be safe All processes must be able to finish to be safe
Banker’s Algorithm for Multiple Resources We can now generalize the banker’s algorithm for multiple resources We can now generalize the banker’s algorithm for multiple resources This time we use a matrix of assigned and request resources similar to before This time we use a matrix of assigned and request resources similar to before
Banker’s Algorithm for Multiple Resources
1. Look at a row, R, whose unmet resource needs are smaller than or equal to A(R<=A). If no row exists, the system will eventually deadlock 2. Assume the process of the chosen row requests its resources and finishes. Mark the process as terminated and add its resources to vector A 3. Repeat 1 and 2 until either all the processes are marked as terminated, which means the state is safe, or until a deadlock occurs, which means the state is unsafe
Banker’s Algorithm for Multiple Resources The current state is safe in the figure The current state is safe in the figure Suppose process B requests a scanner Suppose process B requests a scanner –This is granted since the resulting state is safe –Process D, then process A or E finishes, followed by the rest
Banker’s Algorithm for Multiple Resources After B is granted one of the two remaining scanners, suppose E wants the last printer After B is granted one of the two remaining scanners, suppose E wants the last printer –This reduces A=( ) –This leads to a potential deadlock –This request must be deferred
Houston, we have a problem! This has been highly studied This has been highly studied However, it suffers from a major flaw: However, it suffers from a major flaw: –It is useless –It needs to know the total resource needs of a program in advance –In addition, the number of processes is dynamic