R'- I ~ ~ ~L ~Ijwang3/CMSC312/Wang 11-8-07.pdf · Allow the system to enter deadlock state, detect...

2
266 Chapter 8 Deadlocks 8.8 . Summary A deadlock state occurs when two or more processes are waiting indefinitely for an event that can be caused only by one of the waiting processes. Principally, there are three methods for dealing with deadlocks: ' . Use some protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state. . Allow the system to enter deadlock state, detect it, and then recover. . Ignore the problem all together, and pretend that deadlocks never Occur in the system. This solution is the one used by most operating systems, including UNIX. A deadlock situation may occur if and only if four necessary conditions hold simultaneously in the system: mutual exclusion, hold and wait, no pre- emption, and circular wait. To prevent deadlocks, we ensure that at least one of the necessary conditions never holds. Another method for avoiding deadlocks that is less stringent than the prevention algorithms is to have a priori information on how each process will be utilizing the resources. The banker's algorithm, for example, needs to know the maximum number of each resource class that may be requested by each process. Using this information, we can define a deadlock-avoidance algorithm. If a system does not employ a protocol to ensure that deadlocks will never occur, then a detection-and-recovery scheme must be employed. A deadlock- detection algorithm must be invoked to determine whether a deadlock has occurred. If a deadlock is detected, the system must recover either by terminat- ing some of the deadlocked processes, or by preempting resources from some of the deadlocked processes. In a system that selects victims for rollback primarily on the basis of cost factors, starvation may occur. As a result, the selected process never completes its designated task. . Exercises 8.1 List three examples of deadlocks that are not related to a computer-system environment. Is it possible to have a deadlock involving only one process? Explain your answer. People have said that proper spooling would eliminate deadlocks. ~er- tainly, it eliminates from contention card readers, plotters, printf~rs, an ~~ on. It is even possible to spool tapes (called staging them), which wo I 8.2 8.3 lit. _ Exercises 267 I" R'- I EJ or . I am am am CIEJ ~ ~ ~L ~I Figure 8.8 Traffic deadlock for Exercise 8.4. leave the resources of CPU time, memory, and disk space. Is it possible to have a d~adlock involving these resources? If it is, how could such a deadlock occur? If it is not, why not? What deadlock scheme would seem best to eliminate these deadlocks (if any are possible), or what condition is violated (if they are not possible)? 8.4 Consider the traffic deadlock depicted in Figure 8.8. a. Show that the four necessary conditions for deadlock indeed hold in this example. b. State a simple rule that will avoid deadlocks in this system. (.:0uppose that a system is in an unsafe state. Show that it is possible for the processes to complete their execution without entering a deadlock state. 8.6 In a real computer system, neither the resources available nor the demands of processes for resources are consistent over long periods (months). Resources break or are replaced, new processes come and go, new resources are bought and added to the system. If deadlock is controlled by the banker's algorithm, which of the following changes can be made S~fely(without introducing the possibility of deadlock), and under what cIrcumstances? a. Increase Available (new resources added) ,~I III~ !1' I" I I ,HI II I " I,;' ~II '~fl I ii'

Transcript of R'- I ~ ~ ~L ~Ijwang3/CMSC312/Wang 11-8-07.pdf · Allow the system to enter deadlock state, detect...

266 Chapter 8 Deadlocks

8.8 . Summary

A deadlock state occurs when two or more processes are waiting indefinitely foran event that can be caused only by one of the waiting processes. Principally,there are three methods for dealing with deadlocks: '

. Use some protocol to prevent or avoid deadlocks, ensuring that the systemwill never enter a deadlock state.

. Allow the system to enter deadlock state, detect it, and then recover.

. Ignore the problem all together, and pretend that deadlocks never Occurin the system. This solution is the one used by most operating systems,including UNIX.

A deadlock situation may occur if and only if four necessary conditionshold simultaneously in the system: mutual exclusion, hold and wait, no pre-emption, and circular wait. To prevent deadlocks, we ensure that at least one ofthe necessary conditions never holds.

Another method for avoiding deadlocks that is less stringent than theprevention algorithms is to have a priori information on how each process willbe utilizing the resources. The banker's algorithm, for example, needs to knowthe maximum number of each resource class that may be requested by eachprocess. Using this information, we can define a deadlock-avoidance algorithm.

If a system does not employ a protocol to ensure that deadlocks will neveroccur, then a detection-and-recovery scheme must be employed. A deadlock-detection algorithm must be invoked to determine whether a deadlock hasoccurred. If a deadlock is detected, the system must recover either by terminat-ing some of the deadlocked processes, or by preempting resources from someof the deadlocked processes.

In a system that selects victims for rollback primarily on the basis of costfactors, starvation may occur. As a result, the selected process never completesits designated task.

. Exercises

8.1 List three examples of deadlocks that are not related to a computer-systemenvironment.

Is it possible to have a deadlock involving only one process? Explain youranswer.

