Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective,...

60
Operating Systems: A Modern Perspective, Chapter 10 Slide 10-1 Copyright © 2004 Pearson Education, Inc.

Transcript of Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective,...

Page 1: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-1

Copyright © 2004 Pearson Education, Inc.

Page 2: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-2

Copyright © 2004 Pearson Education, Inc.

10Deadlock

Page 3: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-3

Copyright © 2004 Pearson Education, Inc.

Example

Process 1Process 1 Process 2Process 2

Resource 1Resource 1 Resource 2Resource 2

Process holds the resourceProcess requests the resource

Page 4: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-4

Copyright © 2004 Pearson Education, Inc.

Three Deadlocked Processes

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

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

Process holds the resourceProcess requests the resource

Page 5: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-5

Copyright © 2004 Pearson Education, Inc.

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

Page 6: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-6

Copyright © 2004 Pearson Education, Inc.

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 of a resource

Sj Skxi

Page 7: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-7

Copyright © 2004 Pearson Education, Inc.

Properties of States

• Want to define deadlock in terms of patterns of transitions

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

Sjr3

a1

r1

p2 is blocked in Sj

Page 8: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-8

Copyright © 2004 Pearson Education, Inc.

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

Page 9: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-9

Copyright © 2004 Pearson Education, Inc.

A Simple Process-Resource Model Instance

P1P1 P3

P3P2P2

R1R1 R2

R2 R3R3

Process holds resource

Process requests resource

Page 10: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-10

Copyright © 2004 Pearson Education, Inc.

Example

rad

S0 S1 S2 S3 S4r a

d

• One process, two units of one resource

• Can request one unit at a time

Page 11: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-11

Copyright © 2004 Pearson Education, Inc.

Extension of Exampler0a0

d0

S00 S10 S20 S30 S40r0 a0

d0

r0a0

d0

S01 S11 S21 S31 S41r0 a0

d0

r0a0

d0

S02 S12 S22 S32r0

r0a0

d0

S03 S13 S23 S33r0

S04 S14r0

r1 r1 r1r1

r1r1r1r1

a1 a1 a1 a1

a1a1

d1 d1

d1d1 d1 d1

r1

Page 12: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-12

Copyright © 2004 Pearson Education, Inc.

Figure 10‑10: A Model of a State with a Circular Wait

R P

RP

P holds R

P requests R

Ri

Pi

Page 13: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-13

Copyright © 2004 Pearson Education, Inc.

