51
1 Chapter 6 (Part 2) Deadlock

Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

  • Upload
    others

  • View
    26

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

1

Chapter 6���

(Part 2)

Deadlock

Page 2: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

2

Deadlock Avoidance

Detection – “optimistic” approach Allocate resources “Break” system to fix it

Page 3: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

3

Deadlock Avoidance

Detection – “optimistic” approach Allocate resources “Break” system to fix it

Avoidance – “pessimistic” approach Don’t allocate resource if it may lead to deadlock If a process requests a resource... Make it wait until you are sure it’s OK.

Page 4: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

4

Deadlock Avoidance

Detection – “optimistic” approach Allocate resources “Break” system to fix it

Avoidance – “pessimistic” approach Don’t allocate resource if it may lead to deadlock If a process requests a resource... Make it wait until you are sure it’s OK.

Which one to use depends upon the application!

Page 5: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

5

Process-Resource Trajectories

time

Process A t1 t2 t3 t4

Page 6: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

6

Process-Resource Trajectories

time

Process A t1 t2 t3 t4

Requests Printer

Requests CD-RW

Releases Printer

Releases CD-RW

Page 7: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

7

Process-Resource Trajectories tim

e Pr

oces

s B

tW

tX

tY

tZ

Page 8: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

8

Process-Resource Trajectories tim

e Pr

oces

s B

tW

tX

tY

tZ Requests CD-RW

Requests Printer Releases CD-RW

Releases Printer

Page 9: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

9

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Page 10: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

10

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Both processes hold CD-RW

Page 11: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

11

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Both processes hold Printer

Page 12: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

12

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Forbidden Zone

Page 13: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

13

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Trajectory showing system progress

Page 14: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

14

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

B makes progress, A is not running

Page 15: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

15

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

B requests the CD-RW

Page 16: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

16

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Request is granted

Page 17: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

17

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

A runs & makes a request for printer

Page 18: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

18

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Request is granted; A proceeds

Page 19: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

19

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

B runs & requests the printer...

MUST WAIT!

Page 20: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

20

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

A runs & requests the CD-RW

Page 21: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

21

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

A... holds printer requests CD-RW B... holds CD-RW requests printer

Page 22: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

22

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

A... holds printer requests CD-RW B... holds CD-RW requests printer

DEADLOCK!

Page 23: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

23

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

A danger occurred here.

Should the OS give A the printer, or make it wait???

Page 24: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

24

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

This area is “unsafe”

Page 25: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

25

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time

Within the “unsafe” area, deadlock is inevitable.

We don’t want to enter this area.

The OS should make A wait at this point!

Page 26: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

26

Process-Resource Trajectories Pr

oces

s B

tW

tX

tY

tZ

Process A t1 t2 t3 t4

time

time B requests the printer, B releases CD-RW, B releases printer, then A runs to completion!

Page 27: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

27

Safe states

The current state: “which processes hold which resources”

A “safe” state: • No deadlock, and • There is some scheduling order in which every process can run to completion even if all of them suddenly request their maximum number of units immediately.

The Banker’s Algorithm: Goal: Avoid unsafe states!!! When a process requests more units, should the system grant the request or make it wait?

Page 28: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

28

The Banker’s Algorithm Assumptions: • Only one type of resource, with multiple units. • Processes declare their maximum potential resource needs ahead of time.

When a process requests more units should the system make it wait to ensure safety?

6 2 5

Example: One resource type with 10 units

3!

How many more this process might need

Page 29: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

29

Unsafe states

6 2 5

10 total resource units

3!

5 2 5

2!

Unsafe!

5 0 5

5 0 5

Page 30: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

30

Avoidance Modeling - Multiple Resource Types

Note: These are the max. possible requests, which we assume are known ahead of time

Page 31: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

31

Banker’s Algorithm for Multiple Resources

1)   Look for a row, R, whose unmet resource needs are all smaller than or equal to A. If such row exists, all the possible needs for this process could be met right now.

2) Assume the process of the row chosen requests all the resources that it needs (which is guaranteed to be possible) and the terminates. Mark that process as “terminated” and add all its resources back to the “A” vector.

Repeat steps 1 and 2, until either all process are marked terminated, in which case the initial state was safe. If some processes remain, then initial state was UNSAFE!

