Deadlock Detection & recovery CSSE 332 Operating Systems Rose-Hulman Institute of Technology
The deadlock problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set Example System has 2 disk drives P1 and P2 each holds one disk drive and each needs another one Example semaphores A and B, initialized to 1
Approaches to deadlock handling Ignore Deadlock (Ostrich approach) If infrequent enough and result is not serious Used by most operating systems, including UNIX Deadlock Prevention Prevent one of the necessary/sufficient conditions Deadlock Avoidance Allow the 3 necessary conditions Dynamically make choices to avoid deadlock decide based on knowledge of future requests i.e., find a safe path
Approaches to deadlock handling Deadlock Detection Periodically run algorithm to detect circular waiting After detecting deadlock, run a recovery algorithm to remove deadlock
Deadlock Humor Two drivers were approaching a one- lane bridge. They reached the middle of the bridge at the same time, forcing a confrontation. They got out of their cars and went nose to nose. One driver said, I don t back up for idiots. The other driver calmly got back in his car and started backing up. He said, No problem - - - I do. (Deitel et al) Moral of the story: Processes are neither stubborn nor co- operative, so deadlock is a painful experience at best. Usually, one or more processes will have to be backed out of the deadlock, losing some or all of their work.
Deadlock detec8on and recovery Allow system to enter deadlock state Detection algorithm Recovery scheme Q1
Single Instance of Each Resource Type Maintain wait-for graph" Nodes are processes" P i P j if P i is waiting for P j " Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock!" " An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph"
RAG and Wait-for Graph Resource-Allocation Graph" Corresponding wait-for graph"
Multiple Instances of a Resource Type Available: A vector of length m indicates the number of available resources of each type. " Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. " Request: An n x m matrix indicates the current request of each process. If Request [i j ] = k, then process P i is requesting k more instances of resource type R j."
Detection Algorithm 1."Let Work and Finish be vectors of length m and n, respectively Initialize:" (a) Work = Available" (b)"for i = 1,2,, n, if Allocation i 0, then Finish[i] = false; otherwise, Finish[i] = true" 2."Find an index i such that both:" (a)"finish[i] == false" (b)"request i Work " If no such i exists, go to step 4"
Detection Algorithm (Cont.) 3."Work = Work + Allocation i Finish[i] = true go to step 2 " 4."If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then P i is deadlocked" "" Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state"
Example of Detection Algorithm Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances)" Snapshot at time T 0 :" " " " Allocation " "Request "Available" " " "A B C " A B C " "A B C" " "P 0 " 0 1 0 0 0 0 " "0 0 0" " "P 1 2 0 0 " 2 0 2" " "P 2 " 3 0 3 0 0 0 " " "P 3 2 1 1 " 1 0 0 " " "P 4 0 0 2 " 0 0 2" Is the system in a deadlock state?" "
Example (Cont.) P 2 requests an additional instance of type C" " " "Request" " " "A B C" " " P 0 "0 0 0" " " P 1 "2 0 2" " "P 2 "0 0 1" " "P 3 "1 0 0 " " "P 4 "0 0 2" State of system?"
Detection-Algorithm Usage When, and how often, to invoke depends on:" How often a deadlock is likely to occur?" How many processes will need to be rolled back?" one for each disjoint cycle " If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes caused the deadlock"
Recovery: Process Termina8on Abort all deadlocked processes Abort one process at a time until no deadlock In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated? Is process interactive or batch?
Recovery: Resource Preemp8on Selecting a victim minimize cost Rollback return to some safe state, restart process from that state Starvation same process may always be picked as victim include number of rollback in cost factor
Deadlock should not be ignored A real- time control system monitoring a gasoline refinery Need to ensures the safe and proper operation of the refinery An avoidance strategy or prevention strategy must be employed A computerized pacemaker Can t afford to miss a single beat Deadlock avoidance or prevention must be employed Q6
Deadlock should not be ignored Embedded systems cell phones, PDAs. These are SoCs or system on chips i.e. everything is on one chip and SoCs are characterized by a small set of resources and real- time processing constraints. Can t have a system administrator come in and decide which process to kill after deadlock has been detected. Either a prevention or avoidance strategy must be employed.
Dining Philosopher Problem Five philosophers who alternately think and eat Share a fork with each neighbor Assume each philosopher picks up left fork, then right fork, then eats Deadlock if all are hungry at the same time
Solu8ons: Dining Philosophers Problem Buy more Forks Equivalent to increasing resources Put fork down if 2nd fork busy Can produce livelock if philosophers stay synchronized Room Attendant Only let 4 of the philosophers into the room at once May have 4 philosophers in room, but only 1 might eat Mix order of grabbing forks Some grab forks in the other order (right fork, then left fork) Any mix will avoid deadlock
Correct solu8on with semaphores semaphore fork[5] = {1, 1, 1, 1, 1; semaphore room = 4; process philosopher(int i) { while(true) { think(); wait (room); wait(fork[i]); wait(fork[(i+1) mod 5]); eat(); signal(fork[(i+1) mod 5]); signal(fork[i]); signal (room); void main() { int i; for (i =0; i < 5; i++) { philosopher(i);
Incorrect solu8on with semaphores semaphore fork[5] = {1,1,1,1,1; process philosopher(int i){ while(true){ think(); wait(fork[i]); wait(fork[(i+1)mod5]); eat(); signal(fork[(i+1)mod5]); signal(fork[i]); void main() { int i; for (i =0; i < 5; i++) { philosopher(i);
Another incorrect solu8on binary semaphore x = 1; process philosopher(int i){ while(true){ think(); wait(x); pick up fork[i]; pick up fork[(i+1)mod5]; signal(x); eat(); wait(x); return fork[i] ; return fork[(i+1)mod5] ; signal(x); void main() { int I; for (i =0; i < 5; i++) { philosopher(i);