deadlockcs325/spring2013/lec12-deadlock.pdf · addressing deadlock • prevention: design the...

28
CSE325 Principles of Operating Systems Deadlock David P. Duggan [email protected] February 21, 2013

Upload: others

Post on 12-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

CSE325 Principles of Operating Systems

Deadlock

David P. Duggan [email protected]

February 21, 2013

Page 2: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

Reading Assignment 8 •  Chapter 8 – Main Memory, due 2/26 •  Chapter 9 – Virtual Memory, due 2/28

2/21/13 CSE325 - Deadlock 2

Page 3: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 3

Outline •  What’s deadlock? •  Necessary conditions for deadlock •  Deadlock prevention •  Deadlock avoidance •  Deadlock detection and recovery

Page 4: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 4

Example

Process 1 Process 2

Resource 1 Resource 2

Process holds the resource Process requests the resource

Page 5: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 5

Three Deadlocked Processes

Process 1 Process 2 Process 3

Resource 1 Resource 2 Resource 3

Process holds the resource Process requests the resource

Deadlock : two or more processes are each waiting for each other to release a resource, and thus none ever do.

Page 6: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 6

R P

R P

P holds R

P requests R

Ri

Pi

Necessary Conditions for Deadlock?

Process 1 Process 2 Process 3

Resource 1 Resource 2 Resource 3

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

Resource-allocation graph

Page 7: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 7

Does Deadlock Exist?

•  Mutual exclusion? •  Hold and wait? •  Circular waiting? •  No preemption?

Page 8: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

Methods for Handling Deadlock •  Create protocol to prevent or avoid deadlocks

– Never enter a deadlocked state •  Allow system to enter deadlocked state

– Detect it and recover

•  Ignore the problem – Pretend it won’t happen

2/21/13 CSE325 - Deadlock 8

Page 9: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 9

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 10: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 10

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 11: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

11

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, then …?

it must first release all resources it holds, then reacquire all it needs

Process 1

Resource 1 Resource 2

Page 12: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 12

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

Page 13: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 13

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 resource types, then can

only ask for Rj if Ri < Rj for all Ri the process is currently holding

•  Requires proper use of resource requests

Page 14: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 14

Allow Preemption

•  Allow preemption to prevent a blocked request

Process 1 Process 2

Resource 1 Resource 2

•  Preempt P1’s resources •  Preempt P2’s resource

Page 15: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 15

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 16: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 16

Safe States •  Safe state: there exists a safe sequence

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

– Might have some processes wait for other processes to complete and release resources

Safe sequence for <P1, P2, …, Pn> for current allocation state if, for each Pi, the resource requests Pi can still make can be satisfied by currently available resources plus those held by all Pj, where j < i

Page 17: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 17

Unsafe States •  Unsafe state: there does not exist a safe

sequence – Unsafe state may lead to a deadlock state if too

many processes exercise their maximum claim at once

– Behavior of processes controls unsafe states

Page 18: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 18

More on Safe & Unsafe States

I

Safe States

Unsafe States

Deadlock States

Disallow

Page 19: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

Avoidance Algorithms •  Resource-allocation-graph algorithm

– Only valid for single instance resource types –  In your book

•  Banker’s algorithm – Less efficient than resource-allocation-graph – Works for multiple instance resources

2/21/13 CSE325 - Deadlock 19

Page 20: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

Banker’s Algorithm •  New process enters system

– Declares the maximum number of instances of each resource type it may need

– Can’t exceed total in system •  When resources are requested

– System determines if allocation leaves system in safe state

–  If yes, then allocate resources else wait until other processes release resources

2/21/13 CSE325 - Deadlock 20

Page 21: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 21

Banker’s Algorithm •  Let max[i, j] be the maximum claim for Rj

by Pi •  Let alloc[i, j] be the number of units of Rj

held by Pi

•  Let avail[j] be the number of units of Rj available, C[j] = total available to start