Page 32: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

32

Avoidance algorithm

Max request matrix

Page 33: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

33

Avoidance algorithm

Max request matrix

Page 34: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

34

Avoidance algorithm

Max request matrix

Page 35: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

35

Avoidance algorithm

2 2 2 0

Max request matrix

Page 36: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

36

Avoidance algorithm

2 2 2 0

Max request matrix

Page 37: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

37

Avoidance algorithm

4 2 2 1 2 2 2 0

Max request matrix

Page 38: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

38

Deadlock Avoidance

Deadlock avoidance is usually impractical because you don’t know in advance what resources a

process will need!

Page 39: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

39

Deadlock Avoidance

Alternative approach: “deadlock prevention” Prevent the situation in which deadlock might occur for all time!

Attack one of the four conditions that are necessary for deadlock to be possible.

Four conditions necessary for deadlock:

• Mutual exclusion condition

• Hold and wait condition • No preemption condition

• Circular wait condition

Page 40: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

40

Attacking the conditions

Attacking mutual exclusion? • Not really an option for some resource types • May work for other types

Attacking no preemption? • Not really an option for some resource types • May work for other types

Page 41: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

41

Attacking the conditions

Attacking hold and wait? • Require processes to request all resources before they begin!

• Process must know ahead of time • Process must tell system its “max potential needs”

If a process decides it wants more than its initial declared needs, it must... • Release all resources • Give the system a new “max potential needs” • Resume execution

Issues: • Under-allocation of resources • Resource needs not known in advance

Page 42: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

42

Attacking the conditions

Attacking circular wait?

• Number each of the resources

• Require each process to acquire lower numbered resources before higher numbered resources.

More precisely: A process is not allowed to request a resource whose number is lower than the highest numbered resource it currently holds.

Example: 1. Printer 2. Scanner 3. CD-Rom 4. Plotter

Page 43: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

43

Recall this Example of Deadlock

Assume that resources are ordered: 1. Resource_1 2. Resource_2 3. ...etc...

acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1)

acquire (resource_2) acquire (resource_1) use resources 1 & 2 release (resource_1) release (resource_2)

Thread A: Thread B:

Page 44: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

44

Recall this Example of Deadlock

Assume that resources are ordered: 1. Resource_1 2. Resource_2 3. ...etc...

Thread B violates the ordering!

acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1)

acquire (resource_2) acquire (resource_1) use resources 1 & 2 release (resource_1) release (resource_2)

Thread A: Thread B:

Page 45: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

45

Why Does Resource Ordering Work?

Assume deadlock has occurred.

Process A holds X requests Y

Process B holds Y requests Z

Process C holds Z requests X

Page 46: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

46

Why Does Resource Ordering Work?

Assume deadlock has occurred.

Process A holds X requests Y

Process B holds Y requests Z

Process C holds Z requests X

X < Y

Page 47: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

47

Why Does Resource Ordering Work?

Assume deadlock has occurred.

Process A holds X requests Y

Process B holds Y requests Z

Process C holds Z requests X

X < Y

Y< Z

Page 48: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

48

Why Does Resource Ordering Work?

Assume deadlock has occurred.

Process A holds X requests Y

Process B holds Y requests Z

Process C holds Z requests X

X < Y

Y< Z

Z < X

Page 49: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

49

Why Does Resource Ordering Work?

Assume deadlock has occurred.

Process A holds X requests Y

Process B holds Y requests Z

Process C holds Z requests X

X < Y

Y< Z

Z < X

This is impossible!

Page 50: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

50

Why Does Resource Ordering Work?

Assume deadlock has occurred.

Process A holds X requests Y

Process B holds Y requests Z

Process C holds Z requests X

X < Y

Y< Z

Z < X

This is impossible!

Conclusion: The assumption must have been incorrect

Page 51: Deadlock - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Ch6-Deadlock-Part2.pdf · Deadlock Avoidance" Alternative approach: “deadlock prevention”" Prevent the situation

51

Resource Ordering

The chief problem:

It is hard to come up with an ordering of the resources that everyone finds acceptable!

Still, I believe this is particularly useful within an OS.

1. ProcessControlBlock 2. FileControlBlock 3. Page Frames

Also, the problem of resources with multiple units is not addressed.