Dealing With Deadlock Deadlock Avoidance Deadlock avoidance State matrices example A computer system with 5 processes and 4 resources Examine each resource request and determine whether or not granting the request can lead to deadlock ALLOCATION R1 R2 R 3 R4 Define a set of vectors and matrices that characterize the current state of all resources and processes ÿ resource allocation state matrix Alloc ij = the number of units of resource j held by process i ... resource j that are unallocated ... ÿ available vector Availj = the number of units of Pp np,1 ... np,r 00 11 11 00 00 00 00 33 66 00 11 00 55 33 11 R1 ... of resource j that the process i will ever require simultaneously R1 R2 R 3 ... Rr P1 n1,1 n1,2 n1,3 ... n1,r P2 n2,1 n2,2 P3 n3,1 ... ÿ maximum claim matrix Maxij = the maximum number of units P1 P2 P3 P4 P5 P1 <n1, n2, n3, ..., nr> 22 00 33 22 44 MAX_CLAIM R1 R2 R 3 R4 00 11 22 00 00 R2 P2 00 77 33 66 66 11 55 55 55 55 R3 P3 22 00 66 22 66 AVAILABLE R1 R2 R 3 R4 11 55 22 00 R4 P4 P5 12 Deadlock Avoidance 13 Deadlock Avoidance Algorithm Concept State definitions A resource allocation state is safe if the system can allocate resources to each process up to its maximum claim such that the system can not deadlock The OS will examine each resource request and determine whether or not granting the request can lead to deadlock ÿ If, after we grant this request, all processes simultaneously make their maximum claim, will the system deadlock? There must be an ordering of the processes P1, P2 , ..., Pn, such that for all processes Pi , ÿ Can we satisfy the maximum claims of processes in some order? MAX_CLAIMPi – ALLOCATIONPi ≤ AVAIL + SALLOCATIONPj Term. j ALLOCATION R1 R2 R 3 R4 P1 P2 P3 P4 P5 0 1 1 0 0 0 0 3 6 0 1 0 5 3 1 2 0 3 2 4 MAX_CLAIM R1 R2 R 3 R4 0 1 2 0 0 0 7 3 6 6 1 5 5 5 5 2 0 6 2 6 AVAILABLE R1 R2 R 3 R4 The largest request for resources that process Pi can make 1 5 2 0 The resources available to process Pi after processes P1 through P i–1 terminate This ordering of processes is called a safe sequence 14 If a safe sequence exists then there exists a process ( P1) that can execute to completion in the current state, and Pj can execute to completion at worst after processes P1 - Pj–1 complete 15 Deadlock Avoidance Algorithm Deadlock Avoidance Example Example Safe sequence computation 1. Compute the largest possible resource request a process can make MAX_CLAIM ALLOCATION MAX_REQUEST R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 P1 00 00 11 22 00 00 11 22 00 00 00 00 P2 11 77 55 00 11 00 00 00 00 77 55 00 – = P3 22 33 55 66 11 33 55 33 11 00 00 33 P4 00 66 55 22 00 66 33 22 00 00 22 00 P5 00 66 55 66 00 00 11 44 00 66 44 22 A computer system with 5 processes and 4 resources ALLOCATION R1 R2 R 3 R4 P1 00 00 11 22 P2 11 00 00 00 P3 11 33 55 33 P4 00 66 33 22 P5 00 00 11 44 MAX_CLAIM R1 R2 R 3 R4 00 00 11 22 11 77 55 00 22 33 55 66 00 66 55 22 00 66 55 66 AVAILABLE R1 R2 R 3 R4 11 55 22 00 R1 R2 R3 R4 P1 P2 P3 P4 P5 Is this system in a safe state? Does there exist a safe sequence? i–1 MAX_CLAIMPi – ALLOCATION Pi ≤ AVAIL + SALLOCATION Pj R1 R2 R3 R4 P1 P2 P3 P4 P5 2. Attempt to build a safe sequence j=1 16 17 Deadlock Avoidance Example Deadlock Avoidance Example Safe sequence computation Safe sequence computation ALLOCATION P1 P2 P3 P4 P5 R1 R2 R 3 R4 00 00 11 22 11 00 00 00 11 33 55 33 00 66 33 22 00 00 11 44 MAX_CLAIM AVAILABLE R1 R2 R 3 R4 00 00 11 22 11 77 55 00 22 33 55 66 00 66 55 22 00 66 55 66 R1 R2 R 3 R4 11 55 22 00 MAX_REQUEST R1 R2 R 3 R4 00 00 00 00 00 77 55 00 11 00 00 33 00 00 22 00 00 66 44 22 MAX_CLAIM AVAILABLE MAX_REQUEST R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 11 55 22 00 00 00 11 00 00 P1 P2 P3 P4 P5 ÿ Does there exist a process Pi such that MAX_REQUEST P ≤ AVAILABLE ? 00 11 11 00 00 00 00 33 66 00 11 00 55 33 11 22 00 00 11 33 22 22 0 4 2 0 00 44 00 00 77 33 66 66 11 55 55 55 55 22 00 66 22 66 2 1 0 0 00 77 00 00 66 00 55 00 22 44 00 00 33 001 3 3 0 22 What if P2 wants to change its allocation to <0, 4, 2, 0>? i ÿ If no, then there is no safe sequence, the state is unsafe ÿ Is the resulting allocation state safe? ÿ If yes, add Pi to the sequence ÿ Set AVAILABLE = AVAILABLE + ALLOCATION P ALLOCATION i 18 19 Deadlock Avoidance Dealing With Deadlock Banker’s algorithm (Dijkstra and Habermann) Deadlock detection & recovery When a process makes a request for resources... Deadlock prevention and avoidance: ÿ Simulate the effect of granting the process’s request ÿ Develop and use resource allocation mechanisms and protocols that prohibit deadlock ÿ Then check to see if a safe sequence exists ALLOCATION MAX_CLAIM AVAILABLE R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 0 1 1 0 0 0 1 2 0 0 1 5 2 0 0 0 1 0 0 P1 P2 P3 P4 P5 0 0 3 6 0 1 0 5 3 1 2 0 3 2 4 0 7 3 6 6 1 5 5 5 5 2 0 6 2 6 Deadlock detection and recovery: MAX_REQUEST 0 7 0 0 6 0 5 0 2 4 ÿ Let the system deadlock and then deal with it Detect that a set of processes are deadlocked Recover from the deadlock 0 0 3 0 2 20 Deadlock Detection & Recovery 21 Deadlock Detection & Recovery Detecting deadlock Detecting deadlock Run Banker’s algorithm and see if a safe sequence exists How often should the OS check for deadlock? ÿ Replace MAX_REQUEST with a “ REQUEST ” matrix ÿ After every resource request? ÿ If a safe sequence does not exist then the system is deadlocked ÿ Only when we suspect deadlock has occurred? ALLOCATION MAX_CLAIM AVAILABLE REQUEST ALLOCATION MAX_CLAIM AVAILABLE REQUEST R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 R1 R2 R 3 R4 0 1 1 0 0 0 1 2 0 0 1 5 2 0 0 0 1 0 0 0 1 1 0 0 0 1 2 0 0 1 5 2 0 0 0 1 0 0 P1 P2 P3 P4 P5 0 0 3 6 0 1 0 5 3 1 2 0 3 2 4 0 7 3 6 6 1 5 5 5 5 2 0 6 2 6 0 2 0 0 2 0 1 0 2 2 0 0 0 0 2 P1 P2 P3 P4 P5 22 0 0 3 6 0 1 0 5 3 1 2 0 3 2 4 0 7 3 6 6 1 5 5 5 5 2 0 6 2 6 0 2 0 0 2 0 1 0 2 2 0 0 0 0 2 23 Deadlock Detection & Recovery Recovering from deadlock R1 P1 R2 P2 R3 P3 R4 P4 P5 Abort all deadlocked processes & reclaim their resources Abort one process at a time until all cycles in the RAG are eliminated Where to start? ÿ Select low priority process ÿ Processes with most allocation of resources Caveat: ensure that system is in consistent state (e.g., transactions) Optimization: ÿ Checkpoint processes periodically; rollback processes to checkpointed state 24
© Copyright 2026 Paperzz