49
Deadlock Tanenbaum Ch 6 Silberschatz Ch 7

Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

Embed Size (px)

Citation preview

Page 1: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

Deadlock

Tanenbaum Ch 6

Silberschatz Ch 7

Page 2: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 2

The Deadlock Problem

• A Computing example: record scanned document

• System has 1 scanner and 1 CD Recorder

• 2 processes each need both devices.

P0 acquires scanner and waits for CD

P1 acquires CD and waits for scanner

Page 3: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 3

Preemptable and Nonpreemptable Resources

Sequence of events required to use a resource:

1. Request the resource.

2. Use the resource.

3. Release the resource.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 4: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 4

Figure 6-1. Using a semaphore to protect resources. (a) One resource. (b) Two resources.

Resource Acquisition (1)

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 5: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 5

Figure 6-2. (a) Deadlock-free code.

Resource Acquisition (2)

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 6: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 6

Figure 6-2. (b) Code with a potential deadlock.

Resource Acquisition (3)

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 7: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 7

Introduction To Deadlocks

Deadlock can be defined formally as follows:

A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause.Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 8: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 8

Deadlock Characterization:Necessary Conditions

• Mutual Exclusion:– At least 1 resource must be held in a non-sharable

mode.

• Hold and wait:– There must be at least 1 process that is holding one

resource and is waiting for another

• No preemption:– Resources cannot be preempted

• Circular wait:– There must be a cycle of dependency among a set of

processes and resources.

Page 9: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 9

Deadlock StrategiesStrategies for dealing with deadlocks:1. Just ignore the problem.

2. Detection and recovery. Let deadlocks occur, detect them, take action.

3. Dynamic avoidance by careful resource allocation.

4. Prevention, by structurally negating one of the four required conditions.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 10: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 10

Ignore the Problem

• May not occur very often

• May not have serious consequences if it does happen

• May be difficult / expensive to detect.

Page 11: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 11

Resource Allocation Graphs

Process

Resource

Request a Resource

Hold a Resource

Deadlock

R

A

A

A

B

AR

R

R S

Page 12: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 12

Resource Allocation GraphMultiple Resources with No Cycles

P1 P2 P3

Page 13: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 13

Graph with Deadlock

P1 P2 P3

Page 14: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 14

Graph with Deadlock

P1 P2 P3

Page 15: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 15

Graph with Cycle but no Deadlock

P1

P4

P3

P2

Page 16: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 16

Figure 6-6. The four data structures needed by the deadlock detection algorithm.

Deadlock Detection with Multiple Resources of Each Type

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 17: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 17

Deadlock Detection with Multiple Resources of Each Type (2)

Deadlock detection algorithm:1. Look for an unmarked process, Pi , for which

the i-th row of R is less than or equal to A.

2. If such a process is found, add the i-th row of C to A, mark the process, and go back to step 1.

3. If no such process exists, the algorithm terminates.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 18: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 18

Figure 6-7. An example for the deadlock detection algorithm.

Deadlock Detection with Multiple Resources of Each Type (3)

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 19: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cottercs431-cotter 1919

Deadlock AvoidanceDeadlock Avoidance

• The previous approach can be used to avoid a deadlock (rather than just detect one when it occurs).– Declare the maximum number of resources of each

type that will be required before the process starts.– Before any resource allocation is made, ensure that

the allocation will not leave the system in a state where a deadlock can occur.

– This will ensure that a process can always get access to its needed maximums.

Page 20: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 20

Safe State

• A system is in a safe state if there exists a sequence of resource assignments such that each process can be allocated its maximum request, and then release the resources, and then allow remaining processes to do the same...

Page 21: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 21

Safe State• A system is in a safe state if there exists a sequence of

resource assignments such that each process can be allocated its maximum request, and then release the resources, and then allow remaining processes to do the same...

Process Max Needs Current Needs Total

P0 10 5 12

P1 4 2 Avail

P2 9 2 3

Page 22: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 22

Safe State• A system is in a safe state if there exists a sequence of

resource assignments such that each process can be allocated its maximum request, and then release the resources, and then allow remaining processes to do the same...

Process Max Needs Current Needs Total

P0 10 5 12

P1 4 24 Avail

P2 9 2 31

Page 23: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 23

Safe State• A system is in a safe state if there exists a sequence of

resource assignments such that each process can be allocated its maximum request, and then release the resources, and then allow remaining processes to do the same...

Process Max Needs Current Needs Total

P0 10 5 12

P1 0 40 Avail

P2 9 2 15

Page 24: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 24

Safe State

• A system is in a safe state if there exists a sequence of resource assignments such that each process can be allocated its maximum request, and then release the resources, and then allow remaining processes to do the same...

Process Max Needs Current Needs Total

P0 10 510 12

P1 0 0 Avail

P2 9 2 50

Page 25: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 25

Banker's Algorithm

• Use vectors and matrices to identify resource requirements– Available[j] = k; //resource j has k members available– Max[i,j] = k; //Process i may need k members of

resource j– Allocation [i,j] = k; //Process i is currently allocated k

members of resource j– Need[i,j] = k; // Process i may need as many as k

more of resource j.

Page 26: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 26

Banker's ExampleRequest: P0 wants 0 2 0

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Safe order: ???

Page 27: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 27

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Page 28: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 28

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 3 0 2 3 2 2 2 3 0

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Page 29: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 29

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 3 2 2 3 2 2 2 3 0

