Deadlocks Detection Course: Distributed Computing Faculty: Dr. - - PowerPoint PPT Presentation

deadlocks detection
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Deadlocks Detection

Course: Distributed Computing Faculty: Dr. Rajendra Prasath

Spring 2019

slide-2
SLIDE 2

About this topic

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

slide-3
SLIDE 3

What did you learn so far? What did you learn so far?

è 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

RECAP

Rajendra, IIIT Sri City

3

slide-4
SLIDE 4

Topics to focus on

  • pics to focus on …

è Distributed Mutual Exclusion

è Deadlock Detection

è Check pointing and rollback recovery è Self-Stabilization è Distributed Consensus è Reasoning with Knowledge è Peer – to – peer computing and Overlays è Authentication in Distributed Systems

For End Semester

Rajendra, IIIT Sri City

4

slide-5
SLIDE 5

Deadlocks

Let us explore deadlock detection, prevention and avoidance algorithms in distributed systems

Rajendra, IIIT Sri City

5

slide-6
SLIDE 6

Distributed Mutual Exclusion (r Distributed Mutual Exclusion (recap) ecap)

è 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

  • rder of events. (Fairness prevents starvation)

è Fault Tolerance – the algorithm is able to survive a failure at one or more sites

6

Rajendra, IIIT Sri City

slide-7
SLIDE 7

Deadlock Deadlock – A Simple Example Simple Example

è Vehicular Traffic at a signal Deadlock Possible Deadlocked 7

Rajendra, IIIT Sri City

slide-8
SLIDE 8

Deadlock Deadlock – Another Example Another Example

è Vehicular Traffic – Another Scenario 8

Rajendra, IIIT Sri City

slide-9
SLIDE 9

Deadlock Deadlock – Illustr Illustrated ated

è Vehicular Traffic – A real-time scenario 9

Rajendra, IIIT Sri City

slide-10
SLIDE 10

Dining Philosophers’ P Dining Philosophers’ Problem

  • blem

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?

è Suggest a Simple Solution ??

slide-11
SLIDE 11

Dining Philosophers’ P Dining Philosophers’ Problem

  • blem

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

è Deadlock-Free Solutions !!

slide-12
SLIDE 12

Deadlocks in Distributed Deadlocks in Distributed Sytems Sytems

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

slide-13
SLIDE 13

Deadlocks Deadlocks – A few mor few more examples e examples

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

slide-14
SLIDE 14

Deadlocks Deadlocks – A few mor few more examples e examples

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

slide-15
SLIDE 15

Deadlock Deadlock – Char Characterization acterization

è 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

slide-16
SLIDE 16

System Model System Model

è 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

slide-17
SLIDE 17

Resour Resource ce Allocation Gr Allocation Graph (RA aph (RAG) G)

è 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

slide-18
SLIDE 18

Resour Resource ce Allocation Gr Allocation Graph aph (contd

contd)

è Process è Resource type with 4 instances è Pi requests an instance of Rj

è Pi is holding an instance of Rj

18

Rajendra, IIIT Sri City

Pi Pi

Rj Rj

slide-19
SLIDE 19

RA RAG G – An example An example

è Look at this graph è Resources:

è R1 – 1 unit è R2 – 2 units è R3 – 1 unit è R4 – 3 units

è Requests:

è P1, P2, P3 19

Rajendra, IIIT Sri City

slide-20
SLIDE 20

RA RAG with a Deadlock G with a Deadlock

è Look at this graph

è 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

è This implies Deadlock

20

Rajendra, IIIT Sri City

slide-21
SLIDE 21

RA RAG with a cycle but NO Deadlock G with a cycle but NO Deadlock

è Look at this graph

è 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

slide-22
SLIDE 22

Basic F Basic Facts acts

è If graph contains no cycles ⇒ no deadlock è If graph contains a cycle ⇒

è if only one instance per resource type, then deadlock è if several instances per resource type, possibility of deadlock 22

Rajendra, IIIT Sri City

slide-23
SLIDE 23

How to handle Deadlocks? How to handle Deadlocks?

è 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

  • perating systems, including UNIX

23

Rajendra, IIIT Sri City

slide-24
SLIDE 24

Deadlock P Deadlock Prevention evention

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

slide-25
SLIDE 25

Deadlock Deadlock Avoidance Avoidance

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

  • f each type that it may need

è Resource-allocation state is defined by the number

  • f available and allocated resources, and the

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

slide-26
SLIDE 26

Safe State Safe State

è 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

slide-27
SLIDE 27

Basic F Basic Facts acts

è If a system is in safe state ⇒ no deadlocks è If a system is in unsafe state ⇒ possibility of deadlock è Avoidance ⇒ ensure that a system will never enter an unsafe state

27

Rajendra, IIIT Sri City

slide-28
SLIDE 28

