Deadlock Example Process 1 Process 2 Resource 1 Resource 2 - - PowerPoint PPT Presentation

deadlock example
SMART_READER_LITE
LIVE PREVIEW

Deadlock Example Process 1 Process 2 Resource 1 Resource 2 - - PowerPoint PPT Presentation

Deadlock Example Process 1 Process 2 Resource 1 Resource 2 Example Process 1 Process 2 Process 3 Resource 1 Resource 2 Resource 3 Addressing Deadlock Prevention: Design the system so that deadlock is impossible Avoidance:


slide-1
SLIDE 1

Deadlock

slide-2
SLIDE 2

Example

Process 1 Process 2 Resource 1 Resource 2

slide-3
SLIDE 3

Example

Process 1 Process 2 Process 3 Resource 1 Resource 2 Resource 3

slide-4
SLIDE 4

Addressing Deadlock

¥ Prevention: Design the system so that deadlock is impossible ¥ Avoidance: Construct a model of system states, then choose a strategy that will not allow the system to go to a deadlock state ¥ Detection & Recovery: Check for deadlock (periodically or sporadically), then recover ¥ Manual intervention: Have the operator reboot the machine if it seems too slow

slide-5
SLIDE 5

A Model

¥ P = {p1, p2, É, pn} be a set of processes ¥ R = {R1, R2, É, Rm} be a set of resources ¥ cj = number of units of Rj in the system ¥ S = {S0, S1, É} be a set of states representing the assignment of Rj to pi

Ð State changes when processes take action Ð This allows us to identify a deadlock situation in the operating system

slide-6
SLIDE 6

State Transitions

¥ The system changes state because of the action of some process, pi ¥ There are three pertinent actions:

Ð Request (ÒriÓ): request one or more units of a resource Ð Allocation (ÒaiÓ): All outstanding requests from a process for a given resource are satisfied Ð Deallocation (ÒdiÓ): The process releases units

  • f a resource

Sj Sk xi

slide-7
SLIDE 7

Properties of States

¥ Want to define deadlock in terms of patterns

  • f transitions

¥ Define: pi is blocked in Sj if pi cannot cause a transition out of Sj

slide-8
SLIDE 8

Properties of States

¥ Want to define deadlock in terms of patterns

  • f transitions

¥ Define: pi is blocked in Sj if pi cannot cause a transition out of Sj

Sj r3 a1 r1 p2 is blocked in Sj

slide-9
SLIDE 9

Properties of States (cont)

¥ If pi is blocked in Sj, and will also be blocked in every Sk reachable from Sj, then pi is deadlocked ¥ Sj is called a deadlock state

slide-10
SLIDE 10

Example

r a d S0 S1 S2 S3 S4 r a d

¥ One process, two units of one resource ¥ Can request one unit at a time

slide-11
SLIDE 11

Extension of Example

r0 a0 d0 S00 S10 S20 S30 S40 r0 a0 d0 r0 a0 d0 S01 S11 S21 S31 S41 r0 a0 d0 r0 a0 d0 S02 S12 S22 S32 r0 r0 a0 d0 S03 S13 S23 S33 r0 S04 S14 r0 r1 r1 r1 r1 r1 r1 r1 r1 a1 a1 a1 a1 a1 a1 d1 d1 d1 d1 d1 d1 r1

slide-12
SLIDE 12

Prevention

¥ Necessary conditions for deadlock

Ð Mutual exclusion Ð Hold and wait Ð Circular waiting Ð No preemption

¥ Ensure that at least one of the necessary conditions is false at all times

Ð Mutual exclusion must hold at all times

slide-13
SLIDE 13

Hold and Wait

¥ Need to be sure a process does not hold one resource while requesting another ¥ Approach 1: Force a process to request all resources it needs at one time ¥ Approach 2: If a process needs to acquire a new resource, it must first release all resources it holds, then reacquire all it needs ¥ What does this say about state transition diagrams?

slide-14
SLIDE 14

Circular Wait