P2 3 0 2 9 0 2 2 1 0

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Safe Order: P1,

Page 30: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 30

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 0 0 0 3 2 2 2 3 0

P2 3 0 2 9 0 2 2 1 0

P3 2 1 1 2 2 2 5 3 2

P4 0 0 2 4 3 3

Safe Order: P1,

Page 31: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 31

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 0 0 0 3 2 2 2 3 0

P2 3 0 2 9 0 2 2 1 0

P3 2 2 2 2 2 2 5 3 2

P4 0 0 2 4 3 3 5 2 1

Safe Order: P1, P3,

Page 32: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 32

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 7 4 3

P1 0 0 0 3 2 2 2 3 0

P2 3 0 2 9 0 2 2 1 0

P3 0 0 0 2 2 2 5 3 2

P4 0 0 2 4 3 3 5 2 1

Safe Order: P1, P3,

Page 33: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 33

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 7 4 3

P1 0 0 0 3 2 2 2 3 0 3 1 2

P2 3 0 2 9 0 2 2 1 0

P3 0 0 0 2 2 2 5 3 2

P4 4 3 3 4 3 3 5 2 1

Safe Order: P1, P3, P4,

Page 34: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 34

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 7 4 3

P1 0 0 0 3 2 2 2 3 0 3 1 2

P2 3 0 2 9 0 2 2 1 0 7 4 5

P3 0 0 0 2 2 2 5 3 2

P4 0 0 0 4 3 3 5 2 1

Safe Order: P1, P3, P4,

Page 35: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 35

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 7 5 3 7 5 3 3 3 2 7 4 3

P1 0 0 0 3 2 2 2 3 0 3 1 2

P2 3 0 2 9 0 2 2 1 0 7 4 5

P3 0 0 0 2 2 2 5 3 2 0 0 2

P4 0 0 0 4 3 3 5 2 1

Safe Order: P1, P3, P4, P0,

Page 36: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 36

Banker's Example (change 1)

P1 requests (1 0 2)

Allocate Max Avail.

A B C A B C A B C

P0 0 0 0 7 5 3 3 3 2 7 4 3

P1 0 0 0 3 2 2 2 3 0 3 1 2

P2 3 0 2 9 0 2 2 1 0 7 4 5

P3 0 0 0 2 2 2 5 3 2 0 0 2

P4 0 0 0 4 3 3 5 2 1 7 5 5

Safe Order: P1, P3, P4, P0, P2 !!! OK

Page 37: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 37

Banker's Example (change 2)

P4 requests (3 3 0)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Page 38: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 38

Banker's Example (change 2)

P4 requests (3 3 0)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2 0 0 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 3 3 2 4 3 3

Page 39: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 39

Banker's Example (change 2)

P4 requests (3 3 0)

Allocate Max Avail.

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2 0 0 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

No job can be completed. No Safe Order exists!

Page 40: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 40

Recovery from Deadlock

• Recovery through preemption

• Recovery through rollback

• Recovery through killing processes

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 41: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 41

Deadlock Prevention

• Attacking the mutual exclusion condition

• Attacking the hold and wait condition

• Attacking the no preemption condition

• Attacking the circular wait condition

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 42: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 42

Figure 6-13. (a) Numerically ordered resources. (b) A resource graph.

Attacking the Circular Wait Condition

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 43: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 43

Figure 6-14. Summary of approaches to deadlock prevention.

Approaches to Deadlock Prevention

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 44: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 44

Other Issues

• Two-phase locking

• Livelock

• Starvation

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 45: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 45

Locking Protocols

• Shared Locks (read but not write)• Exclusive Locks (read or write)

• A transaction may require a number of locks that must be applied consistently (same sequence..)

• 2 Phase locking protocol– Growing Phase (obtain but not release locks)– Shrinking Phase (release but not obtain locks)

Page 46: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 46

Figure 6-16. Busy waiting that can lead to livelock.

Livelock

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 47: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 47

Starvation

• Occurs when there is a priority based selection process.

• If there are many high priority tasks (in a continuous stream) and only one (or a few) low priority tasks, the low priority tasks will never be selected

• Starvation avoidance– First-come, First-served– Priority enhancement

Page 48: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 48

Summary• Deadlock Characteristics

– Mutual Exclusion:– Hold and wait:– No preemption:– Circular wait:

• Deadlock Strategies1. Just ignore the problem. 2. Detection and recovery. Let deadlocks occur, detect them,

take action.3. Dynamic avoidance by careful resource allocation.4. Prevention, by structurally negating one of the four required

conditions.• Deadlock Tools and Techniques• Other Issues

Page 49: Deadlock Tanenbaum Ch 6 Silberschatz Ch 7. cs431-cotter2 The Deadlock Problem A Computing example: record scanned document System has 1 scanner and 1

cs431-cotter 49

Questions

• In the context of Operating System processes, what is a deadlock? What conditions are required for a deadlock to exist?

• How does a resource allocation graph help identify opportunities for deadlock? Please show an example of a resource allocation graph that includes a deadlock.

• Discuss how the Banker's algorithm ensures that each process that tries to get access to a critical section will eventually be guaranteed of getting in.

• The Banker’s algorithm only requires that there be at least 1 safe order to make a transaction safe, however there may be many different orders possible. Using the example shown on slide 34, how many safe orders exist given the request by P1?

• How might we handle deadlocks? Give at least 3 of the methods discussed in the book and provide examples of each.