Understanding Operating Systems, 7th edition
Chapter 5 Exercises
1. For each of these conditions— deadlock, race, and starvation—give at least two “real
life” examples (not related to a computer system environment) of each of these
concept. Then give your own opinion on how each of the six conditions can be
resolved. ANS: These answers should vary as students apply these concepts to their
real life experiences.
2. Given the ice cream sundae example from the beginning of this chapter, identify
which elements of the deadlock represent the four necessary conditions for this
deadlock. ANS: These answers should vary from student to student as they seek to
assign the four necessary conditions to the example.
3. Using the narrow staircase example from the beginning of this chapter, create a list of
actions or tasks that could be implements by the building manager that would allow
people to use the staircase without risking a deadlock or starvation. ANS: Look for a
rigorous set of rules that eliminate the possibility of both deadlock and starvation.
4. If a deadlock occurs at a combination of downtown intersections, as shown in the
figure below, explain in detail how you would identify that a deadlock has occurred,
how you would resolve it after it happens, and how you would act to prevent it from
happening again. ANS: Answers here vary but generally require the removal of at
least one victim and forcing it to turn prematurely to open the intersection. To prevent
it, look for original ideas that would stand up to real life conditions at busy
intersections.
5. Regarding the role played by the “victim” in deadlock resolution, explain your
answers to these questions.
a. How is the victim chosen? ANS: Student answers may vary as there are several
ways to choose the victim. However, removal of this victim must help resolve the
problem.
b. What is the fate of the victim? ANS: The victim must be prematurely interrupted
and must restart
c. Describe the actions required, if any, to complete the victim’s tasks. ANS: The
stopped process or job may need to restart from the beginning or from some
intermediate point if it’s status has been saved.
UOS, 7e Ann McIver McHoes Chapter Five, Page 1
Understanding Operating Systems, 7th edition
6. The figure below shows a tunnel going through a mountain and two streets parallel to
each other—one at each end of the tunnel. Traffic lights are located at each end of the
tunnel to control the cross flow of traffic through each intersection. Based on this
figure, answer the following questions:
a. How can deadlock occur and under what circumstances? ANS: Deadlock would
occur if the traffic flow was very heavy and the lights were stuck in such a way
that one would allow only north-south traffic to go by at one end of the tunnel's
exits, while the other would allow only east-west traffic to go by at the other end
of the tunnel. Then one of the north-south lanes would go across the intersection
blocking east-west traffic at that end, while one of the east-west lanes would go
across the other intersection blocking north-south traffic at that end.
b. How can deadlock be detected? ANS: Answers here could vary. One way
deadlock could be detected would be by monitoring traffic flow inside the tunnel.
If one lane became congested and stopped moving it could be an indication of
deadlock.
c. Give a solution to prevent deadlock and starvation. ANS: Answers here could
vary. One way would be to synchronize the lights to give preference to the more
congested lanes during heavy traffic while still allowing traffic in the less
congested ones to move along.
7. Consider the directed graph shown below and answer the following questions:
a. Is this system deadlocked? ANS: yes because it cannot be reduced.
b. Which, if any, processes are blocked? ANS: P2 and P3.
c. What is the resulting graph after reduction? ANS: See artwork below for graph.
answer for this exercise
UOS, 7e Ann McIver McHoes Chapter Five, Page 2
Understanding Operating Systems, 7th edition
8. Consider the directed resource graph shown below, and answer the following
questions:
a. Is this system deadlocked? ANS: no because it can be reduced satisfactorily.
b. Which, if any, processes are blocked? ANS: none.
c. What is the resulting graph after reduction? ANS: See artwork below for graph.
answer for this exercise
9. Consider a system with 13 dedicated devices of the same type and all jobs currently
running on this system require a maximum of 3 devices to complete their execution
but they each run for long periods of time with just 2 devices and request the
remaining one only at the very end of the run. Assume that the job stream is endless
and that your operating system’s device allocation policy is a very conservative one:
No job will be started unless all the required drives have been allocated to it for the
entire duration of its run.
a. What is the maximum number of jobs that can be in progress at once? Explain
your answer.
b. What are the minimum and maximum number of devices that may be idle as a
result of this policy? Under what circumstances would an additional job be
started?
ANS:
a. The maximum number of jobs run is 4 because each job is allocated its 3 maximum
devices at the beginning.
b. The minimum number of idle devices is 1 (when all 4 jobs are running at the very end
of their run 3 allocated devices each = 12 devices are allocated and only 1 remain
unallocated). Explanation: If four jobs are in the system and each is in the completion
UOS, 7e Ann McIver McHoes Chapter Five, Page 3
Understanding Operating Systems, 7th edition
stage, then the only device that’s idle is the 13th device, which was not allocated to a
job, as it was from the beginning of this run.
The maximum number of idle devices is 1 for each of the 4 jobs plus the 1 device not
allocated: 4*1+1 = 5. (Of course all 13 drives could be idle if there were no jobs in the
system but this question assumes a continuous job stream.) If the four jobs are at the
stage of using only 2 devices, then the maximum number of idle devices is 5 (the 1
never allocated + 1 each for the 4 running jobs that have not yet neared completion
and requested their last device).
There would have to be at least 3 devices available for allocation before another job
could begin.
10. Consider a system with 14 dedicated devices of the same type. All jobs currently
running on this system require a maximum of five devices to complete their execution
but they each run for long periods of time with just three devices and request the
remaining two only at the very end of the run. Assume that the job stream is endless
and that your operating system’s device allocation policy is a very conservative one:
No job will be started unless all the required drives have been allocated to it for the
entire duration of its run.
a. What is the maximum number of jobs that can be active at once? Explain your
answer.
b. What are the minimum and maximum number of devices that may be idle as a
result of this policy? Under what circumstances would an additional job be
started?
ANS:
a. The maximum number of jobs that can be active at once is 2, because each job is
allocated its five devices at the beginning of the run and there are enough devices to
begin 2 jobs, each with 5 devices. Four devices will remain unallocated.
b. The minimum number of idle devices is 4. There are always four devices that are not
allocated. (Of course all 13 drives could be idle if there were no jobs in the system but
this question assumes a continuous job stream.) The maximum number of idle devices
is 8 (4 that were not allocated plus 2 each for the two jobs that have not yet accessed
their last 2 devices).
UOS, 7e Ann McIver McHoes Chapter Five, Page 4
Understanding Operating Systems, 7th edition
There would have to be 5 devices available before another job would be allowed to
begin.
11. Suppose your operating system is identical to the one described in the (previous exercise)
with 14 devices, but supports the Banker’s Algorithm.
a. What is the maximum number of jobs that can be in progress at once? Explain
your answer.
b. What are the minimum and maximum number of devices that may be idle as a
result of this policy? Under what circumstances would an additional job be
started?
ANS:
a. The max run is 4 because each job is allocated its 3 maximum devices at the beginning
so 12 devices are allocated and 2 remain available which matches the remaining need
for any of the 4 running jobs.
b. At the beginning of the run, two devices are not allocated, but those two can be
allocated to any job so it can finish and release its devices so the next waiting job can
immediately begin its execution.
12. Suppose your operating system is identical to the one described in the previous exercise
but has 16 devices and supports the Banker’s Algorithm.
a. What is the maximum number of jobs that can be in progress at once? Explain
your answer.
b. What are the minimum and maximum number of devices that may be idle as a
result of this policy? Under what circumstances would an additional job be
started?
ANS:
a. The max run is 4. The answer is NOT 5 because if 5 jobs were allocated at the same
time with 3 maximum devices each at the beginning, then 15 devices would be
allocated and that would leave only 1 device available, which is less than the
remaining needs of all of the running jobs. Doing so would put the system into an
unsafe state. Therefore the maximum run is 4 so that the remaining needs of all jobs
can be satisfied.
UOS, 7e Ann McIver McHoes Chapter Five, Page 5
Understanding Operating Systems, 7th edition
b. At the beginning of the run, two devices are not allocated, but those two can be
allocated to any job so it can finish and release its devices.
13-16. For the systems described below, given that all of the devices are of the same type,
and using the definitions presented in the discussion of the Banker’s Algorithm,
answer these questions:
a. Calculate the number of available devices.
b. Determine the remaining needs for each job in each system.
c. Determine whether each system is safe or unsafe.
d. If the system is in a safe state, list the sequence of requests and releases that will
make it possible for all processes to run to completion.
e. If the system is in an unsafe state, show how it’s possible for deadlock to occur.
13. This system has 16 devices. [Instructor note: This is an unsafe state. To change this
exercise to a safe state, just change Job 4’s Maximum Required to 4 from 5 giving it a
remaining need of 2 and not 3.]
Job No. Devices Allocated Maximum Required Remaining Needs
Job 1 5 8 3
Job 2 3 9 6
Job 3 4 8 4
Job 4 2 5 3
ANS:
a. The remaining available devices is 2 [16 - (5+3+4+2) = 2]
b. Max Remaining = 16 additional devices [(3+6+4+3) = 16].
c. This is an unsafe state
d. N/A (no answer required here because it’s in an unsafe state)
e. If any of the jobs actually needs the maximum number of devices, (all of the
maximums are more than the 2 devices that are remaining) then a deadlock will result.
14. This system has 12 devices. [Instructor note: This is an unsafe state. To change this
exercise to a safe state, just change Job 3’s Maximum Required to 6 from 7 giving it a
remaining need of 1.]
UOS, 7e Ann McIver McHoes Chapter Five, Page 6
Understanding Operating Systems, 7th edition
Job No. Devices Maximum Required Remaining Needs
Allocated
Job 1 5 8 3
Job 2 1 4 3
Job 3 5 7 2
ANS:
a. There is one remaining available device [12 - (5+1+5) = 1].
b. Maximum Remaining = 8 more devices (3+3+2) = 8.
c. This system is in an unsafe state
d. N/A (no answer required here because it’s in an unsafe state)
e. If any of the jobs actually needs the maximum number of devices, (all of the
maximums are more than the 1 device remaining) then a deadlock will result.
15. This system has 14 devices. [Instructor note: This is a safe state. To change this
exercise to an unsafe state, just change Job 3’s Maximum Required to 7 from 6 giving
it a remaining need of 2 devices.]
Job No. Devices Allocated Maximum Required Remaining Needs
Job 1 2 6 4
Job 2 4 7 3
Job 3 5 6 1
Job 4 0 2 2
Job 5 2 4 2
ANS:
a. There is one remaining available device [14 – (2+4+5+0+2) = 1]
b. Max Remaining = 12 more devices (4+3+1+2+2) = 12.
c. This system is in a safe state
d. If Job 3 is allowed to finish first, then it will release its allocated devices. When those
are reallocated to the remaining Jobs, they can run in any order to completion.
e. N/A (no answer required here because it’s in a safe state)
16. This system has 32 devices. [Instructor note: This is an unsafe state. To change this
exercise to a safe state, just change Job 2’s Maximum Required to 9 from 10 giving it
a remaining need of 2 and not 3.]
UOS, 7e Ann McIver McHoes Chapter Five, Page 7
Understanding Operating Systems, 7th edition
Job No. Devices Allocated Maximum Required Remaining Needs
Job 1 11 17 6
Job 2 7 10 3
Job 3 12 18 6
Job 4 0 8 8
ANS:
a. The number of remaining available devices is 2. [32 – (11+7+12+0) = 2]
b. Maximum Remaining = 23 additional devices (6+3+6+8) = 23.
c. It is an unsafe state
d. N/A
e. If any of the jobs actually needs the maximum number of devices, (all of the
maximums are more than the 2 devices remaining) then a deadlock will result.
Advanced Exercises
17. Explain how you would design and implement a mechanism to allow the operating
system to detect which, if any, processes are starving. ANS: A solution would be to
time stamp a process when it initially entered the READY queue. Then, as the process
moved along its execution, preemption, and waiting stages the original time stamp
would be compared to the system clock and, if the difference between the two
exceeded a predefined maximum, then the process would be recognized as a victim of
starvation and the Process Scheduler would position it at the head of the READY
queue while ensuring that the resources needed for this process to run were available.
18. As mentioned in this chapter, Havender proposed a hierarchical ordering of resources,
such as disks, printers, files, ports, etc. Explain in detail the limitations imposed on
programs (and on systems) that have to follow such a hierarchical ordering system.
Think of some circumstance for which would you recommend a hierarchical ordering
system and explain your answer. ANS: Hierarchical ordering of resources is used to
suppress the circular wait condition. Resources are grouped into a hierarchy of levels.
Once a process has been allocated resources at one level, it can request other
resources but only at a higher level. It can release resources at a given level only after
releasing all allocated resources at every higher level. After a process has been
UOS, 7e Ann McIver McHoes Chapter Five, Page 8
Understanding Operating Systems, 7th edition
allocated and then has released resources at a given level, it can again request
resources at that same level.
Hierarchical allocation may be more costly to perform than preallocation, but it can
reduce the waste associated with full preallocation. If the order in which a process
needs resources is different from that in which the levels are ordered then this
reduction will not develop. For example, if a printer is needed early in the execution
and the disk drive is needed only at the end, but the disk drive is at a lower level than
the printer, then the disk drive will have to be allocated early and remain unused until
needed. To answer the final part of this question, students should create their own
hierarchy and explain why they would implement the system they have conceived.
17. Suppose you are an operating system designer and have been approached by the
system administrator to help solve the recurring deadlock problem in your
installation’s spooling system. What features might you incorporate into the operating
system so that deadlocks in the spooling system can be resolved without losing the
work (the system processing) that was underway or already performed by the
deadlocked processes? ANS: You could write a program that keeps track of the
amount of space occupied in the spooling device. When it is 75% full, for example,
the program would trigger a warning so that no new output could be sent to the
spooling device. When existing files had been printed out and more space was
available (say 50%), then the spooler would be ready to accept new output.
Another solution could be to have the users create their output files on their allocated
disk space and when their program ended the users would issue a print command that
would send their entire output file to the spooling device. If the spooler was full, then
the operating system would put the arriving output files on a waiting queue and
dispatch them to the spooler as space became available.
19. Given the four primary types of resources—CPU, main memory, storage devices, and
files—select for each one the most suitable technique described in this chapter to fight
deadlock and explain why that is your choice. ANS:
CPU. To prevent deadlock we can remove the no preemption condition. This is
practical, relatively easy to implement and is present in systems that support
preemptive CPU policies, such as Round Robin.
UOS, 7e Ann McIver McHoes Chapter Five, Page 9
Understanding Operating Systems, 7th edition
Memory. To prevent deadlock we can remove the no preemption condition. This is
true of all systems using the virtual memory approach. If a process needs to expand its
working set, then another process' working set may be swapped out to secondary
storage freeing up some page frames.
Storage devices. To prevent deadlock we can remove the mutual exclusion condition
from disk drives, but not from tape drives. It may be that for these devices prevention
of deadlock is not possible, but avoidance is possible by using the Banker's
Algorithm.
Files. To prevent deadlock we can remove the hold and wait (Resource Holding)
condition by requiring preallocation. A process must request its file resources initially
and cannot proceed until they have been allocated, although this would entail a
substantial cost in inefficiency of resource utilization. It may be better to implement a
lock which can keep all other processes out until the first process is finished. In this
case the other processes are not deadlocked, just temporarily blocked.
21. (old ex 18) Consider a banking system with 10 accounts. Funds may be transferred
between two of those accounts by following these steps:
lock A(i); lock A(j);
update A(i); update A(j);
unlock A(i); unlock A(j);
a. Can this system become deadlocked? If yes, show how. If no, explain why not.
ANS: When updating two accounts and being rigorous about each step by locking,
updating, and unlocking each account, the possibility of deadlock is avoided,
assuming that the system resources can function while two accounts are locked.
b. Could the numbering request policy (presented in the chapter discussion about
detection) be implemented to prevent deadlock if the number of accounts is
dynamic? Explain why or why not. ANS: Likewise, if the system can
accommodate all ten accounts locked at the same time, this rigorous locking,
updating, and unlocking sequence would prevent deadlock, regardless of the order
of the accounts being processed.
UOS, 7e Ann McIver McHoes Chapter Five, Page 10