¥ Have a situation in which there are K processes holding units of K resources

R P R P P holds R P requests R Ri Pi

slide-15
SLIDE 15

Circular Wait (cont)

¥ There is a cycle in the graph of processes and resources ¥ Choose a resource request strategy by which no cycle will be introduced ¥ Total order on all resources, then can only ask for Rj if Ri < Rj for all Ri the process is currently holding

slide-16
SLIDE 16

Circular Wait (cont)

¥ There is a cycle in the graph of processes and resources ¥ Choose a resource request strategy by which no cycle will be introduced ¥ Total order on all resources, then can only ask for Rj if Ri < Rj for all Ri the process is currently holding ¥ Here is how we saw the easy solution for the dining philosophers

slide-17
SLIDE 17

Allowing Preemption

¥ Allow a process to time-out on a blocked request -- withdrawing the request if it fails

Si ru dv ru Sj Sk wu

slide-18
SLIDE 18

Avoidance

¥ Construct a model of system states, then choose a strategy that will guarantees that the system will not go to a deadlock state ¥ Requires extra information -- the maximum claim for each process ¥ Allows resource manager to see the worst case that could happen, then to allow transitions based on that knowledge

slide-19
SLIDE 19

Safe vs Unsafe States

¥ Safe state: one in which there is guaranteed to be a sequence of transitions that leads back to the initial state

Ð Even if all exercise their maximum claim, there is an allocation strategy by which all claims can be met

¥ Unsafe state: one in which the system cannot guarantee there is such a sequence

Ð Unsafe state can lead to a deadlock state if too many processes exercise their maximum claim at once

slide-20
SLIDE 20

More on Safe & Unsafe States

Normal Execution Request Max Claim Execute, then release No Yes

slide-21
SLIDE 21

More on Safe & Unsafe States

Normal Execution Request Max Claim Execute, then release No Yes

Likely to be in a safe state Probability of being in unsafe state increases

slide-22
SLIDE 22

More on Safe & Unsafe States

Normal Execution Request Max Claim Execute, then release No Yes

¥Suppose all processes take ÒyesÓ branch ¥Avoidance strategy is to allow this to happen, yet still be safe

slide-23
SLIDE 23

More on Safe & Unsafe States

Safe States Unsafe States Deadlock States

slide-24
SLIDE 24

BankerÕs Algorithm

¥ Let maxc[i, j] be the maximum claim for Rj by pi ¥ Let alloc[i, j] be the number of units of Rj held by pi ¥ Can always compute

Ð avail[j] = cj - Σ0≤i< nalloc[i,j] Ð Then number of available units of Rj

¥ Should be able to determine if the state is safe or not using this info

slide-25
SLIDE 25

BankerÕs Algorithm

¥ Copy the alloc[i,j] table to allocÕ[i,j] ¥ Given C, maxc and allocÕ, compute avail vector ¥ Find pi: maxc[i,j] - allocÕ[i,j] ≤ avail[j] for 0 ≤ j < m and 0 ≤ i < n.

Ð If no such pi exists, the state is unsafe Ð If allocÕ[i,j] is 0 for all i and j, the state is safe

¥ Set allocÕ[i,j] to 0; deallocate all resources held by pi; go to Step 2

slide-26
SLIDE 26

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 4 3 p3 2 1 p4 1 3

Allocated Resources C = <8, 5, 9, 7>

slide-27
SLIDE 27

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 4 3 p3 2 1 p4 1 3 Sum 7 3 7 5

Allocated Resources C = <8, 5, 9, 7> ¥Compute total allocated

slide-28
SLIDE 28

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 4 3 p3 2 1 p4 1 3 Sum 7 3 7 5

Allocated Resources C = <8, 5, 9, 7> ¥Compute total allocated ¥Determine available units avail = <8-7, 5-3, 9-7, 7-5> = <1, 2, 2, 2>

slide-29
SLIDE 29

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 4 3 p3 2 1 p4 1 3 Sum 7 3 7 5

