Professional Documents
Culture Documents
Systems
Sixth Edition
CHAPTER 5
PROCESS MANAGEMENT
Learning Objectives 2
(contd)
None of them can be removed easily without causing the
systems overall functioning to suffer.
The system needs to recognize the combination of
conditions before they occur and threaten to cause the
system to lock up.
Conditions for Deadlock 31
(cont'd.)
Mutual exclusion
Allowing only one process access to a dedicated resource.
The first condition for deadlock.
Resource holding
Holding resource and not releasing it
Waiting for other job to retreat
The second condition for deadlock.
No preemption
Lack of temporary reallocation of resources.
The third condition for deadlock.
Conditions for Deadlock 32
(cont'd.)
Circular wait
Each process involved in the impasse is waiting for
another to voluntarily release the resource so that at
least one will be able to continue on and eventually
arrive at the destination.
Fourth condition for deadlock.
All four conditions are required for the deadlock to
occur.
Deadlock remains until one condition removed
Modeling Deadlocks 33
Directed graphs
Circles represent processes
Squares represent resources
Solid arrow from resource to process
Process holding resource
Solid arrow from a process to resource
Process waiting for resource
Arrow direction indicates flow
Cycle in graph
Deadlock involving processes and resources
Modeling Deadlocks 34
(cont'd.)
Modeling Deadlocks 35
(cont'd.)
Three graph scenarios to help detect deadlocks
System has three processes (P1, P2, P3)
System has three resources (R1, R2, R3)
Scenario one: no deadlock
Resources released before next process request
Scenario two: deadlock
Processes waiting for resource held by another
Scenario three: no deadlock
Resources released before deadlock
Modeling Deadlocks 36
(cont'd.)
No deadlock
Resources released before next process request
Modeling Deadlocks 37
(cont'd.)
Modeling Deadlocks 38
(cont'd.)
Deadlock
Processes waiting for resource held by another
Modeling Deadlocks 39
(cont'd.)
Modeling Deadlocks 40
(cont'd.)
No deadlock
Resources released before deadlock
Modeling Deadlocks 41
(cont'd.)
Modeling Deadlocks 42
(cont'd.)
Another example
Resources of same type
Allocated individually or grouped in same process
Graph clusters devices into one entity
Allocated individually or grouped in different
process
Graph clusters devices into one entity
Modeling Deadlocks 43
(cont'd.)
Strategies for Handling 44
Deadlocks
The requests and releases are received in an
unpredictable order, which makes it very
difficult to design a foolproof preventive policy.
OSs use one of three strategies to deal with
deadlocks:
Prevent one of the four conditions from occurring.
Avoid the deadlock if it becomes probable.
Detect the deadlock when it occurs and recover
from it gracefully.
Strategies for Handling 45
Deadlocks (cont'd.)
Prevention:
To prevent deadlocks, the OS must eliminate one
of the four necessary conditions:
Complicated by the fact that the same condition
cant be eliminated from every resource.
Mutual exclusion:
Necessary in any computer system because some
resources, such as memory, CPU, and dedicated
devices must be exclusively allocated to one user at a
time.
In the case of I/O devices (printers, the mutual
exclusion may be bypassed by spooling.
Strategies for Handling 46
Deadlocks
Prevention: (cont'd.)
Resource Holding:
A job holds on to one resource while waiting for another one thats not
yet available.
Could be sidestepped by forcing each job to request, at creation time,
every resource it will need to run to completion.
If every job in a batch system is given as much memory as it needs,
then the number of active jobs will be dictated by how many can fit in
memory.
Would significantly decrease the degree of multiprogramming.
Peripheral devices would be idle because they would be allocated to a
job even though they wouldnt be use all the time.
Strategies for Handling 47
Deadlocks
Prevention: (cont'd.)
Resource Holding:
Used successfully in batch environments although it:
Reduced the effective use of resources;
Restricted the amount of multiprogramming.
Doesnt work well in interactive systems.
Strategies for Handling 48
Deadlocks (cont'd.)
Prevention (cont'd.)
No preemption:
Could be bypassed by allowing the OS to deallocate
resources from jobs.
Can be done if the state of the job can be easily
saved and restored.
As when a job is preempted in a round robin
environment or a page is swapped to secondary
storage in a virtual memory system.
Preemption of a dedicated I/O device, during the
modification process, however, can require some
extremely unpleasant recovery tasks. Not accepted
to preempt dedicated I/O device or files during
modification.
Strategies for Handling 49
Deadlocks (cont'd.)
Prevention (cont'd.)
Circular Wait:
Hierarchical Ordering:
Based on a numbering system for the resources:
Printer = 1;
Disk = 2;
Tape = 3;
Plotter = 4;
And so on.
Strategies for Handling 50
Deadlocks
Prevention (cont'd.)
(cont'd.)
The system forces each job to request its resources in
ascending order.
If a job needed a printer and then a plotter, it would request them
in that order.
Printer (#1)
Plotter (#4)
If the job required the plotter first and then a plotter, it would still
request the printer first even though it wouldnt be used right away.
A job could request a printer (#1) and then a disk (#2) and then a
tape (#3); but if it needed another printer (#1) late in its
processing, it would still have to anticipate that need when it
requested the first one, and before it requested the disk.
Strategies for Handling 51
Deadlocks (cont'd.)
Prevention (cont'd.)
This scheme of hierarchical ordering removes the
possibility of a circular wait and, therefore,
guarantees the removal of deadlocks. It doesnt
require that jobs state their maximum needs in
advance, but it does require that the jobs anticipate
the order in which they will request resources.
From the perspective of a system designer, one of
the difficulties of this scheme is discovering the best
order for the resources so that the needs of the
majority of the users are satisfied.
Another difficulty is that of assigning a ranking to
nonphysical resources such as files or locked
database records where there is no basis for
assigning a higher number to one over the other.
Strategies for Handling 52
Deadlocks (cont'd.)
Avoidance:
Even if the OS cant remove one of the conditions
for deadlock, it can avoid one if the system knows
ahead of time the sequence of requests
associated with each of the active processes.
Dijkstras Bankers Algorithm was proposed in
1965 to regulate resource allocation to avoid
deadlocks.
Strategies for Handling 53
Deadlocks (cont'd.)
Avoidance:
Based on a bank with a fixed amount of capital
that operates on the following principles:
No customer will be granted a loan exceeding the
banks total capital.
All customers will be given a maximum credit limit
when opening an account.
No customer will be allowed to borrow over the limit.
The sum of all loans will not exceed the banks total
capital.
Strategies for Handling 54
Deadlocks (cont'd.)
Avoidance:
The bank has total capital of $10,000 and has
three customers, C1, C2, and C3, who have
maximum credit limits of $4,000, $5,000, and
$8,000, respectively.
Strategies for Handling 55
Deadlocks (cont'd.)
Strategies for Handling 56
Deadlocks (cont'd.)
Avoidance:
A few weeks later after more loans have been
made, and some have been repaid -
Strategies for Handling 57
Deadlocks (cont'd.)
Strategies for Handling 58
Deadlocks (cont'd.)
Avoidance:
An unsafe state doesnt necessarily lead to
deadlock, but it does indicate that the system is
an excellent candidate for one.
If we substitute jobs for customers and dedicated
devices for dollars.
In this example the system has 10 devices.
Strategies for Handling 59
Deadlocks (cont'd.)
Strategies for Handling 60
Deadlocks (cont'd.)
Strategies for Handling 61
Deadlocks (cont'd.)
The OS must be sure never to satisfy a request
that moves it from a safe state to an unsafe one.
As users are satisfied, the OS must identify the
job with the smallest number of remaining
resources and make sure that the number of
available resources is always equal to, or greater
than, the number needed for this job to run to
completion.
Requests that would place the safe state in
jeopardy must be blocked by the OS until they
can be safely accommodated.
Strategies for Handling 62
Deadlocks (cont'd.)
If this solution is expanded to work with several
classes of resources, the system sets up a
resource assignment table for each type of
resource and tracks each table to keep the
system in a safe state.
Strategies for Handling 63
Deadlocks (cont'd.)
Although the Bankers Algorithm has been used
to avoid deadlocks in systems with a few
resources, it isnt always practical for most
systems:
As they enter the system, jobs must predict the
maximum number resources needed.
Not practical in interactive systems.
The number of total resources for each class must
remain constant.
If a device breaks and becomes unavailable, the
algorithm wont work.
Strategies for Handling 64
Deadlocks (cont'd.)
The number of jobs must remain fixed.
Isnt possible in interactive systems where the
number of active jobs is constantly changing.
The overhead cost incurred by running the
avoidance algorithm can be quite high when there
are many active jobs and many devices because it
has to be invoked for every request.
Resources arent well utilized because the
algorithm assumes the worst case and keeps vital
resources unavailable to guard against unsafe
states.
Scheduling suffers as a result of the poor utilization
and jobs are kept waiting for resource allocation.
Strategies for Handling 65
Deadlocks (cont'd.)
Detection
Deadlocks can be detected by building directed
resource graphs and looking for cycles.
The algorithm used to detect circularity can be
executed whenever it is appropriate every hour,
one a day, only when the operator notices that
throughput has deteriorated, or when an angry
user complains.
Strategies for Handling 66
Deadlocks (cont'd.)
Detection algorithm
1. Find a process that is currently using a resource
and not waiting for one.
This process can be removed from the graph
Disconnecting the link tying the resource to the
process.
The resource can be returned to the available list.
This is possible because the process would
eventually finish and return the resource.
Strategies for Handling 67
Deadlocks (cont'd.)
Detection algorithm
2. Find a process thats waiting only for resource
classes that arent fully allocated
(P2, 5.12(c)).
This process isnt contributing to deadlock since it
would eventually get the resource its waiting for,
finish its work, and return the resource to the
available list.
3. Go back to step 1 and continue with steps 1 and 2
until all lines connecting resources have been
removed (5.12(d)).
Strategies for Handling 68
Deadlocks (cont'd.)
Strategies for Handling 69
Deadlocks (cont'd.)
Detection algorithm
If there are any lines left, this indicates that the
request of the process in question cant be
satisfied and that a deadlock exists.
Strategies for Handling 70
Deadlocks (cont'd.)
Strategies for Handling 71
Deadlocks (cont'd.)
Detection algorithm
A circular wait.
Strategies for Handling 72
Deadlocks (cont'd.)
Recovery
Once a deadlock has been detected, it must be
untangled and the system returned to normal as
quickly as possible.
There are several recovery algorithms, but they
have one feature in common:
They all require at least one victim, an
expendable job which, when removed from the
deadlock, will free the system.
Strategies for Handling 73
Deadlocks
Recovery
(cont'd.)
Recovery Methods:
The first and simplest recovery method, and the most drastic, is to
terminate every job thats active in the system and restart them
from the beginning.
The second method is to terminate only the jobs involved in the
deadlock and ask their users to resubmit them.
The third method is to identify which jobs are involved in the
deadlock and terminate them one at a time, checking to see if the
deadlock is eliminated after each removal, until the deadlock has
been resolved.
Once the system is free, the remaining jobs are allowed to complete
their processing and later the halted jobs are started again from the
beginning.
Strategies for Handling 74
Deadlocks
Recovery
(cont'd.)
Recovery Methods:
The fourth method can be put into effect only if the job keeps a
record, a snapshot, of its progress so it can be interrupted and then
continued without starting again from the beginning of its
execution.
This method is favored for long-running jobs to help them make a
speedy recovery.
The fifth method selects a nondeadlocked job, preempts the
resources its holding and allocates them to a deadlocked process
so it can resume execution, thus breaking the deadlock.
Strategies for Handling 75
Deadlocks
Recovery (cont'd.)
Recovery Methods:
The sixth method stops new jobs from entering the system, which
allows the nondeadlocked jobs to run to completion so theyll
release their resources.
Eventually, with fewer jobs in the system, competition for resources is
curtailed so the deadlocked processes get the resources they need to
run to completion.
The only method that doesnt rely on a victim.
Not guaranteed to work unless the number of available resources
surpasses that needed by at least one of the deadlocked jobs to run.
Several factors must be considered to select the victim
that will have the least-negative effect on the system.
Strategies for Handling 76
Deadlocks (cont'd.)
The most common are:
The priority of the job under consideration:
High-priority jobs are usually untouched.
CPU time used by the job:
Jobs close to completion are usually left alone.
The number of other jobs that would be affected
if this job were selected as the victim.
Jobs that are modifying data shouldnt be selected
for termination because the consistency and
validity of the database would be jeopardized.
Starvation 77