Safe / Unsafe / Deadlock State Safe / Unsafe / Deadlock State

è Illustration of safe, unsafe and deadlock state 28

Rajendra, IIIT Sri City

slide-29
SLIDE 29

29

Rajendra, IIIT Sri City

slide-30
SLIDE 30

Avoidance Avoidance Algorithms Algorithms

è Single instance of a resource type

è Use a resource-allocation graph

è Multiple instances of a resource type

è Use the banker’s algorithm 30

Rajendra, IIIT Sri City

slide-31
SLIDE 31

Resour Resource-Allocation Gr ce-Allocation Graph Scheme aph Scheme

è 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

slide-32
SLIDE 32

Resour Resource-Allocation Gr ce-Allocation Graph aph

è An Example Unsafe state 32

Rajendra, IIIT Sri City

slide-33
SLIDE 33

Resour Resource-Allocation Gr ce-Allocation Graph aph Algorithm Algorithm

è Suppose that process Pi requests a resource Rj è The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph

33

Rajendra, IIIT Sri City

slide-34
SLIDE 34

Bank Banker er’s s Algorithm Algorithm

è 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

slide-35
SLIDE 35

Data Structur Data Structures - Bank es - Banker er’s s Algorithm Algorithm

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

slide-36
SLIDE 36

Safety Safety Algorithm Algorithm

è (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

slide-37
SLIDE 37

Resour Resource-Request ce-Request Algo Algo for P for Process P

  • cess Pi

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

slide-38
SLIDE 38

Example of Example of Bank Banker er’s s Algorithm Algorithm

è 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

slide-39
SLIDE 39

Example Example (contd

contd)

è 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

slide-40
SLIDE 40

Example: P Example: P1 Request (1,0,2) Request (1,0,2)

è 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

slide-41
SLIDE 41

Deadlock Detection Deadlock Detection

è Allow system to enter deadlock state è Detection Algorithm è Recovery Scheme

41

Rajendra, IIIT Sri City

slide-42
SLIDE 42

Single Instance of Single Instance of Each Resour Each Resource ce Type ype

è 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

slide-43
SLIDE 43

Resour Resource-Allocation / W ce-Allocation / Wait-for Gr ait-for Graph aph

Resource allocation Graph Wait For Graph (WFG)

43

Rajendra, IIIT Sri City

slide-44
SLIDE 44

Sever Several Instances of al Instances of a Resour a Resource ce Type ype

è Available: A vector of length m indicates the number of available resources of each type è Allocation: An n x m matrix defines the number

  • f 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 Pi is requesting k more instances of resource type Rj. 44

Rajendra, IIIT Sri City

slide-45
SLIDE 45

Detection Detection Algorithm 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 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

slide-46
SLIDE 46

Detection Detection Algorithm Algorithm (contd

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

slide-47
SLIDE 47

Detection Detection Algorithm - Example Algorithm - Example

è 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

slide-48
SLIDE 48

Detection Detection Algorithm Algorithm – Example Example (contd

contd)

è 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

slide-49
SLIDE 49

Detection-Algorithm Usage 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. 49

Rajendra, IIIT Sri City

slide-50
SLIDE 50

Recovery fr Recovery from Deadlock: P

  • m Deadlock: Process
  • cess

Termination ermination

è 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

slide-51
SLIDE 51

Recovery fr Recovery from Deadlock: r

  • m Deadlock: resour

esource ce Preemption eemption

è 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

51

Rajendra, IIIT Sri City

slide-52
SLIDE 52

Resour Resource Links ce Links

è Distributed Deadlock Detection è http://www.cse.scu.edu/~jholliday/dd_9_16.htm è Coffman et. al., System Deadlocks, ACM Computing

  • Surveys. 3 (2) (1971): 67–78. DOI:

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–

  • 328. DOI:10.1145/45075.46163. ISSN 0360-0300

Rajendra, IIIT Sri City

52

slide-53
SLIDE 53

Summary Summary

è Deadlocks

è 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

slide-54
SLIDE 54

How to r How to reach me? each me?

è Please leave me an email:

rajendra [DOT] prasath [AT] iiits [DOT] in

è Visit my homepage @

è http://www.iiits.ac.in/FacPages/index- rajendra.html OR è http://rajendra.2power3.com 54

Rajendra, IIIT Sri City

slide-55
SLIDE 55
  • Perspective Students (having CGPA above 8.5

and above)

  • Promising Students (having CGPA above 6.5

and less than 8.5)

  • Needy Students (having CGPA less than 6.5)
  • Can the above group help these students? (Your

work will also be rewarded)

  • You may grow a culture of collaborative

learning by helping the needy students

Help among Yourselves?

55

Rajendra, IIIT Sri City

slide-56
SLIDE 56

… Questions ???

Thanks …

Rajendra, IIIT Sri City

56