Allocated Resources C = <8, 5, 9, 7> ¥Compute total allocated ¥Determine available units avail = <8-7, 5-3, 9-7, 7-5> = <1, 2, 2, 2> ¥Can anyoneÕs maxc be met?

maxc[2,0]-allocÕ[2,0] = 5-4 = 1≤1 = avail[0] maxc[2,1]-allocÕ[2,1] = 1-0 = 1≤2 = avail[1] maxc[2,2]-allocÕ[2,2] = 0-0 = 0≤2 = avail[2] maxc[2,3]-allocÕ[2,3] = 5-3 = 2≤2 = avail[3]

slide-30
SLIDE 30

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 4 3 p3 2 1 p4 1 3 Sum 7 3 7 5

Allocated Resources C = <8, 5, 9, 7> ¥Compute total allocated ¥Determine available units avail = <8-7, 5-3, 9-7, 7-5> = <1, 2, 2, 2> ¥Can anyoneÕs maxc be met?

maxc[2,0]-allocÕ[2,0] = 5-4 = 1≤1 = avail[0] maxc[2,1]-allocÕ[2,1] = 1-0 = 1≤2 = avail[1] maxc[2,2]-allocÕ[2,2] = 0-0 = 0≤2 = avail[2] maxc[2,3]-allocÕ[2,3] = 5-3 = 2≤2 = avail[3]

¥P2 can exercise max claim

avail[0] = avail[0]+allocÕ[2,0] = 1+4 = 5 avail[1] = avail[1]+allocÕ[2,1] = 2+0 = 2 avail[2] = avail[2]+allocÕ[2,2] = 2+0 = 2 avail[3] = avail[3]+allocÕ[2,3] = 2+3 = 5

slide-31
SLIDE 31

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 p3 2 1 p4 1 3 Sum 3 3 7 2

Allocated Resources C = <8, 5, 9, 7> ¥Compute total allocated ¥Determine available units avail = <8-3, 5-3, 9-7, 7-2> = <5, 2, 2, 5> ¥Can anyoneÕs maxc be met?

maxc[4,0]-allocÕ[4,0] = 5-1 = 4≤5 = avail[0] maxc[4,1]-allocÕ[4,1] = 0-0 = 0≤2 = avail[1] maxc[4,2]-allocÕ[4,2] = 3-3 = 0≤2 = avail[2] maxc[4,3]-allocÕ[4,3] = 3-0 = 3≤5 = avail[3]

slide-32
SLIDE 32

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 p3 2 1 p4 1 3 Sum 3 3 7 2

Allocated Resources C = <8, 5, 9, 7> ¥Compute total allocated ¥Determine available units avail = <8-7, 5-3, 9-7, 7-5> = <5, 2, 2, 5> ¥Can anyoneÕs maxc be met?

maxc[4,0]-allocÕ[4,0] = 5-1 = 4≤5 = avail[0] maxc[4,1]-allocÕ[4,1] = 0-0 = 0≤2 = avail[1] maxc[4,2]-allocÕ[4,2] = 3-3 = 0≤2 = avail[2] maxc[4,3]-allocÕ[4,3] = 3-0 = 3≤5 = avail[3]

¥P4 can exercise max claim

avail[0] = avail[0]+allocÕ[4,0] = 5+1 = 6 avail[1] = avail[1]+allocÕ[4,1] = 2+0 = 2 avail[2] = avail[2]+allocÕ[4,2] = 2+3 = 5 avail[3] = avail[3]+allocÕ[4,3] = 5+0 = 5

slide-33
SLIDE 33

Example

Process R0 R1 R2 R3 p0 3 2 1 4 p1 2 5 2 p2 5 1 5 p3 1 5 3 p4 3 3 3

Maximum Claim

Process R0 R1 R2 R3 p0 2 1 1 p1 1 2 1 p2 p3 2 1 p4 Sum 2 1 4 2

