Lecture 29 Syed Mansoor Sarwar Operating Systems Lecture 29 Syed Mansoor Sarwar
© Copyright Virtual University of Pakistan Agenda for Today Review of previous lecture Deadlock detection: resources with single and multiple instances Recovery from deadlocks Process termination Resource preemption Recap of lecture 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Review of Lecture 28 Deadlock avoidance Resource with single instances Resources with multiple instances: Banker’s algorithm 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme 12 January 2019 © Copyright Virtual University of Pakistan
Single Instance of Each Resource Type Maintain a wait-for graph Nodes are processes. Pi Pj if Pi is waiting for Pj. 12 January 2019 © Copyright Virtual University of Pakistan
Single Instance of Each Resource Type Periodically invoke an algorithm that searches for a cycle in the wait-for graph. The algorithm is expensive—it requires an order of n2 operations, where n is the number of vertices in the graph. 12 January 2019 © Copyright Virtual University of Pakistan
RAG and Wait-for Graph Resource-Allocation Graph 12 January 2019 © Copyright Virtual University of Pakistan Resource-Allocation Graph Corresponding wait-for graph
Multiple Instance of Each 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. 12 January 2019 © Copyright Virtual University of Pakistan
Multiple Instance of Each Resource Type Request: An n x m matrix indicates the current request of each process. If Request[i,j]=k, then process Pi is requesting k more instances of resource type. Pj. 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Detection Algorithm Let Work and Finish be vectors of length m and n, respectively Initialize: Work = Available; for i = 1, 2, …, n if Allocationi 0 then Finish[i] = false; else Finish[i] = true; 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Detection Algorithm Find an index i such that both: (a) Finish[i] == false (b) Requesti Work If no such i exists, go to step 4. 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Detection Algorithm Work = Work + Allocationi Finish[i] = true go to step 2. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked. 12 January 2019 © Copyright Virtual University of Pakistan
Example of Detection Algorithm Consider the following system: P = { P0, P1, P2, P3, P4 } R = { A, B, C } A: 7 instances B: 2 instances C: 6 instances 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C Finish Sequence: <> 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 Finish Sequence: <P0> 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 Finish Sequence: <P0, P2> 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 5 Finish Sequence: < P0, P2, P3> 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 5 Finish Sequence: < P0, P2, P3, P4 > 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Final finish sequence: < P0, P2, P3, P4 , P1 > Other possible finish sequences: < P0, P2, P3, P1 , P4 > < P0, P2, P4, P1 , P3 > < P0, P2, P4, P3 , P1 > … 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State P2 requests an additional instance of C. Do we have a finish sequence? Process P0 P1 P2 P3 P4 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2 Work A B C Finish Sequence: <> 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2 Work A B C 1 Finish Sequence: <P0> 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example System State P0’s request can be satisfied with currently available resources, but request for no other process can be satisfied after that. Deadlock exists, consisting of processes P1, P2, P3, and P4. 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Detection Algorithm How often should the detection algorithm be invoked? Every time a request for allocation cannot be granted immediately—expensive but process causing the deadlock is identified, along with processes involved in deadlock 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Detection Algorithm Periodically, or based on CPU utilization Arbitrarily—there may be many cycles in the resource graph and we would not be able to tell which of the many deadlocked processes “caused” the deadlock. 12 January 2019 © Copyright Virtual University of Pakistan
Recovery from Deadlock: Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. 12 January 2019 © Copyright Virtual University of Pakistan
Recovery from Deadlock: Process Termination In which order should we choose to abort processes? Priority of a process. How long the process has computed, and how much longer to completion. 12 January 2019 © Copyright Virtual University of Pakistan
Recovery from Deadlock: Process Termination Resources the process has used. Resources the process needs to complete. How many processes will need to be terminated. Is the process interactive or batch? 12 January 2019 © Copyright Virtual University of Pakistan
Recovery from Deadlock: Resource Preemption 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 rollbacks in cost factor. 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Recap of Lecture Deadlock detection Recovery from deadlock Process termination Resource preemption 12 January 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Operating Systems Lecture 29 Syed Mansoor Sarwar 12 January 2019 © Copyright Virtual University of Pakistan