Dining Philosophers Revisited philosopher(int i){ while (TRUE) { // Think // Eat P(fork[i]); /* Pick up left fork */ P(fork[(i+1) mod 5]); /* Pick up right fork */ eat(); V(fork[(i+1) mod 5]); V(fork[i]); }}philosopher4(){ while (TRUE) { // Think // Eat P(fork[0]); /* Pick up right fork */ P(fork[4]); /* Pick up left fork */ eat(); V(fork[4]); V(fork[0]); }}

semaphore fork[5];fork[0] = fork[1] = fork[2] = fork[3] = fork[4] = 1;fork(philosopher, 1, 0);fork(philosopher, 1, 1);fork(philosopher, 1, 2);fork(philosopher, 1, 3);fork(philosopher4, 0);

Page 14: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-14

Copyright © 2004 Pearson Education, Inc.

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

Page 15: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-15

Copyright © 2004 Pearson Education, Inc.

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

Page 16: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-16

Copyright © 2004 Pearson Education, Inc.

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?

Page 17: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-17

Copyright © 2004 Pearson Education, Inc.

Circular Wait

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

R P

RP

P holds R

P requests R

Ri

Pi

Page 18: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-18

Copyright © 2004 Pearson Education, Inc.

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

Page 19: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-19

Copyright © 2004 Pearson Education, Inc.

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

• This is how we noticed the easy solution for the dining philosophers

Page 20: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-20

Copyright © 2004 Pearson Education, Inc.

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

Page 21: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-21

Copyright © 2004 Pearson Education, Inc.

Avoidance

• Define a model of system states, then choose a strategy that will guarantee that the system will not go to a deadlock state

• Requires extra information, e.g., 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

Page 22: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-22

Copyright © 2004 Pearson Education, Inc.

Safe vs Unsafe States• Safe state: one in which the system can assure

that any sequence of subsequent transitions 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 that the system will transition back to the initial state– Unsafe state can lead to a deadlock state if too many

processes exercise their maximum claim at once

Page 23: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-23

Copyright © 2004 Pearson Education, Inc.

More on Safe & Unsafe States

NormalExecution

RequestMax Claim

Execute, thenrelease

No

Yes

Likely to be in a safe state

Probability of being in unsafe state increases

Page 24: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-24

Copyright © 2004 Pearson Education, Inc.

More on Safe & Unsafe States

I

Safe States

Unsafe States

Deadlock States

Disallow

Page 25: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-25

Copyright © 2004 Pearson Education, Inc.

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 - 0i 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

Page 26: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-26

Copyright © 2004 Pearson Education, Inc.

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

Page 27: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-27

Copyright © 2004 Pearson Education, Inc.

ExampleProcess R0 R1 R2 R3

p0 3 2 1 4p1 0 2 5 2p2 5 1 0 5p3 1 5 3 0p4 3 0 3 3

Maximum Claim

Process R0 R1 R2 R3

p0 2 0 1 1p1 0 1 2 1p2 4 0 0 3p3 0 2 1 0p4 1 0 3 0Sum 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 = 11 = avail[0]maxc[2,1]-alloc’[2,1] = 1-0 = 12 = avail[1]maxc[2,2]-alloc’[2,2] = 0-0 = 02 = avail[2]maxc[2,3]-alloc’[2,3] = 5-3 = 22 = avail[3]

•P2 can exercise max claimavail[0] = avail[0]+alloc’[2,0] = 1+4 = 5avail[1] = avail[1]+alloc’[2,1] = 2+0 = 2avail[2] = avail[2]+alloc’[2,2] = 2+0 = 2avail[3] = avail[3]+alloc’[2,3] = 2+3 = 5

Page 28: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-28

Copyright © 2004 Pearson Education, Inc.

ExampleProcess R0 R1 R2 R3

p0 3 2 1 4p1 0 2 5 2p2 5 1 0 5p3 1 5 3 0p4 3 0 3 3

Maximum Claim

Process R0 R1 R2 R3

p0 2 0 1 1p1 0 1 2 1p2 0 0 0 0p3 0 2 1 0p4 1 0 3 0Sum 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 = 45 = avail[0]maxc[4,1]-alloc’[4,1] = 0-0 = 02 = avail[1]maxc[4,2]-alloc’[4,2] = 3-3 = 02 = avail[2]maxc[4,3]-alloc’[4,3] = 3-0 = 35 = avail[3]

•P4 can exercise max claimavail[0] = avail[0]+alloc’[4,0] = 5+1 = 6avail[1] = avail[1]+alloc’[4,1] = 2+0 = 2avail[2] = avail[2]+alloc’[4,2] = 2+3 = 5avail[3] = avail[3]+alloc’[4,3] = 5+0 = 5

Page 29: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-29

Copyright © 2004 Pearson Education, Inc.

ExampleProcess R0 R1 R2 R3

p0 3 2 1 4p1 0 2 5 2p2 5 1 0 5p3 1 5 3 0p4 3 0 3 3

Maximum Claim

Process R0 R1 R2 R3

p0 2 0 1 1p1 0 1 2 1p2 0 0 0 0p3 0 2 1 0p4 0 0 0 0Sum 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)

Page 30: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-30

Copyright © 2004 Pearson Education, Inc.

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

Page 31: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-31

Copyright © 2004 Pearson Education, Inc.

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

Page 32: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-32

Copyright © 2004 Pearson Education, Inc.

State Transitions due to Request

• In Sj, pi is allowed to request qch 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

Rhpi Rhpi

State SjState Sk

pi request q unitsof Rh

q edges

Page 33: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-33

Copyright © 2004 Pearson Education, Inc.

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.

Rhpi Rhpi

State SjState Sk

pi acquires unitsof Rh

Page 34: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-34

Copyright © 2004 Pearson Education, Inc.

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.

Rhpi Rhpi

State SjState Sk

pi releases unitsof Rh

Page 35: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-35

Copyright © 2004 Pearson Education, Inc.

Example

P holds one unit of R

A Deadlock State

P requests one unit of RR

R

p

p

Page 36: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-36

Copyright © 2004 Pearson Education, Inc.

Example

Not a Deadlock State No Cycle in the Graph

Page 37: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-37

Copyright © 2004 Pearson Education, Inc.

Example

p0

p1

S00

Page 38: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-38

Copyright © 2004 Pearson Education, Inc.

Example

p0

p1

p0

p1

S00 S01

Page 39: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-39

Copyright © 2004 Pearson Education, Inc.

Example

p0

p1

p0 p0

p1 p1

S00 S01 S11

Page 40: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-40

Copyright © 2004 Pearson Education, Inc.

Example

p0

p1

p0 p0 p0

p1 p1 p1

S00 S01 S11 S21

Page 41: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-41

Copyright © 2004 Pearson Education, Inc.

Example

p0

p1

p0 p0 p0 p0

p1 p1 p1 p1

S00 S01 S11 S21 S22

Page 42: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-42

Copyright © 2004 Pearson Education, Inc.

Example

p0

p1

p0 p0 p0 p0 p0

p1 p1 p1 p1 p1

S00 S01 S11 S21 S22 S33

. . .

Page 43: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-43

Copyright © 2004 Pearson Education, Inc.

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

Page 44: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-44

Copyright © 2004 Pearson Education, Inc.

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

Page 45: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-45

Copyright © 2004 Pearson Education, Inc.

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.

Page 46: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-46

Copyright © 2004 Pearson Education, Inc.

Example RRGp0

p1 p2

p0

p1 p2

p0

p1 p2

p0

p1 p2

Page 47: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-47

Copyright © 2004 Pearson Education, Inc.

Example RRGp0

p1 p2

Page 48: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-48

Copyright © 2004 Pearson Education, Inc.

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 one producer for each Rj)

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