•  Let need[i, j] be the number of units of Rj that Pi may still need – Can be computed as: max[i, j] - alloc[i, j]

Page 22: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

Example

Process R0 R1 R2 R3 P0 3 2 1 4 P1 0 2 5 2 P2 5 1 0 5 P3 1 5 3 0 P4 3 0 3 3

Maximum Claim

Process R0 R1 R2 R3 P0 2 0 1 1 P1 0 1 2 1 P2 4 0 0 3 P3 0 2 1 0 P4 1 0 3 0 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 max be met? max[2,0]-alloc'[2,0] = 5-4 = 1≤1 = avail[0] max[2,1]-alloc'[2,1] = 1-0 = 1≤2 = avail[1] max[2,2]-alloc'[2,2] = 0-0 = 0≤2 = avail[2] max[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

Page 23: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

Example

Process R0 R1 R2 R3 P0 3 2 1 4 P1 0 2 5 2 P2 5 1 0 5 P3 1 5 3 0 P4 3 0 3 3

Maximum Claim

Process R0 R1 R2 R3 P0 2 0 1 1 P1 0 1 2 1 P2 0 0 0 0 P3 0 2 1 0 P4 1 0 3 0 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 max be met? max[4,0]-alloc'[4,0] = 5-1 = 4≤5 = avail[0] max[4,1]-alloc'[4,1] = 0-0 = 0≤2 = avail[1] max[4,2]-alloc'[4,2] = 3-3 = 0≤2 = avail[2] max[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

Page 24: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

24

Example

Process R0 R1 R2 R3 P0 3 2 1 4 P1 0 2 5 2 P2 5 1 0 5 P3 1 5 3 0 P4 3 0 3 3

Maximum Claim

Process R0 R1 R2 R3 P0 2 0 1 1 P1 0 1 2 1 P2 0 0 0 0 P3 0 2 1 0 P4 0 0 0 0 Sum 2 3 4 2

Allocated Resources

C = <8, 5, 9, 7>

• Compute total allocated • Determine available units

avail = <8-2, 5-3, 9-4, 7-2> = <6, 2, 5, 5>

• Can anyone’s max be met? (Yes, all except P3)

Page 25: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 25

Detection & Recovery

•  Allow system to enter deadlock state •  Check for deadlock (periodically or

sporadically), then recover •  Better resource allocation and usage •  No maximum claim, no safe/unsafe states •  Detection algorithm •  Recovery scheme

Page 26: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

26

A Deadlock Detection Algorithm •  Makes use of previous resource-allocation matrices and vectors •  Marks each process as not deadlocked. Initially all processes are

unmarked. Then perform: –  Mark each process i for which: alloc(i,j) = 0 for all resource type

j. (since these are not deadlocked) –  Initialize work vector: avail(j) = C(j) for all j –  REPEAT:

•  Find an unmarked process i: request(i,j) <= avail(j) for all j. •  If such i does not exist, then go to END. •  Else mark process i and set avail(j) = avail(j) + alloc(i,j) for

all j. •  Goto REPEAT

–  END: each unmarked process is deadlocked

Page 27: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 27

Deadlock Detection Example

•  Mark P3 since it has no allocated resources •  Set avail = (0,0,0,0,1) •  P2’s request <= avail. So mark P2 and set

avail = avail + (0,0,0,1,0) = (0,0,0,1,1) •  Algorithm terminates. P0 and P1 are deadlocked

R0 R1 R2 R3 R4

P0 P1 P2 P3

Request Allocated Available

R0 R1 R2 R3 R4 R0 R1 R2 R3 R4

0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1

1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0

0 0 0 0 1

Page 28: Deadlockcs325/spring2013/Lec12-Deadlock.pdf · Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states,

2/21/13 CSE325 - Deadlock 28

Recovery •  No magic here

– Choose a blocked resource – Preempt it (releasing its resources) – Or terminate processes – Run the detection algorithm –  Iterate until the state is not a deadlock state

•  What issues occur with these solutions?