Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 81 Deadlock Avoidance Technique  Resource Allocation Denial: Grant incremental resource requests if we can prove that this leaves the system in.

Similar presentations


Presentation on theme: "Chapter 81 Deadlock Avoidance Technique  Resource Allocation Denial: Grant incremental resource requests if we can prove that this leaves the system in."— Presentation transcript:

1 Chapter 81 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.

2 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 R j currently available  Max: n x m matrix. If Max [i,j] = k, then process P i will request at most k instances of resource type R j.  Alloc: n x m matrix. If Alloc[i,j] = k then P i is currently allocated (i.e. holding) 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] – Alloc [i,j].

3 Chapter 83 Safety Algorithm 1. 1.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 Need i  Work If no such i exists, go to step 4. 3. 3.Work := Work + Allocation i (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.

4 Chapter 84 Resource-Request Algorithm for Process P i Request i = request vector for P i. Request i [j] = k means process P i wants k instances of resource type R j. 1.If Request i  Need i go to step 2. Otherwise, error ( process exceeded its maximum claim). 2.If Request i  Available, go to step 3. Otherwise P i must wait, (resources not available). 3.“Allocate” requested resources to P i as follows: Available := Available - Request i Alloc i := Alloc i + Request i Need i := Need i – Request i If safe  the resources are allocated to P i. If unsafe  restore the old resource-allocation state and block P i

5 Chapter 85 Example of Banker’s Algorithm 5 processes P 0 through P 4 3 resource types A (10 units), B (5 units), and C (7 units). Snapshot at time T 0 : AllocationMaxAvailable A B CA B C A B C P 0 0 1 07 5 3 3 3 2 P 1 2 0 0 3 2 2 P 2 3 0 2 9 0 2 P 3 2 1 1 2 2 2 P 4 0 0 24 3 3

6 Chapter 86 Example (cont) Need = Max – Allocation Need A B C P 0 7 4 3 P 1 1 2 2 P 2 6 0 0 P 3 0 1 1 P 4 4 3 1 The system is in a safe state since the sequence satisfies safety criteria.

7 Chapter 87 Now P 1 requests (1,0,2) Check that Request  Available (that is, (1,0,2)  (3,3,2))  true. AllocationNeedAvailable A B CA B CA B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 20 2 0 P 2 3 0 2 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Executing safety algorithm shows that sequence satisfies safety requirement. Can request for (3,3,0) by P 4 be granted? (no!) Can request for (0,2,0) by P 0 be granted? (no, unsafe)

8 Chapter 88 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

9 Chapter 89 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

10 Chapter 810 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

11 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

12 Chapter 812 Deadlock Detection Example  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 R1 R2 R3 R4 R5 P1 P2 P3 P4 Request Allocated Available R1 R2 R3 R4 R5 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

13 Chapter 813 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

14 Chapter 814 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

15 Chapter 815 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...


Download ppt "Chapter 81 Deadlock Avoidance Technique  Resource Allocation Denial: Grant incremental resource requests if we can prove that this leaves the system in."

Similar presentations


Ads by Google