160 likes | 411 Views
Deadlock Avoidance Technique. Resource Allocation Denial: Grant incremental resource requests if we can prove that this leaves the system in a state in which deadlock cannot occur . Based on the concept of a “safe state” Banker’s Algorithm: Tentatively grant each resource request
E N D
Deadlock Avoidance Technique • Resource Allocation Denial: • Grant incremental resource requests if we can prove that this leaves the system in a state in which deadlock cannot occur. • Based on the concept of a “safe state” • Banker’s Algorithm: • Tentatively grant each resource request • Analyze resulting system state to see if it is “safe”. • If safe, grant the request • if unsafe refuse the request (undo the tentative grant) • block the requesting process until it is safe to grant it. Chapter 8
Data Structures for the Banker’s Algorithm Let n = number of processes, m = number of resource types • Available: Vector of length m. If Available [j] = k, there are k instances of resource type Rjcurrently available • Max: n x m matrix. If Max [i,j] = k, then process Piwill request at most kinstances of resource type Rj. • Alloc: n x m matrix. If Alloc[i,j] = k then Pi is currently allocated (i.e. holding) k instances of Rj. • Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rjto complete its task. Need [i,j] = Max[i,j] – Alloc [i,j].
Safety Algorithm • Let Work and Finish be vectors of length m and n, respectively. Initialize: • Work := Available • Finish [i] == false for i = 1,2, …, n. • 2. Find an i such that both: • Finish [i] == false • Needi Work • If no such i exists, go to step 4. • Work := Work + Allocationi(Resources freed when process completes!) • Finish[i] := true • go to step 2. • 4. If Finish [i] = true for all i, then the system is in a safe state. Chapter 8
Resource-Request Algorithm for Process Pi • Requesti = request vector for Pi . • Requesti [j] = k means process Pi wants k instances of resource • type Rj. • 1. If Requesti Needi go to step 2. Otherwise, error ( process exceeded its maximum claim). • 2. If Requesti Available, go to step 3. Otherwise Pi must wait, (resources not available). • 3. “Allocate” requested resources to Pi as follows: • Available := Available - Requesti • Alloci := Alloci + Requesti • Needi := Needi – Requesti • If safe the resources are allocated to Pi. • If unsafe restore the old resource-allocation state and block Pi Chapter 8
Example of Banker’s Algorithm 5 processes P0 through P4 3 resource types A (10 units), B (5 units), and C (7 units). Snapshot at time T0: AllocationMaxAvailable A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 Chapter 8
Example (cont) Need = Max – Allocation Need A B C P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria. Chapter 8
Now P1 requests (1,0,2) Check that Request Available (that is, (1,0,2) (3,3,2)) true. AllocationNeedAvailable A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement. Can request for (3,3,0) by P4 be granted? (no!) Can request for (0,2,0) by P0 be granted? (no, unsafe) Chapter 8
Banker’s algorithm: Comments • A safe state cannot be deadlocked. But an unsafe state is not necessarily deadlocked. • So if we withhold resources because the system would be “unsafe”: • some processes may need to wait unnecessarily • sub optimal use of resources Chapter 8
Deadlock Detection • Resource accesses are granted to processes whenever possible (might lead to deadlock) • (No need to declare Maximum Claim) • Detect the deadlock and recover. • The OS needs: • an algorithm to check if deadlock is present • an algorithm to recover from deadlock • The deadlock check can be performed at every resource request (high overhead) • ...or less frequently Chapter 8
Deadlock Detection • Similar to Safety Algorithm, but use Request matrix instead of Max matrix. • Note that processes not holding any resources cannot be involved in a deadlock! • Like Safety, find a sequence (if possible) in which resource allocations can be granted • assume they can run to completion with these resources and then release all held resources • might need more, leading to deadlock later, but we can detect that later! • Any processes not in this sequence are deadlocked Chapter 8
Deadlock Detection Algorithm • Marks each process not deadlocked. Initially all processes are unmarked. Then perform: • Mark each process j for which: Alloc[ j,i] = 0 for all resource type i. (These are not deadlocked) • Initialize work vector: Work[i] = Available[i] for all i • REPEAT: Find a unmarked process j such that Request[ j,i ] <= Work[i] for all i. Stop if no such process exists. • If such process j exists: mark process j and set Work[i] = Work [i] + Alloc[ j,i] for all i. Goto REPEAT. • At the end: each unmarked process is deadlocked
Deadlock Detection Example Request Allocated Available R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 • Mark P4 since it has no allocated resources • Set Work = (0,0,0,0,1) • P3’s request <= Work. So mark P3 and set Work = Work + (0,0,0,1,0) = (0,0,0,1,1) • Algorithm terminates. P1 and P2 are deadlocked P1 P2 P3 P4 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 Chapter 8
Deadlock Recovery • If you detect a deadlock, you need to do something! • The following approaches are possible: • Abort all deadlocked processes (common to do this manually, inefficient) • Rollback each deadlocked process to some previously defined checkpoint and restart them • Requires checkpoint/restart procedures in OS • The original deadlock may reoccur but good chance it won’t Chapter 8
More recovery approaches • Successively abort deadlocked processes until deadlock no longer exists • (Re-invoke the deadlock detection algorithm each time) • Successively preempt some resources from processes and give them to other processes until deadlock no longer exists • a process that has a resource preempted must be rolled back to a point prior to its acquisition of the resource Chapter 8
Deadlock Recovery (cont.) • Last approaches involve choosing a “victim” process : • Possible selection criteria: • least amount of CPU time consumed so far • least total resources allocated so far • least amount of “work” produced so far • Lowest priority • Largest estimated remaining time • Dean’s processes last... Chapter 8