You are on page 1of 17

DEADLOCKS

Lecture 25

4/3/2019 1
Deadlock-Avoidance

Approaches to Handling Deadlocks


Deadlock Detection and Recovery
– grant resource requests when possible, but
periodically check for the presence of deadlock and
then take action to recover from it
Deadlock Avoidance
– do not grant a resource request if this allocation
might lead to deadlock
Deadlock Prevention
– disallow 1 of the 3 necessary conditions of deadlock
occurrence, or prevent the circular wait condition
from happening

4/3/2019 2
Deadlock Prevention
“The goal of deadlock prevention is to
prevent the four conditions being
satisfied.”

4/3/2019 3
Mutual Exclusion: only one process can use a resource
at a time.
Hold and Wait: a process holding at least one resource is
waiting to acquire additional resources which are currently
held by other processes.
Non Preemption: a resource can only be released
voluntarily by the process holding it.
Circular Wait: a cycle of process requests exists (i.e., P0
is waiting for a resource hold by P1 who is waiting for a
resource held 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).

4/3/2019 4
1) Mutual exclusion:
– One process holds a resource in a non-sharable mode.
– Other processes requesting resource must wait for
resource to be released.
2) Hold-and-wait:
– A process must hold at least one allocated resource while
awaiting other resources held by other processes.
3) No preemption:
– Resources not forcibly removed from a process holding it,
the holding process must voluntarily released it.
4) Circular wait
– a closed chain of processes exists, such that each process
holds at least one resource needed by the next process in
the chain.
4/3/2019 5
1- Mutual Exclusion Condition
“Only one process at a time can use the resource.”
If other process requests that resource, the requesting
process must be blocked until the resource has been
released.
2- Hold and Wait Condition
“Processes currently holding resources can request new
resources while waiting for that resource which is held by
other process.”
3- Non-Preemptive Condition
“Resources already allocated to a process cannot be taken
away.”
4- Circular Wait Condition
“There must be a circular chain of two or more processes,
each is waiting for the resource held by the next process.”
4/3/2019 6
Attacking the Mutual Exclusion Condition
Make some resources un-sharable, such as
printers, tape drives etc.
Example: On a single lane bridge, no other
vehicle should be allowed on the bridge while a
vehicle is on it. These are all examples of access
to a resource by a process. If no other process is
allowed access during this time, then access
must be mutually exclusive.

4/3/2019 7
Attacking the Mutual Exclusion Condition
Example: In a file update, where records are read,
modified and written back, no other process should
be allowed access to a record while one process is
changing it.

4/3/2019 8
Attacking the Hold and Wait Condition
1. Each process must request and get all of its
resources before it begins execution.
2. A process never has to wait for what it needs.
3. Process must request all needed resources at one
time

4/3/2019 9
Attacking the Hold and Wait Condition
• Implementation
– A process is given its resources
on a "ALL or NONE" basis
– Either a process gets ALL its
required resources and proceeds
or it gets NONE of them and waits
until it can
4/3/2019 10
Attacking the Non-Preemption Condition

• Implementation
– Resources can be removed from a
process before it is finished with them

4/3/2019 11
Attacking the Non-Preemption Condition

Make it possible for the O/S to make a process give


up a resource. This may result in lost data.

Consider a process given the printer


1. halfway through its job
2. now forcibly take away printer

4/3/2019 12
Attacking the Circular Wait Condition

Circular wait can be eliminated in two ways:


WAY1
Process is entitled only to a single resource at
any moment. If it needs a second one, it must
release the first one.

WAY2
Provide global numbering of all the resources.

4/3/2019 13
Attacking the Circular Wait Condition
1. Process can request resources whenever they want to,
but all requests must be made in numerical order.
2. A process may request first a scanner and then plotter,
but it may not request first the plotter and then printer.
3. In this method the resource allocation graph can never
have cycles.

4/3/2019 14
Attacking the Circular Wait Condition

4/3/2019 15
Starvation
A process may be held for a long time waiting for all its
required resources.
One or more programs are in starvation, when
each of them is waiting for resources for a long
period of time that are occupied by programs.
This is mostly happens in time sharing systems in which the
process which requires less time slot is waiting for the large
process to finish and to release the resources, but the large
process holding the resources for long time and the process
that requires small time slot goes on waiting. Such situation is
starvation for small process
4/3/2019 16
End of Chapter 3

4/3/2019 17

You might also like