Allocated Resources C = <8, 5, 9, 7> ¥Compute total allocated ¥Determine available units avail = <8-7, 5-3, 9-7, 7-5> = <6, 2, 5, 5> ¥Can anyoneÕs maxc be met? (Yes, any of them can)

slide-34
SLIDE 34

Detection & Recovery

¥ Check for deadlock (periodically or sporadically), then recover ¥ Can be far more aggressive with allocation ¥ No maximum claim, no safe/unsafe states ¥ Differentiate between

Ð Serially reusable resources: A unit must be allocated before being released Ð Consumable resources: Never release acquired resources; resource count is number currently available

slide-35
SLIDE 35

Reusable Resource Graphs (RRGs)

¥ Micro model to describe a single state ¥ Nodes = {p0, p1, É, pn} ∪ {R1, R2, É, Rm} ¥ Edges connect pi to Rj, or Rj to pi

Ð (pi, Rj) is a request edge for one unit of Rj Ð (Rj, pi) is an assignment edge of one unit of Rj

¥ For each Rj there is a count, cj of units Rj ¥ Number of units of Rj allocated to pi plus the number requested by pi cannot exceed cj

slide-36
SLIDE 36

Example

P holds one unit of R A Deadlock State P requests one unit of R R R p p

slide-37
SLIDE 37

Example

Not a Deadlock State No Cycle in the Graph

slide-38
SLIDE 38

State Transitions due to Request

¥ In Sj, pi is allowed to request q≤ch units of Rh, provided pi has no outstanding requests. ¥ Sj → Sk, where the RRG for Sk is derived from Sj by adding q request edges from pi to Rh

Rh pi Rh pi State Sj State Sk pi request q units

  • f Rh

q edges

slide-39
SLIDE 39

State Transition for Acquire

¥ In Sj, pi is allowed to acquire units of Rh, iff there is (pi, Rh) in the graph, and all can be satisfied. ¥ Sj → Sk, where the RRG for Sk is derived from Sj by changing each request edge to an assignment edge.

Rh pi Rh pi State Sj State Sk pi acquires units

  • f Rh
slide-40
SLIDE 40

State Transition for Release

¥ In Sj, pi is allowed to release units of Rh, iff there is (Rh, pi) in the graph, and there is no request edge from pi. ¥ Sj → Sk, where the RRG for Sk is derived from Sj by deleting all assignment edges.

Rh pi Rh pi State Sj State Sk pi releases units

  • f Rh
slide-41
SLIDE 41

Example

p0 p1

S00

slide-42
SLIDE 42

Example

p0 p1 p0 p1

S00 S01

slide-43
SLIDE 43

Example

p0 p1 p0 p0 p1 p1

S00 S01 S11

slide-44
SLIDE 44

Example

p0 p1 p0 p0 p0 p1 p1 p1

S00 S01 S11 S21

slide-45
SLIDE 45

Example

p0 p1 p0 p0 p0 p0 p1 p1 p1 p1

S00 S01 S11 S21 S22

slide-46
SLIDE 46

Example

p0 p1 p0 p0 p0 p0 p0 p1 p1 p1 p1 p1

S00 S01 S11 S21 S22 S33 . . .

slide-47
SLIDE 47

Graph Reduction

¥ Deadlock state if there is no sequence of transitions unblocking every process ¥ A RRG represents a state; can analyze the RRG to determine if there is a sequence ¥ A graph reduction represents the (optimal) action of an unblocked process. Can reduce by pi if

Ð pi is not blocked Ð pi has no request edges, and there are (Rj, pi) in the RRG

slide-48
SLIDE 48

Graph Reduction (cont)

¥ Transforms RRG to another RRG with all assignment edges into pi removed ¥ Represents pi releasing the resources it holds

pi pi Reducing by pi

slide-49
SLIDE 49

Graph Reduction (cont)

¥ A RRG is completely reducible if there a sequence of reductions that leads to a RRG with no edges ¥ A state is a deadlock state if and only if the RRG is not completely reducible.

slide-50
SLIDE 50

Example RRG

p0 p1 p2

slide-51
SLIDE 51

