deadlock - new mexico institute of mining and …cs325/spring 11/lectures/lec12...3/22/11 cse325 -...

28
CSE325 Principles of Operating Systems Deadlock David P. Duggan [email protected] March 22, 2011

Upload: others

Post on 03-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

CSE325 Principles of Operating Systems

Deadlock

David P. Duggan [email protected]

March 22, 2011

Page 2: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 2

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

Page 3: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 3

Example

Process 1 Process 2

Resource 1 Resource 2

Process holds the resource Process requests the resource

Page 4: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 4

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 neither ever does.

Page 5: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 5

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 6: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 6

Does Deadlock Exist?

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

Page 7: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

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

3/22/11 CSE325 - Deadlock 7

Page 8: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 8

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 9: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 9

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 10: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

10

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 11: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 11

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 12: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 12

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 13: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 13

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 14: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 14

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 15: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 15

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 16: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 16

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 17: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 17

More on Safe & Unsafe States

I

Safe States

Unsafe States

Deadlock States

Disallow

Page 18: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

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

3/22/11 CSE325 - Deadlock 18

Page 19: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

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

3/22/11 CSE325 - Deadlock 19

Page 20: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 20

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 21: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

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 22: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

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 23: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

23

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, any of them can)

Page 24: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 24

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 25: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

25

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 exists, 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 26: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 26

Deadlock Detection Example

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

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

R1 R2 R3 R4 R5

P1 P2 P3 P4

Request Allocated Available

R1 R2 R3 R4 R5 R1 R2 R3 R4 R5

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 27: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

3/22/11 CSE325 - Deadlock 27

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?

Page 28: Deadlock - New Mexico Institute of Mining and …cs325/spring 11/Lectures/Lec12...3/22/11 CSE325 - Deadlock 9 Prevention • Necessary conditions for deadlock – Mutual exclusion

Assignment •  Read chapter 8

3/22/11 CSE325 - Deadlock 28