People have said that proper spooling would eliminate deadlocks. ~er-tainly, it eliminates from contention card readers, plotters, printf~rs, an ~~on. It is even possible to spool tapes (called staging them), which wo

I 8.2

8.3

lit. _

Exercises 267

I"

R'- I

EJ or . IamamamCIEJ

~ ~

~L ~I

Figure 8.8 Traffic deadlock for Exercise 8.4.

leave the resources of CPU time, memory, and disk space. Is it possibleto have a d~adlock involving these resources? If it is, how could such adeadlock occur? If it is not, why not? What deadlock scheme would seembest to eliminate these deadlocks (if any are possible), or what conditionis violated (if they are not possible)?

8.4 Consider the traffic deadlock depicted in Figure 8.8.

a. Show that the four necessary conditions for deadlock indeed hold inthis example.

b. State a simple rule that will avoid deadlocks in this system.

(.:0uppose that a system is in an unsafe state. Show that it is possible for theprocesses to complete their execution without entering a deadlock state.

8.6 In a real computer system, neither the resources available nor the demandsof processes for resources are consistent over long periods (months).Resources break or are replaced, new processes come and go, newresources are bought and added to the system. If deadlock is controlledby the banker's algorithm, which of the following changes can be madeS~fely(without introducing the possibility of deadlock), and under whatcIrcumstances?

a. Increase Available (new resources added)

,~I

III~!1'I"

II,HI III "

I,;' ~II

'~fl Iii'

Exercises 269

268 Chapter 8 Deadlocks

,,

\

;'

.

1

IIJ

b. Decrease Available (resource permanently removed from system)

c. Increase Max for one process (the process needs more resources thanallowed, it may want more)

d. Decrease Max for one process (the process decides it does not needthat many resources)

e. Increase the number of processes

f. Decrease the number of processes

8.7 Prove that the safety algorithm presented in Section8.5.3requires an orderof m x n2 operations.

8.8 Consider a system consisting of four resources of the same type that areshared by three processes, each of which needs at most two resources.

~hOWthat the system is deadlock-free.

. Consider a system consisting of m resources of the same type, being shared

. by n processes. Resourcescanbe requested and released by processes onlyone at a time. Show that the system is deadlock-free if the following twoconditions hold:

type banker's scheme cannot be implemented by individual applicationof the single-resource-type scheme to each resource type.

. 8.12 Can a system detect that some of its processes are starving? If you answer"yes," explain how it can. If you answer "no," explain how the system candeal with the starvation problem.

~onsider the following snapshot of a system:

I

II

II

a. The maximum need of each process is between 1 and m resources

b. The sum of all maximum needs is less than m + n

Answer the following questions using the banker's algorithm:

a. What is the content of the matrix Need?

b. Is the system in a safe state?

c. If a request from process PI arrives for (0,4,2,0), can the request be

/" ) granted immediately?

~onsider the following resource-allocation policy. Requests and releasesfor resources are allowed at any time. If a request for resources cannotbe satisfied because the resources are not available, then we check anyprocesses that are blocked, waiting for resources. If they have the desiredresources, then these resources are taken away from them and are givento the requesting process. The vector of resources for which the waitingprocess is waiting is increased to include the resources that were takenaway.

For example, consider a system with three resource types and the vectorAvailable initialized to (4,2,2). If process Po asks for (2,2,1), it gets them. IfPI asks for (1,0,1), it gets them. Then, if Po asks for (0,0,1), it is blocked(resource not available). If P2 now asks for (2,0,0), it gets the available one(1,0,0) and one that was allocated to Po (since Po is blocked). Po's Allocationvector goes down to (1,2,1),and its Needvector goes up to (1,0,1).

a. Can deadlock occur? If so, give an example. If not, which necessarycondition cannot occur?

b. Can indefinite blocking occur?

ii

\1

II

I

'I'II

111

8.10 Consider a computer system that runs 5,000 jobs per month with nodeadlock-prevention or deadlock-avoidance scheme. Deadlocks occurabout twice per month, and the operator must terminate and rerun about10 jobs per deadlock. Each job is worth about $2 (in CPU time), and thejobs terminated tend to be about half-done when they are aborted.

A systems programmer has estimated that a deadlock-avoidancealgorithm (like the banker's algorithm) could be installed in the systemwith an increase in the average execution time per job of about 10 percent.Since the machine currently has 30-percent idle time, all 5,000 jobs permonth could still be run, although turnaround time would increase byabout 20 percent on average.

a. What are the arguments for installing the deadlock-avoidance algo-rithm ?

b. What are the arguments against installing the deadlock-avoidancealgorithm?

8.11 We can obtain the banker's algorithm for a single resource type from thegeneral banker's algorithm simply by reducing the dimensionality of thevarious arrays by 1. Show through an example that the multiple-resource-

li!l:

111111

III

Allocation Max Available

ABCD ABCD ABCDPo 0012 0012 1520PI 1000 1750

P2 1354 2356P3 0632 0652P4 0014 0656