Presentation on theme: "Lecture 7: Deadlock Necessary Conditions for Deadlock Deadlock Prevention - Havender's Linear Ordering Deadlock Avoidance Deadlock Detection &"— Presentation transcript:
1 Lecture 7: Deadlock Necessary Conditions for Deadlock Deadlock Prevention - Havender's Linear OrderingDeadlock AvoidanceDeadlock Detection & RecoveryResource Allocation Graphs
2 Examples of DeadlockDeadlock 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.Resource1ProcessAB2In 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.
3 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.
4 Deadlock PreventionDeadlock 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.
5 Havender's Linear Ordering 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.R6P2P1R5R4P2R3P1P2R2R1
6 Deadlock AvoidanceSometimes 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 StateCurrent Aloc Max NeedUser(1)User(2)User(3)AvailAn Unsafe StateCurrent Aloc Max NeedUser(1)User(2)User(3)Avail
7 Deadlock Detection P1 P2 P3 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.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.R1R2P1P2P3
8 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 haveall their resources.3. Return all released resources tothe pool of available resources.4. Satisfy waiting resource requestswith newly released resources.5. Repeat Steps 2,3 and 4 (with back-tracking if necessary) until allprocesses are removed or until nomore requests can be satisfied.6. Remaining processes are suspendedone-at-a-time until deadlock iseliminated.Is there a way to avoid the deadlock shown in Step D?ABCD
9 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?R1R2R3Develop 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).P1P2
10 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)Total ResourcesResources AvailableA B C D A B C DCurrent Maximum CurrentProcess loan need requestA B C D A B C D A B C DThe current claim (request) is given as the maximum allowed for each process and resource type.
14 Summary Examples of Deadlock Four Necessary Conditions (1) mutual exclusion( 2) wait-for(3) no preemption(4) circular waitDeadlock Prevention requires denial of one of the four necessary conditionsDeadlock Avoidance (Safe State) allows the possibility of deadlockDeadlock Detection is not always simpleRestrictions on Resource Allocation (preventing or avoiding deadlock?)Dining PhilosophersHow many different resources?How to represent Resource Allocation GraphHow to implement Havender's Linear Ordering