Examples of Deadlock Deadlock occurs with two or more processes are waiting for system conditions that will never occur. In a typical deadlock state there are processes holding some resource while waiting for other resources to become available. Resource 1 Process A Process B Resource 2 In a resource allocation diagram, an arrow from a resource to a process means that this resource is allocated to the process. An arrow from a process to a resource means that this process is requesting the resource. The diagram shows a simple case of deadlock between the two processes. Each process is holding a resource and requesting the other.
Four Necessary Conditions for Deadlock 1. mutual exclusion - processes claim exclusive control of the resources they require. 2. wait for - processes hold resource already allocated to them while waiting for additional resources. 3. no preemption - resources cannot be removed from the processes holding them until the resources are used to completion. 4. circular wait - a circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain.
Deadlock Prevention Deadlock can be prevented by denying one of the four necessary contitions for deadlock to occur. This is called deadlock prevention. Since we must enforce mutual exclusion there are three general approaches for deadlock prevention, suggested by Havender. denying wait-for - each process must request all it required resource at once and cannot proceed until all have been granted. deny no-preemption - if a process holding certain resource is denied a further request, that process must release its original resources and, if necessary, requiest them again together with the additinal resources. deny circular wait - impose a linear ordering of resource types on all processes; i.e., if a process has been allocated resources of a given type, it may subsequently request only those resources of types later in the ordering.
Havender's Linear Ordering R6 R5 R4 R3 R2 R1 The linear-ordering strategy denies the possibility of circular wait in the following manner. Each resource type is labeled with a value with those resource commonly needed at the beginning of a tasks having lower values than those that typically come at the end of a task. A process may request and hold resources in an ascending orderonly. This means that a process may not request any resource of a lower value (ordering value) so long as any resources of a higher value are being held. For example, while process P1 has possession of R4 it may not request an R3 or an R2. Discuss how a process that needs an R1 resource both early and late in its operations can accomplish this. P1 P2 P1 P2
Deadlock Avoidance Sometimes it is not feasible to prevent deadlock. This can occur when we need the most effective use of all our resources. Instead of denying one of the four necessary conditions for deadlock we can monitor resource allocation and avoid the situations in which deadlock might occur (unsate state).Dijkstra has developed the Banker's Algorithm which avoids unsafe states. A Safe State Current Aloc Max Need User(1) 1 4 User(2) 4 6 User(3) 5 8 Avail 2 An Unsafe State Current Aloc Max Need User(1) 1 4 User(2) 4 6 User(3) 6 8 Avail 1
Deadlock Detection In some systems the OS allows deadlock to occur and then suspends or restarts the deadlocked processes allowing others to proceed. To accomplish this recovery we must first have a means of detecting deadlock. Remember that all processes running in a multiprogramming environment need not be involved in the deadlock. Resource allocation diagrams are a graphical representation of an algorithm for deadlock detection. R1 R2 P1P2 P3 Each resource type is shown as a large circle, each instance of a resource is shown as a smaller circle in one of the large circles. Each process is shown as a rectangle. When a resource is allocated to a process an arrow is drawn from the resource instance to the process. When a process requests a resource an arrow is drawn from the process to the desired resource type.
Deadlock Detection by Graph Resolution Deadlock can be detected by reducing resource allocation graphs. Follow the algorithm described below to find the processes involved in deadlock: 1. Build a complete graph. 2. Remove any processes that have all their resources. 3. Return all released resources to the pool of available resources. 4. Satisfy waiting resource requests with newly released resources. 5. Repeat Steps 2,3 and 4 (with back- tracking if necessary) until all processes are removed or until no more requests can be satisfied. 6. Remaining processes are suspended one-at-a-time until deadlock is eliminated. Is there a way to avoid the deadlock shown in Step D? ABCDABCD
Using Resource Allocation Graphs to Prevent Deadlock Consider a special case of resource allocation graphs in which you require that each resource type contains no more than one more resource that any other resource type and that a process may request at most one resource of each type. Given 3 resource types (R), and 2 processes (P) what is the total number of resources N (evenly disributed among the 3 resource types) needed to prevent the possibility of deadlock? R1 R3 P1P2 R2 Develop an expression for the total number of resources N needed to be distributed evenly across R resource types to prevent deadlock in P processes (again assume that each process can request at most 1 of each resource type).
Banker’s Algorithm A Sample Problem A system that uses Banker’s Algorithm deadlock avoidance has five processes and uses resorces of four different types. There are multiple resources of each type. Is the state of the system as shown below safe? Explain your answer. If the system is safe, show how all the processes could complete their execution successfully. If the system is unsafe, show how deadlock might occur. (textbook p. 178, prob. 6.34) Current Maximum Current Process loan need request A B C D A B C D A B C D 1 1 0 2 0 3 2 4 2 2 2 2 2 2 0 3 1 2 3 5 1 2 3 2 0 0 3 2 4 5 1 2 7 7 5 0 3 2 4 4 3 0 0 6 5 5 0 8 2 5 0 2 5 4 2 1 3 6 2 1 4 2 0 0 1 Total Resources Resources Available A B C D A B C D 13 13 9 13 3 4 0 1 The current claim (request) is given as the maximum allowed for each process and resource type.
Examples of Deadlock Four Necessary Conditions (1) mutual exclusion ( 2) wait-for (3) no preemption (4) circular wait Deadlock Prevention requires denial of one of the four necessary conditions Deadlock Avoidance (Safe State) allows the possibility of deadlock Deadlock Detection is not always simple Restrictions on Resource Allocation (preventing or avoiding deadlock?) Dining Philosophers How many different resources? How to represent Resource Allocation Graph How to implement Havender's Linear Ordering Summary