Presentation on theme: "Deadlock 2. Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state System."— Presentation transcript:
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 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 is resource needs are not immediately available, then P i can wait until all P j have finished – When P j finishes, P i can obtain needed 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 system is in safe state no deadlock If system is in unsafe state possibility of deadlock Avoidance ensure that system will never enter an unsafe state
Safe, Unsafe, Deadlock State
Example Allocation Max P P P Assume the system has 12 instances of the particular resource Is the system currently in a safe or in an unsafe state?
Example Suppose that P 2 requests and is allocated one more instance of the resource, still assume the system has 12 instances of the resource Allocation Max P P P Is the system currently in a safe or in an unsafe state?
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
Avoidance algorithms Single instance of a resource type –Use a resource-allocation graph Multiple instances of a resource type – Use the bankers algorithm
Resource-Allocation Graph Algorithm Can be used if we have a resource-allocation system with only one instance of each resource type. We just need ensure that no cycle exists Claim edge P i R j indicates that process P i may request resource R j ; represented by a dashed line Claim edge converts to request edge when a process requests a resource Resources must be claimed a priori in the system
Resource-Allocation Graph For Deadlock Avoidance Suppose P 2 requests R 2, can this request be granted?
Resource-Allocation Graph For Deadlock Avoidance
Resource-Allocation Graph Algorithm Suppose that process Pi requests a resource Rj The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph
Bankers Algorithm Multiple instances 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
Bankers Algorithm Toward right idea: – State maximum resource needs in advance – Allow particular process to proceed if: (available resources - #requested) max remaining that might be needed by any process Bankers algorithm (less conservative): – Allocate resources dynamically Evaluate each request and grant if some ordering of processes is still deadlock free afterward Technique: pretend request is granted, and check to see if resulting state would be a SAFE state. Grant request if result is deadlock free (conservative!) Keeps system in a SAFE state (i.e., there is always a safe sequence) – Algorithm allows the sum of maximum resource needs of all current processes to be greater than total resources
Bankers Algorithm: Example 5 processes P 0 through P 4 ; 3 resource types R 0 (10 instances), R 1 (5 instances), and R 2 (7 instances) Snapshot at initial time: AllocationMaxAvailable R 0 R 1 R 2 R 0 R 1 R 2 R 0 R 1 R 2 P P P P P Is the system in a safe state? – The system is in a safe state since the sequence satisfies safety criteria Need R 0 R 1 R
Bankers Algorithm: Example Suppose that P 1 requests (1, 0, 2). Is it safe to honor this request? AllocationMaxAvailable R 0 R 1 R 2 R 0 R 1 R 2 R 0 R 1 R 2 P P P P P Find the new state that the system would be in if this request is granted. If the new state is safe, then it is safe to grant this request. Otherwise, it is not safe to honor this request (safety algorithm)
Bankers Algorithm: Example This is the new state that the system would be in if P 1s request is honored AllocationMaxAvailable R 0 R 1 R 2 R 0 R 1 R 2 R 0 R 1 R 2 P P P P P – The sequence satisfies safety requirement, so it is safe to honor P 1s request Can request for (3,3,0) by P 4 be granted? Can request for (0,2,0) by P 0 be granted?
Data Structures for the Bankers Algorithm Let n be the number of processes, and m be the number of resource types – Available : Vector of length m. If Available[j]= k, there are k instances of resource type R j available – Max : n x m matrix. If Max[i,j]= k, then process P i may request at most k instances of resource type R j – Allocation : n x m matrix. If Allocation[i,j]= k then P i is currently allocated k instances of R j – Need : 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]
Resource-Request Algorithm for Process P i Let Request be the request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j 1. If Request i Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim 2.If Request i Available, go to step 3. Otherwise P i must wait, since resources are not available 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available - Request i Allocation i = Allocation i + Request i Need i = Need i – Request i If safe the resources are allocated to P i If unsafe P i must wait, and the old resource-allocation state is restored
Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish[i] = false for i = 0, 1, …, n 2. Find an i such that Finish[i] = false AND Need i Work If no such i exists, go to step 4 3. Work = Work + Allocation i Finish[i] = true go to step 2 4. If Finish[i] == true for all i, then the system is in a safe state
Example Consider a computer system having 4 serially-reusable resource classes R0 (4 instances), R1 (3 instances), R2 (11 instances), and R3 (2 instances). There are 4 processes in the system P0 through P3. Assume that the maximum claim approach to deadlock avoidance is used. The current state is given by: Allocation Max R 0 R 1 R 2 R 3 R 0 R 1 R 2 R 3 P P P P Which of the following outstanding requests can be safely satisfied? Request R 0 R 1 R 2 R 3 P P P P