# Washington WASHINGTON UNIVERSITY IN ST LOUIS Concurrency: Deadlock Detection Fred Kuhns Department.

## Presentation on theme: "Washington WASHINGTON UNIVERSITY IN ST LOUIS Concurrency: Deadlock Detection Fred Kuhns Department."— Presentation transcript:

Washington WASHINGTON UNIVERSITY IN ST LOUIS Concurrency: Deadlock Detection Fred Kuhns (fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk) Department of Computer Science and Engineering Washington University in St. Louis

2 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme

3 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization 1 Instance of each Resource Type Maintain wait-for graph –Nodes are processes. –P i P j if P i is waiting for P j to release some resource R k Periodically invoke an algorithm that searches for a cycle in the graph. 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.

4 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Wait-for Graph R1R1 R2R2 P1P1 P2P2 P3P3 R3R3 R4R4 P4P4 P0P0 Resource Allocation Graph P1P1 P2P2 P3P3 P4P4 P0P0 Wait-For Graph

5 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization >1 Instance of each Resource Type Assume m resource types and n processes Available[i]: A vector of length m indicates the number of available resources of each type. Allocation[i,j]: An n x m matrix defines the number of resources of each type currently allocated to each process. –Allocation[i, j] = number of resources of type j currently allocated to Process i. –Allocation i = allocation vector for Process i Request[i,j]: An n x m matrix indicates the current per process requist vector. –Request[i,j] = number of resources of type j requested by Process i. –Request i = request vector for Process i

6 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Detection Algorithm 0. Work and Finish are vectors of length m and n, respectively. Initialize: (a) Work = Available (b) if Allocation i 0, then Finish[i] = false; else Finish[i] = true. 1. Find an index i such that both: (a) Finish[i] == false// has allocated resources (b) Request i Work// request can be filled If no such i exists, go to step 4. 2. Work = Work + Allocation i ; Finish[i] = true; go to step 1 3. 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.

7 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Detection Algorithm (Cont.) Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state.

8 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Example of Detection Algorithm Five processes P 0 through P 4 three resource types A (7), B (2), and C (6). Snapshot at time T 0 : AllocationRequestAvailable A B C A B C A B C P 0 0 1 00 0 00 0 0 P 1 2 0 02 0 2 P 2 3 0 30 0 0 P 3 2 1 11 0 0 P 4 0 0 20 0 2 Sequence will result in Finish[i] = true for all i.

9 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Step 0 Total Resource {A = 7, B = 2, C = 6} AllocationRequestAvailable A B C A B C A B C P 0 0 1 00 0 00 0 0 P 1 2 0 02 0 2 P 2 3 0 30 0 0 P 3 2 1 11 0 0 P 4 0 0 20 0 2 Step 0: –Work = Available = {0, 0, 0} –Finish = {false, false, false, false, false}

10 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Steps 1, 2 and 3 AllocationRequest A B C P 0 0 1 00 0 0 P 1 2 0 02 0 2 P 2 3 0 30 0 0 P 3 2 1 11 0 0 P 4 0 0 20 0 2 Steps 1 & 2: Request 0 < work // {0 0 0} Work = {0 1 0} Finish = {true, false, false, false, false} Request 2 < work // {0 1 0} Work = {3 1 3} Finish = {true, false, true, false, false} Request 1 < work // {3 1 3} Work = {5 1 3} Finish = {true, true, true, false, false} Request 3 < work // {5 1 3} Work = {7 2 4} Finish = {true, true, true, true, false} Request 4 < work // {7 2 4} Work = {7 2 6} Finish = {true, true, true, true, true} Step 3: No deadlock since all elements of Finish = true

11 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization P 2 requests an additional instance of type C AllocationRequestAvailable A B C A B C A B C P 0 0 1 00 0 00 0 0 P 1 2 0 02 0 2 P 2 3 0 30 0 1 P 3 2 1 11 0 0 P 4 0 0 20 0 2 State of system? –Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. –Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4. New Request from P 2

12 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization 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 process 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.

13 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Recovery: Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. 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?

14 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Recovery: Resource Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process for that state. Starvation – same process may always be picked as victim, include number of rollback in cost factor.

15 Fred Kuhns (6/13/2014)Cs422 – Operating Systems Organization Combined Approach Combine the three basic approaches –prevention –avoidance –detection allowing the use of the optimal approach for each resource type in the system. Partition resources into hierarchically ordered classes. Use most appropriate technique for handling deadlocks within each class.