Page 49: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-49

Copyright © 2004 Pearson Education, Inc.

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

Rhpi Rhpi

State SjState Sk

pi request q unitsof Rh

q edges

Page 50: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-50

Copyright © 2004 Pearson Education, Inc.

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.

Rhpi Rhpi

State SjState Sk

pi acquires unitsof Rh

Page 51: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-51

Copyright © 2004 Pearson Education, Inc.

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.

Rhpi Rhpi

State SjState Sk

pi releases 2 unitsof Rh

Page 52: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-52

Copyright © 2004 Pearson Education, Inc.

Example

p0

p1

p0

p1

p0

p1

p0

p1

p0

p1

Page 53: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-53

Copyright © 2004 Pearson Education, Inc.

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

Page 54: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-54

Copyright © 2004 Pearson Education, Inc.

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

Page 55: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-55

Copyright © 2004 Pearson Education, Inc.

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.

Page 56: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-56

Copyright © 2004 Pearson Education, Inc.

General Resource Graphs

• Have consumable and reusable resources

• Apply consumable reductions to consumables, and reusable reductions to reusables

• See Figure 10.29

Page 57: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-57

Copyright © 2004 Pearson Education, Inc.

GRG Example (Fig 10.29)

Reusable

Consumable

p0

R2

R1

R0

p3 p2

p1

Not in Fig 10.29

Page 58: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-58

Copyright © 2004 Pearson Education, Inc.

GRG Example (Fig 10.29)

Reusable

Consumable

p0

R2

R1

R0

p3 p2

p1

Reduce by p3

Page 59: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-59

Copyright © 2004 Pearson Education, Inc.

GRG Example (Fig 10.29)

Reusable

Consumable

p0

R2

R1

R0

p3 p2

p1

Reduce by p0

Page 60: Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.

Operating Systems: A Modern Perspective, Chapter 10

Slide 10-60

Copyright © 2004 Pearson Education, Inc.

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