Deadlocks Detection
Course: Distributed Computing Faculty: Dr. Rajendra Prasath
Spring 2019
Deadlocks Detection Course: Distributed Computing Faculty: Dr. - - PowerPoint PPT Presentation
Deadlocks Detection Course: Distributed Computing Faculty: Dr. Rajendra Prasath Spring 2019 About this topic This course covers various concepts in Mutual Exclusion in Distributed Systems. We will also focus on different types of distributed
Spring 2019
This course covers various concepts in Mutual Exclusion in Distributed Systems. We will also focus on different types of distributed mutual exclusion algorithms in distributed contexts and their analysis
2
Rajendra, IIIT Sri City
è Challenges in Message Passing systems è Distributed Sorting è Space-Time Diagram è Partial Ordering / Causal Ordering è Concurrent Events è Local Clocks and Vector Clocks è Distributed Snapshots è Termination Detection è Topology Abstraction and Overlays è Leader Election Problem in Rings è Message Ordering / Group Communications è Distributed Mutual Exclusion Algorithms
Rajendra, IIIT Sri City
3
Rajendra, IIIT Sri City
4
Let us explore deadlock detection, prevention and avoidance algorithms in distributed systems
Rajendra, IIIT Sri City
5
è No Deadlocks – No processes should be permanently blocked, waiting for messages (Resources) from other sites è No starvation – no site should have to wait indefinitely to enter its critical section, while other sites are executing the CS more than once è Fairness - requests honored in the order they are made. This means processes have to be able to agree on the
è Fault Tolerance – the algorithm is able to survive a failure at one or more sites
6
Rajendra, IIIT Sri City
è Vehicular Traffic at a signal Deadlock Possible Deadlocked 7
Rajendra, IIIT Sri City
è Vehicular Traffic – Another Scenario 8
Rajendra, IIIT Sri City
è Vehicular Traffic – A real-time scenario 9
Rajendra, IIIT Sri City
10
Rajendra, IIIT Sri City
è Each philosopher must alternately think and eat è A philosopher can only eat when they have both left and right forks è Problem: How to design a discipline of behavior (a concurrent algorithm) such that no philosopher will starve?
11
Rajendra, IIIT Sri City
è Soln - 1: Forks will be numbered 1 through 5 and each philosopher will always pick up the lower-numbered fork first, and then the higher- numbered fork è Soln - 2: Use Arbitrator (waiter) to grant permission to pick up both forks
Definition
è A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set è No process can progress in the system è Competing processes may WAIT indefinitely for resources è How do we manage resources among the competing tasks efficiently?
12
Rajendra, IIIT Sri City
Tape Drives
è Assume that a system has two Tape Drives è There are two processes P1 and P2 each hold one drive è Now each process needs access to another tape drive è P1 does not get access to the resource held by P2 and vice versa. è This implies DEADLOCK … neither P1 nor P2 succeeds in its attempt
13
Rajendra, IIIT Sri City
Semaphores
è Semaphores A and B P1 P2 wait (A) wait(B) OR wait (B) wait(A) è This implies DEADLOCK … neither P1 nor P2 succeeds in its attempt
14
Rajendra, IIIT Sri City
è Mutual exclusion - only one process at a time can use a resource è Hold and wait - a process holding at least one resource is waiting to acquire additional resources held by other processes. è No preemption - a resource can be released only voluntarily by the process holding it, after that process has completed its task. è Circular wait - there exists a set { P0, P1, , P0 } of waiting processes such that Pi is waiting for a resource that is held by Pj (mod n) where n is the total number of resources
15
Rajendra, IIIT Sri City
è Resource types: R1, R2, . . . , Rm
è CPU cycles, memory space, I/O devices
è Each resource type Ri has Wi instances. è Each process utilizes a resource as follows:
è REQUEST è USE (Critical Section) è RELEASE
è Recall - Distributed Exclusion Algorithms 16
Rajendra, IIIT Sri City
è A set of vertices V and a set of edges E è V is partitioned into two types:
è Set consisting of all processes P = {P1, P2, …, Pn} è Set consisting of all resource types R = {R1, R2, …, Rm}
è request edge – directed edge Pi → Rj è assignment edge – directed edge Rj → Pi
17
Rajendra, IIIT Sri City
contd)
è Process è Resource type with 4 instances è Pi requests an instance of Rj
18
Rajendra, IIIT Sri City
Pi Pi
Rj Rj
è R1 – 1 unit è R2 – 2 units è R3 – 1 unit è R4 – 3 units
è P1, P2, P3 19
Rajendra, IIIT Sri City
è P1 needs R1 which in turn used by P2 and P2 is requesting R3 which is currently being accessed by P3 and P3 needs R2 which is being locked by P1 and P2
20
Rajendra, IIIT Sri City
è P2 and P4 may release the resource R3 in finite time as they do not depend on other competing processes è There exists a cycle but may not be a deadlock !! 21
Rajendra, IIIT Sri City
è if only one instance per resource type, then deadlock è if several instances per resource type, possibility of deadlock 22
Rajendra, IIIT Sri City
è Ensure that the system will never enter a deadlock state
è Deadlock Prevention – Stop before it happens! è Deadlock Avoidance – Precautions !! è Deadlock Detection – How to overcome?
è Allow the system to enter a deadlock state and then recover è Ignore the problem and pretend that deadlocks never occur in the system; used by most
23
Rajendra, IIIT Sri City
4 Conditions to occur Deadlocks: è Mutual Exclusion – Exclusive access – when a process accesses a resource, it is granted exclusive use of that resource è Hold and wait – a process is allowed to hold onto some resources while waiting for other resources è No preemption – a process cannot preempt or take away the resources held by another process è Cyclical wait – There is a circular chain of waiting processes, each waiting for a resource held by the next process in the chain
24
Rajendra, IIIT Sri City
Requires that the system has some additional a priori information available è Simplest and most useful model requires that each process declare the maximum number of resources
è Resource-allocation state is defined by the number
maximum demands of the processes è The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition
25
Rajendra, IIIT Sri City
è When a process requests an available resource, system must decide whether the allocation immediate leaves the system in a Safe State? Safe State? è System is in Safe State Safe State if there exists a sequence <P1, P2, …, Pn> of ALL processes such that for each Pi, the resources that Pi can still request, can be satisfied by available resources + resources held by all Pj, j < I è If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished è When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate è When Pi terminates, Pi +1 can get resources and so on
26
Rajendra, IIIT Sri City
27
Rajendra, IIIT Sri City
è Illustration of safe, unsafe and deadlock state 28
Rajendra, IIIT Sri City
29
Rajendra, IIIT Sri City
è Use a resource-allocation graph
è Use the banker’s algorithm 30
Rajendra, IIIT Sri City
è Claim edge Pi → Rj indicate: (i) process Pj may request resource Rj (ii) represented by a dashed line è Claim edge converts to Request edge when a process requests for a resource è Request edge converted to an assignment edge when the resource is allocated to the process è When a resource is released by a process, assignment edge reconverts to a claim edge è Resources must be claimed a priori in the system
31
Rajendra, IIIT Sri City
è An Example Unsafe state 32
Rajendra, IIIT Sri City
33
Rajendra, IIIT Sri City
è Multiple instances of Resources è Each process must a priori claim maximum use è When a process requests a resource it may have to wait è When a process gets all its resources it must return them in a finite amount of time 34
Rajendra, IIIT Sri City
Let n = number of processes; m = number of resources types; è Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available è Max: n x m matrix. If Max[i,j] = k, then process Pi may request at most k instances of resource type Rj è Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj è Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task Need [i,j] = Max[i,j] – Allocation [i,j]
35
Rajendra, IIIT Sri City
è (1) Let Work and Finish be vectors of length m and n respectively. Initialize: Work = Available and Finish [i] = false for i = 0, 1, …, n- 1 è (2) Find an i such that both: (a) Finish [i] = false; (b) Needi ≤ Work. If no such i exists, go to step 4 è (3) Work = Work + Allocationi Finish[i] = true; go to step 2 è (4) If Finish [i] == true for all i, then the system is in a safe state 36
Rajendra, IIIT Sri City
i
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type Rj 1. If Requesti ≤ Needi then go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim 2. If Requesti ≤ Available, then go to step 3. Otherwise Pi must wait, since resources are not available 3. Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available – Requesti; Allocationi = Allocationi + Requesti; Needi = Needi – Requesti;
è If safe ⇒ the resources are allocated to Pi è If unsafe ⇒ Pi must wait, and the old resource- allocation state is restored
37
Rajendra, IIIT Sri City
è 5 processes P0 through P4 è 3 resource types:
è A (10 instances), B (5instances), and C (7 instances)
è Snapshot at time T0: Allocation Max Available A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3
38
Rajendra, IIIT Sri City
è Need matrix is defined to be as follows: Need = Max – Allocation Need A B C P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 è The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria
39
Rajendra, IIIT Sri City
è Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true Allocation Need Available A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 è Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety requirement è Can request for (3,3,0) by P4 be granted? è Can request for (0,2,0) by P0 be granted? 40
Rajendra, IIIT Sri City
41
Rajendra, IIIT Sri City
è Maintain wait-for graph è Nodes are processes è Pi → Pj if Pi is waiting for Pj è 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 n2 operations, where n is the number of vertices in the graph 42
Rajendra, IIIT Sri City
Resource allocation Graph Wait For Graph (WFG)
43
Rajendra, IIIT Sri City
è Available: A vector of length m indicates the number of available resources of each type è Allocation: An n x m matrix defines the number
each process è 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 Rj. 44
Rajendra, IIIT Sri City
è (1) Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b) For i = 1,2, …, n, if Allocationi ≠ 0, then Finish[i] = false; else Finish[i] = true; è (2) Find an index i such that both: (a) Finish[i] == false (b) Requesti ≤ Work If no such i exists, go to step 4 45
Rajendra, IIIT Sri City
contd) è (3) Work = Work + Allocationi 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 Pi is deadlocked Algorithm requires an order of O(m x n2) operations to detect whether the system is in deadlocked state 46
Rajendra, IIIT Sri City
è Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6 instances) è Snapshot at time T0: Allocation Request Available A B C A B C A B C P0 0 1 0 0 0 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2 è Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
47
Rajendra, IIIT Sri City
è P2 requests an additional instance of type C Request A B C P0 0 0 0 P1 2 0 2 P2 0 0 1 P3 1 0 0 P4 0 0 2 è State of system? è Can reclaim resources held by process P0, but insufficient resources to fulfill other processes’ requests è Deadlock exists, consisting of processes P1, P2, P3, and P4
48
Rajendra, IIIT Sri City
è 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. 49
Rajendra, IIIT Sri City
è 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?
50
Rajendra, IIIT Sri City
51
Rajendra, IIIT Sri City
è Distributed Deadlock Detection è http://www.cse.scu.edu/~jholliday/dd_9_16.htm è Coffman et. al., System Deadlocks, ACM Computing
10.1145/356586.356588 è Havender, James W., Avoiding deadlock in multitasking systems, IBM Systems Journal. 7 (2) (1968): 74. DOI: 10.1147/sj.72.0074 è Knapp, Edgar, Deadlock detection in distributed databases, ACM Computing Surveys, 19 (4) (1987): 303–
Rajendra, IIIT Sri City
52
è Deadlock Prevention è Deadlock Avoidance è Deadlock Detection è Resource Allocation Graphs è Banker’s Algorithm è Recovery from Deadlocks è Performance Metrics
è Stay tuned ... More to come up … !!
Rajendra, IIIT Sri City
53
rajendra [DOT] prasath [AT] iiits [DOT] in
è http://www.iiits.ac.in/FacPages/index- rajendra.html OR è http://rajendra.2power3.com 54
Rajendra, IIIT Sri City
and above)
and less than 8.5)
work will also be rewarded)
learning by helping the needy students
55
Rajendra, IIIT Sri City
Rajendra, IIIT Sri City
56