Deadlocks
Deadlocks
Deadlocks
Deadlocks
1. OVERVIEW A set of blocked processes each holding a
resource and waiting to acquire a resource
1.1. Objective
held by another process in the set.
After reading this chapter, you should
understand: A process requests resources; if the resources
are not available at that time, the process
the problem of deadlock.
enters a wait state. It may happen that
the four necessary conditions for deadlock
waiting processes will never again change
to exist.
state, because the resources they have
the problem of indefinite postponement.
requested are held by other waiting
the notions of deadlock prevention,
processes. This situation is called a deadlock.
avoidance, detection and recovery.
algorithms for deadlock avoidance and
detection.
how systems can recover from deadlocks
1.2. Definitions
time, then can be taken away and allocated to Request: The process requests the resource.
another process and then be reallocated to If the request cannot be granted immediately
the first process without any ill effects. (for example, if the resource is being used by
Memory, buffers, CPU, array processor etc are another process), then the requesting process
some preemptable resources. must wait until it can acquire the resource.
For example, a system with 32 MB of user Use: The process can operate on the resource
memory, one printer, and two 32-MB (for example, if the resource is a printer, the
processes that each want to print something. process can print on the printer).
Process A request and gets the printer, then
Release: The process releases the resource.
start to compute the values to print. Before it
has finished with the computation, it exceeds
its time quantum and is swapped out.
Process B now runs and tires, unsuccessfully,
to acquire the printer. Potentially, we now
have a deadlock situation, because A has the
printer and B has the memory, and neither
2. CONDITIONS NECESSARY FOR
can proceed without the resource held by the
DEADLOCK
other. Fortunately, it is possible to preempt
(take away) the memory from B by swapping
it out and swapping A in. Now A can run, do Mutual exclusions
its printing, and then release the printer. No Hold and wait
deadlock occurs. No preemption
Circular wait
A nonpreemptable resource is the one that
cannot be taken away from the current
process and given to another without causing In 1971 Coffman et al. showed that four
side effects or may even fail the computation conditions must hold in order for deadlock to
altogether. Eg. Disks, files, mutex etc. CD occur:
recorders are not preemptable at an arbitrary 1. Mutual exclusions: A resource may be
moment. Service Request Block (SRB) which acquired exclusively by only one process at
represents a request for a system service are a time
often non-preemptable.
2. Hold and wait: Processes that currently
If a process has begun to burn a CD-ROM, hold resources can request and wait for
suddenly taking the CD recorder away from it additional resources which are currently
and giving it to another process will result in a held by other processes.
garbled CD. 3. No preemption: Resources previously
Each process utilizes a resource as follows: granted cannot be taken away from the
processes that hold them; they must be
voluntarily released by the holding process.
That is, Once a process has obtained a
resource, the system cannot remove it
from the process's control until the process
has finished using the resource.
4. Circular wait: Two or more processes are
locked in a "circular chain" in which each
process is waiting for one or more
resources that the next process in the
chain is holding.
Eg. P0 is waiting for a resource held
by P1 who is waiting for a resource held
U by Pj ... who is waiting for a resource held
by P(n-1) which is waiting for a resource held
by Pn which is waiting for a resource held
by P0.
Introduction to Operating System 3
Fig: Process
A rectangular vertices is used to
represent resource classes, with dots
within the vertex to represent instances of Some Resource Allocation Graphs
the resource. resource allocation graph without
deadlock:
o P1 wants a resource held by P2
o no process is requesting an
instance of R4
Detect deadlock and, when it occurs, take This approach to the deadlock problem
steps to recover. Allow the system to enter a anticipates deadlock before it actually occurs.
deadlocked state, then deal with and eliminate
This approach employs an algorithm to access
the problem
the possibility that deadlock could occur and
3. 3. Deadlock Avoidance
acting accordingly.
Avoid deadlock by careful resource It differs from deadlock prevention, which
scheduling. Anticipates deadlock before it guarantees that deadlock cannot occur by
actually occurs. denying one of the necessary conditions of
4. 4. Deadlock Prevention deadlock.
Prevent deadlock by resource scheduling so as This requires that the system has some
to negate at least one of the four conditions. information available up front.
Now we consider each strategy in order of Each process declares in advance, the
decreasing severity. maximum number of resources of each type
that it may need.
Deadlock Prevention Dynamically examine the resource
By ensuring that at least one of these allocation state to ensure that there can
conditions cannot hold, we can prevent the never be a circular-wait condition.
occurrence of a deadlock The system's resource-allocation state is
Mutual Exclusion – Not required for defined by the number of available and
sharable resources; must hold for allocated resources, and the maximum
nonsharable resources. Spooling can possible demands of the processes.
convert serially reusable devices into When a process requests an available
seemingly shareable virtual devices as in resource, the system must decide if
case of printers, thus denying the mutual immediate allocation leaves the system in
exclusion precondition. a safe state.
Hold and Wait – must guarantee that
whenever a process requests a resource, it That is,
does not hold any other resources. o Do not start a process it its maximum
o Require process to request and be requirement might lead to deadlock.
allocated all its resources before it begins o Do not grant an incremental resource
execution, or allow process to request request if this allocation might lead to
resources only when the process has deadlock.
none.
o Low resource utilization; starvation Safe State
possible.
No Preemption – If a process that is The key to a state being safe is that there is
holding some resources requests another at least one way for all users to finish.
resource that cannot be immediately The system is in a safe state if there exists
allocated to it, then all resources currently a safe sequence of all processes P1, P2, P3 …
being held are released. Pn, where, for each Pi, the resources which
o Preempted resources are added to the list Pi can still request can be satisfied by
of resources for which the process is o the currently available resources plus
waiting. o the resources held by all of the Pj's,
o Process will be restarted only when it can where j < i.
regain its old resources, as well as the If Pi resource needs are not immediately
new ones that it is requesting. available, then Pi can wait
Circular Wait – impose a total ordering of until all Pj have finished.
all resource types, and require that each
When Pj is finished, Pi can obtain needed
process requests resources in an increasing
resources, execute,
order of enumeration.
return allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its
Deadlock Avoidance
needed resources, and so on.
6 Essentials of Operating System
Banker's Algorithm
Banker’s algorithm is a deadlock avoidance
algorithm. It is named so because this
algorithm is used in banking systems to
determine whether a loan can be granted or
not.
Consider there are n account holders in a
bank and the sum of the money in all of their
accounts is S. Everytime a loan has to be
granted by the bank, it subtracts the loan
amount from the total money the bank has.
Then it checks if that difference is greater
than S. It is done because, only then, the
bank would have enough money even if all the
n account holders draw all their money at
once.
Banker’s algorithm works in a similar way in
computers. Whenever a new process is
Example:
created, it must exactly specify the maximum
instances of each resource type that it needs.
8 Essentials of Operating System
For both single and multiple instances per 3. Otherwise, pretend to allocate the
resource types requested resources to Pi :
The resource manager must know in 4. available[j] = available[j] -
advance the maximum needs of all the request[i][j]
processes. 5. allocation[i][j] = allocation[i][j] +
Processes request 1 resource at a time request[i][j]
Request granted ONLY if it results in a safe need[i][j] = need[i][j] - request[i][j]
state i.e. the request plus current usage of
the processes is less than the need after Once the resources are allocated, check to see
allocating the request there exists a if the system state is safe. If unsafe, the
sequence in which all processes can run to process must wait and the old resource-
completion even if they demand their full allocated state is restored.
claim. Safety algorithm (to check for a safe
If request results in an unsafe state, it is state):
denied and user holds resources and waits 1. Let work be an integer array of length m,
until request is eventually satisfied initialized to available.
Users must guarantee to release resources Let finish be a boolean array of length n,
in finite time initialized to false.
2. Find an i such that both:
Resource manager guarantees to allocate
resources in finite time Process
o finish[i] == false
o need[i] <= work
If no such i exists, go to step 4
3. work = work + allocation[i];
Let us assume that there are n processes
finish[i] = true;
and m resource types. Some data structures
Go to step 2
are used to implement the banker’s algorithm.
4. If finish[i] == true for all i, then the
They are:
system is in a safe state, otherwise unsafe.
We call Banker's algorithm when a request for
Run-time complexity: O(m × n²).
R is made. Let n be the number of processes
Example: consider a system with 5 processes
in the system, and m be the number of
(P0 ... P4) and 3 resources types (A(10) B(5)
resource types.
C(7))
resource-allocation state at time t0:
Define:
Process Allocation Max Need Available
available[m]: the number of units of R
currently unallocated (e.g., available[3] = A B C A B C A B C A B C
2) P0 0 1 0 7 5 3 7 4 3 3 3 2
max[n][m]: describes the maximum P1 2 0 0 3 2 2 1 2 2
demands of each process (e.g., max[3][1]
= 2) P2 3 0 2 9 0 2 6 0 0
allocation[n][m]: describes the current P3 2 1 1 2 2 2 0 1 1
allocation status ( e.g., allocation[5][1] = P4 0 0 2 4 3 3 4 3 1
3) Is the system in a safe state? If so, which
need[n][m]: describes sequence satisfies the safety criteria?
the remaining possible need (i.e., need[i][j] < P1, P3, P4, P2, P0 >
= max[i][j] - allocation[i][j]) Now suppose, P1 requests an additional
instance of A and 2 more instances of type C.
Resource-request algorithm: request[1] = (1,0,2)
1. check if request[1] <= need[i] (yes)
Define: 2. check if request[1] <= available[i] (yes)
request[n][m]: describes the current 3. do pretend updates to the state
outstanding requests of all processes
Process Allocation Max Need Available
(e.g., request[2][1] = 3)
1. If request[i][j] <= need[i][j], to to step 2; A B C A B C A B C A B C
otherwise, raise an error condition. P0 0 1 0 7 5 3 7 4 3 3 3 2
2. If request[i][j] > available[j], then the P1 3 0 2 3 2 2 0 2 0
process must wait.
Introduction to Operating System 9
7 2 6
What is a Livelock?
There is a variant of deadlock called livelock.
This is a situation in which two or more
processes continuously change their state in
response to changes in the other process(es)
without doing any useful work. This is similar
to deadlock in that no progress is made but
differs in that neither process is blocked or
waiting for anything.
A human example of livelock would be two
people who meet face-to-face in a corridor
and each moves aside to let the other pass,
but they end up swaying from side to side