Objectives/Outline Objectives Outline Develop conceptual Introduction Chapter 7: Deadlocks � � understanding of deadlocks System Model � Present a number of different � Deadlock Characterization � methods for preventing and Methods for Handling � avoiding deadlocks Deadlocks Presented By: Dr. El-Sayed M. El-Alfy Deadlock Prevention � Deadlock Avoidance � Deadlock Detection � Recovery from Deadlock � Note: Most of the slides are compiled from the textbook and its complementary resources April 08 2 Deadlock Characterization: Conditions for Introduction Deadlock � Deadlock is defined as the permanent blocking of a set of processes that are competing for a finite number of system resources occurs when a set of processes are in a wait state and each process � is waiting for a resource that is held by some other waiting process all deadlocks involve conflicting resource needs by two or more � processes Traffic only in one direction � � Unlike other problems in multiprogramming systems, Each section of a bridge can be viewed as a resource � there is no efficient solution to the deadlock problem in If a deadlock occurs, it can be resolved if one car backs up � the general case (preempt resources and rollback) Several cars may have to be backed up if a deadlock occurs � Starvation is possible � 3 4 April 08 April 08
Deadlock Characterization: Conditions for Deadlock Characterization: Resource-Allocation Deadlock (cont.) Graph � � The four necessary conditions for a deadlock: A set of vertices V and a set of edges E Mutual Exclusion: processes require exclusive control of their V is partitioned into two types: P = { P 1 , P 2 , …, P n } , the set of all � � resources (no sharing) processes in the system and R = { R 1 , R 2 , …, R m } , the set of all resource Hold and Wait: process may wait for a resource while holding others types in the system � A request edge is a directed edge P 1 → R j and an assignment edge is a No Preemption: resources cannot be preempted; a process will only � � voluntarily give up a resource after completing its task with this directed edge R j → P i resource. Process is represented by � 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 Resource type with 4 instances is represented by � that is held by P n , and P n is waiting for a resource that is held by P 0 � Example: semaphores A and B , initialized to 1 P i P i requests instance of R j represented by � P 0 P 1 R j wait (A); wait(B) wait (B); wait(A) P i is holding an instance of R j . This is represented by � P i R j April 08 5 April 08 6 Example of a Resource Allocation Graph Resource Allocation Graph with a Deadlock � If graph contains no cycles: � Processes: P = { P1, P2, no deadlock P3} � If graph contains a cycle: � Resource types: R= { R1, � If only one instance per R2, R3, R4} resource type, then deadlock � Edges: E = { P 1 → R 1 , � If several instances per P 2 → R 3 , R 1 → P 2 , R 2 → P 2 , resource type, possibility of deadlock R 2 → P 1 , R 3 → P 3 } � Resource instances: R 1 (one), R 2 (two), R 3 (one), R 4 (three) 7 8 April 08 April 08
Methods for Handling Deadlocks Resource Allocation Graph with a Cycle But No Deadlock � How can we handle a deadlock situation? � Ensure that the system will never enter a deadlock state � In this case, the system can use either deadlock prevention or deadlock avoidance techniques � Allow the system to enter a deadlock state and then recover In this case, the system employs deadlock detection and deadlock � recovery techniques � Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX � This results in deterioration of system performance and results in restarting the system manually April 08 9 April 08 10 Deadlock Prevention (cont.) Deadlock Prevention � By ensuring that at least one of the four necessary � No Preemption deadlock conditions cannot hold, we prevent the � If a process that is holding some resources requests another occurrence of a deadlock resource that cannot be immediately allocated to it, then all resources currently being held are released � Mutual Exclusion � Preempted resources are added to the list of resources for � Not required for sharable resources such as read-only files which the process is waiting � Must hold for non-sharable resources such as a printer � Process will be restarted only when it can regain its old � Hold and Wait: must guarantee that whenever a resources, as well as the new ones that it is requesting process requests a resource, it does not hold any � other solutions?? If requested resources are held by waiting other resources processes, preempt them from the waiting processes and allocate them to the requesting process; otherwise wait � Require the process to request and be allocated all its resources before it begins execution, or allow the process to � Circular Wait – impose a total ordering of all resource request resources only when the process has no other types, and require that each process requests resources resources in an increasing order of enumeration � Low resource utilization and starvation is possible 11 12 April 08 April 08
Activity Deadlock Avoidance � Requires that the system has some additional a priori � Prove that the circular-wait condition can not hold information available under each of the following conditions � Simplest and most useful model requires that each process � A process holding Ri can request Rj iff F(Rj)> F(Ri) declare the maximum number of resources of each type that � If a process request Rj then it has released all resources Ri for it may need which F(Ri) > = F(Rj) � 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 � We want to insure that the resource-allocation state is safe April 08 13 April 08 14 Deadlock Avoidance: Safe State Deadlock Avoidance: Safe State When a process requests an available resource, system must decide � if immediate allocation leaves the system in a safe state System is in a safe state if there exists a safe sequence of all � processes Sequence < P 1 , P 2 , …, P n > is safe if for each P i , the resources that P i � can still request can be satisfied by currently available resources + resources held by all the P j with j < i If P i resource needs are not immediately available, then P i can wait until � all P j have finished When P j is finished, P i can obtain needed resources, execute, return � allocated resources, and terminate If a system is in safe state ⇒ no deadlocks � When P i terminates, P i + 1 can obtain its needed resources, and so on � If a system is in unsafe state ⇒ possibility of deadlock � Avoidance ⇒ ensure that a system will never enter an unsafe state � 15 16 April 08 April 08
Resource-Allocation Graph Algorithm Deadlock Avoidance: Banker’s Algorithm Applicable to a system with ONE � � Applicable to a system with multiple instances of each instance of each resource resource Claim edge P i → R j indicated that � Analogy to a banking system � process P j may request resource R j � Could be used in banking system to ensure that the bank never represented by a dashed line � allocates its available cash such that it can no longer satisfy the needs of all customers Claim edge converts to request edge � when a process requests a resource � Each process must claim maximum resources usage in advance When a resource is released by a � process, assignment edge reconverts to � When a process requests a resource it may have to wait a claim edge Resources must be claimed a priori in � the system April 08 17 April 08 18 Data Structures for the Banker’s Algorithm: Data Structures for the Banker’s Algorithm Example � Assume that there are 5 processes P0 through P4; 3 resource types � Let n = number of processes, and m = number of A (10 instances), B (5 instances), and C (7 instances) resources types � n = ?, m = ? � available [A] = ? � Available: Vector of length m . If available [ j ] = k , there � available [B] = ? are k instances of resource type R j available � available [C] = ? � Max: n x m matrix. If Max [ i,j ] = k , then process P i may � Snapshot at time: request at most k instances of resource type R j Allocation Max Available Need � Allocation: n x m matrix. If Allocation[ i,j ] = k then P i A B C A B C A B C A B C is currently allocated k instances of R j P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 � Need: n x m matrix. If Need [ i,j ] = k , then P i may need P2 3 0 2 9 0 2 k more instances of R j to complete its task P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 Need [ i,j] = Max [ i,j ] – Allocation [ i,j ] 19 20 April 08 April 08
Recommend
More recommend