0% found this document useful (0 votes)
13 views

Chapter06 Deadlocks

Uploaded by

phitruongpro211
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter06 Deadlocks

Uploaded by

phitruongpro211
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Deadlocks

Chapter 6

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
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 & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Resource Acquisition (1)

Figure 6-1. Using a semaphore to protect resources.


(a) One resource. (b) Two resources.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Resource Acquisition (2)

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


(b) Code with a potential deadlock.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Definition

A set of processes is deadlocked if …


•Each process in the set waiting for an
event
•That event can be caused only by
another process

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Conditions for Resource Deadlocks

Four conditions that must hold:


1.Mutual exclusion
2.Hold and wait
3.No preemption
4.Circular wait condition

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Modeling (1)

Figure 6-3. Resource allocation graphs. (a) Holding a


resource. (b) Requesting a resource. (c) Deadlock.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Modeling (2)

Figure 6-4. An example of how deadlock occurs


and how it can be avoided.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Modeling (3)

Figure 6-4. An example of how deadlock occurs


and how it can be avoided.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Modeling (4)

Figure 6-4. An example of how deadlock occurs


and how it can be avoided.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Modeling (5)
Strategies are used for dealing with deadlocks:
1.Ignore the problem, maybe it will go away.
2.Detection and recovery. Let deadlocks occur,
detect them, and take action.
3.Dynamic avoidance by careful resource
allocation.
4.Prevention, by structurally negating one of the
four required conditions.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Detection with One
Resource of Each Type (1)
Example of a system – is it deadlocked?
1.Process A holds R, wants S
2.Process B holds nothing, wants T
3.Process C holds nothing, wants S
4.Process D holds U, wants S and T
5.Process E holds T, wants V
6.Process F holds W, wants S
7.Process G holds V, wants U

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Detection with One
Resource of Each Type (2)

Figure 6-5. (a) A resource graph. (b) A cycle extracted from (a).

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Algorithm to Detect Deadlocks (1)
1. For each node, N in the graph, perform
following five steps with N as starting node.
2. Initialize L to empty list, and designate all arcs
as unmarked.
3. Add current node to end of L, check to see if
node now appears in L two times. If so, graph
contains a cycle (listed in L) and algorithm
terminates

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Algorithm to Detect Deadlocks (2)
4. From given node, see if there are any unmarked
outgoing arcs. If so, go to step 5; if not, go to
step 6.
5. Pick unmarked outgoing arc at random, mark it.
Then follow to new current node and go to step
3.
6. If this is initial node, graph does not contain
cycles, algorithm terminates. Otherwise, dead
end. Remove it and go back to the previous
node.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Detection with Multiple
Resources of Each Type (1)

Figure 6-6. The four data structures needed


by the deadlock detection algorithm.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Detection with Multiple
Resources of Each Type (2)
Deadlock detection algorithm:
1.Look for 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, go back to step 1.
3.If no such process exists, algorithm terminates.

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Detection with Multiple
Resources of Each Type (3)

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


Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Recovery from Deadlock

Possible Methods of recovery (though none


are “attractive”):
1.Preemption
2.Rollback
3.Killing processes

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Avoidance
Resource Trajectories

Figure 6-8. Two process resource trajectories.

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Safe and Unsafe States (1)

Figure 6-9. Demonstration that the state in (a) is safe.

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Safe and Unsafe States (2)

Figure 6-10. Demonstration that the state in (b) is not safe.

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Banker’s Algorithm for
Single Resource

Figure 6-11. Three resource allocation states:


(a) Safe. (b) Safe. (c) Unsafe.
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Banker’s Algorithm for
Multiple Resources (1)

Figure 6-12. The banker’s algorithm with multiple resources.

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Banker’s Algorithm for
Multiple Resources (2)
1. Look for a row, R, whose unmet resource needs are
all smaller than or equal to A. If no such row exists,
system will eventually deadlock.
2. Assume the process of row chosen requests all
resources needed and finishes. Mark that process as
terminated, add its resources to the A vector.
3. Repeat steps 1 and 2 until either all processes are
marked terminated (safe state) or no process is left
whose resource needs can be met (deadlock)

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Deadlock Prevention
Assure that at least one of conditions is
never satisfied
•Mutual exclusion
•Hold and wait
•No Preemption
•Circular wait

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Attacking Circular Wait Condition (1)

Figure 6-13. (a) Numerically ordered resources.


(b) A resource graph
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Attacking Circular Wait Condition (2)

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

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Communication Deadlocks

Figure 6-15. A resource deadlock in a network.

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
Livelock

Figure 6-16. Busy waiting


that can lead to livelock.

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.
End

Chapter 6

Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All rights reserved.

You might also like