Download presentation
1
Tanenbaum Ch 6 Silberschatz Ch 7
Deadlock Tanenbaum Ch 6 Silberschatz Ch 7
2
The Deadlock Problem A Computing example: record scanned document
4/16/2017 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 cs431-cotter cs431-cotter
3
Preemptable and Nonpreemptable Resources
4/16/2017 Preemptable and Nonpreemptable Resources Sequence of events required to use a resource: Request the resource. Use the resource. Release the resource. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
4
Resource Acquisition (1)
4/16/2017 Resource Acquisition (1) Figure 6-1. Using a semaphore to protect resources. (a) One resource. (b) Two resources. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
5
Resource Acquisition (2)
4/16/2017 Figure 6-2. (a) Deadlock-free code. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
6
Resource Acquisition (3)
4/16/2017 Resource Acquisition (3) Figure 6-2. (b) Code with a potential deadlock. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
7
Introduction To Deadlocks
4/16/2017 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. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
8
Deadlock Characterization: Necessary Conditions
4/16/2017 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. cs431-cotter cs431-cotter
9
Deadlock Strategies Strategies for dealing with deadlocks:
4/16/2017 Deadlock Strategies Strategies for dealing with deadlocks: Just ignore the problem. Detection and recovery. Let deadlocks occur, detect them, take action. Dynamic avoidance by careful resource allocation. Prevention, by structurally negating one of the four required conditions. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved 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. cs431-cotter
11
Resource Allocation Graphs
Process Resource Request a Resource Hold a Resource Deadlock A R R A A R A R S B cs431-cotter 11
12
Resource Allocation Graph Multiple Resources with No Cycles
4/16/2017 Resource Allocation Graph Multiple Resources with No Cycles P2 P3 P1 cs431-cotter cs431-cotter
13
4/16/2017 Graph with Deadlock P2 P3 P1 cs431-cotter cs431-cotter
14
4/16/2017 Graph with Deadlock P2 P3 P1 cs431-cotter cs431-cotter
15
Graph with Cycle but no Deadlock
4/16/2017 Graph with Cycle but no Deadlock P2 P1 P3 P4 cs431-cotter cs431-cotter
16
Deadlock Detection with Multiple Resources of Each Type
4/16/2017 Deadlock Detection with Multiple Resources of Each Type Figure 6-6. The four data structures needed by the deadlock detection algorithm. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
17
Deadlock Detection with Multiple Resources of Each Type (2)
4/16/2017 Deadlock Detection with Multiple Resources of Each Type (2) Deadlock detection algorithm: Look for an unmarked process, Pi , for which the i-th row of R is less than or equal to A. If such a process is found, add the i-th row of C to A, mark the process, and go back to step 1. If no such process exists, the algorithm terminates. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
18
Deadlock Detection with Multiple Resources of Each Type (3)
4/16/2017 Figure 6-7. An example for the deadlock detection algorithm. cs431-cotter 18 Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter 18
19
Deadlock 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. cs431-cotter 19
20
4/16/2017 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... cs431-cotter cs431-cotter
21
Safe State Process Max Needs Current Needs Total P0 10 5 12
4/16/2017 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 P P Avail P cs431-cotter cs431-cotter
22
Safe State Process Max Needs Current Needs Total P0 10 5 12
4/16/2017 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 P P ïƒ 4 Avail P ïƒ 1 cs431-cotter cs431-cotter
23
Safe State Process Max Needs Current Needs Total P0 10 5 12
4/16/2017 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 P P ïƒ 0 Avail P ïƒ 5 cs431-cotter cs431-cotter
24
Safe State Process Max Needs Current Needs Total P0 10 5ïƒ 10 12
4/16/2017 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 P ïƒ P Avail P ïƒ 0 cs431-cotter cs431-cotter
25
4/16/2017 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. cs431-cotter cs431-cotter
26
Banker's Example Request: P0 wants 0 2 0
4/16/2017 Banker's Example Request: P0 wants Allocate Max Avail. A B C A B C A B C P P P P P Safe order: ??? cs431-cotter cs431-cotter
27
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P cs431-cotter cs431-cotter
28
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P cs431-cotter cs431-cotter
29
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, cs431-cotter cs431-cotter
30
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, cs431-cotter cs431-cotter
31
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, P3, cs431-cotter cs431-cotter
32
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, P3, cs431-cotter cs431-cotter
33
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, P3, P4, cs431-cotter cs431-cotter
34
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, P3, P4, cs431-cotter cs431-cotter
35
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, P3, P4, P0, cs431-cotter cs431-cotter
36
Banker's Example (change 1)
4/16/2017 Banker's Example (change 1) P1 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P Safe Order: P1, P3, P4, P0, P2 !!! OK cs431-cotter cs431-cotter
37
Banker's Example (change 2)
4/16/2017 Banker's Example (change 2) P4 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P cs431-cotter cs431-cotter
38
Banker's Example (change 2)
4/16/2017 Banker's Example (change 2) P4 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P cs431-cotter cs431-cotter
39
Banker's Example (change 2)
4/16/2017 Banker's Example (change 2) P4 requests ( ) Allocate Max Avail. A B C A B C A B C P P P P P No job can be completed. No Safe Order exists! cs431-cotter cs431-cotter
40
Recovery from Deadlock
4/16/2017 Recovery from Deadlock Recovery through preemption Recovery through rollback Recovery through killing processes cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
41
Deadlock Prevention Attacking the mutual exclusion condition
4/16/2017 Deadlock Prevention Attacking the mutual exclusion condition Attacking the hold and wait condition Attacking the no preemption condition Attacking the circular wait condition cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
42
Attacking the Circular Wait Condition
4/16/2017 Attacking the Circular Wait Condition Figure (a) Numerically ordered resources. (b) A resource graph. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
43
Approaches to Deadlock Prevention
4/16/2017 Approaches to Deadlock Prevention Figure Summary of approaches to deadlock prevention. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
44
Other Issues Two-phase locking Livelock Starvation cs431-cotter
4/16/2017 Other Issues Two-phase locking Livelock Starvation cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved cs431-cotter
45
Locking Protocols Shared Locks (read but not write)
4/16/2017 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) cs431-cotter cs431-cotter
46
Livelock Figure 6-16. Busy waiting that can lead to livelock.
4/16/2017 Livelock Figure Busy waiting that can lead to livelock. cs431-cotter Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved 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 cs431-cotter
48
Summary Deadlock Characteristics Deadlock Strategies
Mutual Exclusion: Hold and wait: No preemption: Circular wait: Deadlock Strategies Just ignore the problem. Detection and recovery. Let deadlocks occur, detect them, take action. Dynamic avoidance by careful resource allocation. Prevention, by structurally negating one of the four required conditions. Deadlock Tools and Techniques Other Issues 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. cs431-cotter
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.