Example RRG

p0 p1 p2 p0 p1 p2

slide-52
SLIDE 52

Example RRG

p0 p1 p2 p0 p1 p2 p0 p1 p2

slide-53
SLIDE 53

Example RRG

p0 p1 p2 p0 p1 p2 p0 p1 p2 p0 p1 p2

slide-54
SLIDE 54

Example RRG

p0 p1 p2

slide-55
SLIDE 55

Consumable Resource Graphs (CRGs)

¥ Number of units varies, have producers/consumers ¥ Nodes = {p0, p1, É, pn} ∪ {R1, R2, É, Rm} ¥ Edges connect pi to Rj, or Rj to pi

Ð (pi, Rj) is a request edge for one unit of Rj Ð (Rj, pi) is an producer edge (must have at least

  • ne producer for each Rj)

¥ For each Rj there is a count, wj of units Rj

slide-56
SLIDE 56

State Transitions due to Request

¥ In Sj, pi is allowed to request any number of units of Rh, provided pi has no outstanding requests. ¥ Sj → Sk, where the RRG for Sk is derived from Sj by adding q request edges from pi to Rh

Rh pi Rh pi State Sj State Sk pi request q units

  • f Rh

q edges

slide-57
SLIDE 57

State Transition for Acquire

¥ In Sj, pi is allowed to acquire units of Rh, iff there is (pi, Rh) in the graph, and all can be satisfied. ¥ Sj → Sk, where the RRG for Sk is derived from Sj by deleting each request edge and decrementing wh.

Rh pi Rh pi State Sj State Sk pi acquires units

  • f Rh
slide-58
SLIDE 58

State Transition for Release

¥ In Sj, pi is allowed to release units of Rh, iff there is (Rh, pi) in the graph, and there is no request edge from pi. ¥ Sj → Sk, where the RRG for Sk is derived from Sj by incrementing wh.

Rh pi Rh pi State Sj State Sk pi releases 2 units

  • f Rh
slide-59
SLIDE 59

Example

p0 p1

slide-60
SLIDE 60

Example

p0 p1 p0 p1

slide-61
SLIDE 61

Example

p0 p1 p0 p0 p1 p1

slide-62
SLIDE 62

Example

p0 p1 p0 p0 p0 p1 p1 p1

slide-63
SLIDE 63

Example

p0 p1 p0 p0 p0 p0 p1 p1 p1 p1

slide-64
SLIDE 64

Deadlock Detection

¥ May have a CRG that is not completely reducible, but it is not a deadlock state ¥ For each process:

Ð Find at least one sequence which leaves each process unblocked.

¥ There may be different sequences for different processes -- not necessarily an efficient approach

slide-65
SLIDE 65

Deadlock Detection

¥ May have a CRG that is not completely reducible, but it is not a deadlock state ¥ Only need to find sequences, which leave each process unblocked.

p0 p1

slide-66
SLIDE 66

Deadlock Detection

¥ May have a CRG that is not completely reducible, but it is not a deadlock state ¥ Only need to find a set of sequences, which leaves each process unblocked.

slide-67
SLIDE 67

General Resource Graphs

¥ Have consumable and reusable resources ¥ Apply consumable reductions to consumables, and reusable reductions to reusables ¥ See Figure 10.29

slide-68
SLIDE 68

GRG Example (Fig 10.29)

Reusable Consumable p0 R2 R1 R0 p3 p2 p1 Not in Fig 10.29

slide-69
SLIDE 69

GRG Example (Fig 10.29)

Reusable Consumable p0 R2 R1 R0 p3 p2 p1

Reduce by p3

slide-70
SLIDE 70

GRG Example (Fig 10.29)

Reusable Consumable p0 R2 R1 R0 p3 p2 p1

Reduce by p0

slide-71
SLIDE 71

Recovery

¥ No magic here

Ð Choose a blocked resource Ð Preempt it (releasing its resources) Ð Run the detection algorithm Ð Iterate if until the state is not a deadlock state