You are on page 1of 32

CSC 302

Operating
Systems
PART TWO: PROCESS MANAGEMENT:
Lecture11: Deadlocks:
Lecture 11:

Scope:
Introduction to Deadlocks.
Deadlock management.
Goals:
Examine conditions that lead to deadlocks.
Discuss different methods applied in deadlock
management.

Lecture 11 CSC302 Oerating System 2


Deadlocks An
Introduction:
In a multiprogramming environment, several
processes may compete for a finite number
of resources.
Situation:
A system consists of a finite number of resources
to be distributed among a number of competing
processes.
A process requests a resource if the resource is
not available at that time, the process enters a
wait state.

Lecture 11 CSC302 Oerating System 3


Introduction

If the resource is held by another waiting


state, then the two may end up waiting for
ever.
This situation is called a deadlock.
Example1 deadlock:
A certain law passed in Kansas said in part:
when two trains approach each other at a crossing,
both shall come to a full stop and neither shall
start up again until the other has gone.

Lecture 11 CSC302 Oerating System 4


Introduction

Computer systems have resources that can


be used by one process at a time,
for example printers.
Having two processes simultaneously writing to
the same printer leads to gibberish.
Having two processes using the same slot in the
process table will lead to a system crash.
A resource can be a hardware device or a
piece of information.

Lecture 11 CSC302 Oerating System 5


Introduction

Consequently, the operating system has the


ability to grant a process exclusive access to
certain resources.
In certain circumstances, a process needs
exclusive access to more than one resource.
Deadlocks can occur when processes have
been granted exclusive access to resources
(devices, files and so forth).
Deadlocks can also occur in hardware.
Lecture 11 CSC302 Oerating System 6
System Resources:

Resources come in two types:


preemptable and
non-preemptable.
A preemptable resource is one that can be
taken away from the process owning it with
no ill effects. E.g. Memory.
A non-preemptable resource is one that
cannot be taken away from its current owner
without causing the computation to fail. E.g.
Printers.
Lecture 11 CSC302 Oerating System 7
Resources

In general deadlocks involve non-


preemptable resources.
Potential deadlocks that involve preemptable
ones can usually be solved by reallocating
resources from one process to another.

Lecture 11 CSC302 Oerating System 8


Example2resource
reallocation:
Consider a system with 512K of user memory, one
printer and two 512K processes that each wants to
print something.
Process A requests and gets the printer, then starts
computing value to print.
Before it is through with its computations, it exceeds its
time quantum and is swapped out.
Process B now runs and tries unsuccessfully to acquire the
printer.
Now we have a deadlock situation because A has the
printer and B has the memory and neither can proceed
without the resource held by the other.
However, it is possible to preempt (take away) the memory
from process B by swapping it out and swapping A in. Now
A can run and do its printing.

Lecture 11 CSC302 Oerating System 9


Resources
A process must request a resource before using it,
and must release the resource after using it.
The sequence of events required to use a resource
is:
Request the resource
Use the resource
Release the resource
If the resource is not available when it is requested,
the requesting process is forced to wait. In some
systems, the process block when a resource request
fails and is awakened when it becomes available.

Lecture 11 CSC302 Oerating System 10


Principles of Deadlocks:

A deadlock can be formally defined as:


A set of processes is deadlocked if each
process in the set is waiting for an event that
only another process in the set can cause.
For example;
In a database system, a process may have to lock
several records to avoid race conditions. If
process A locks record R1 and process B locks
record R2 and then each process tries to lock the
other ones record, we have a deadlock.

Lecture 11 CSC302 Oerating System 11


Deadlock principles
Because all the processes are waiting, none of them
will ever cause any of the event that could wake up
any of the other members of the set, and all the
processes continue to wait forever.
In most cases, the event that each process is
waiting for is the release of some resource that is
currently possessed by another member of the set.
None of the processes can run, none of them can
release any resource and none of them can be
awakened.

Lecture 11 CSC302 Oerating System 12


Necessary conditions for
Deadlock:
There are four conditions that must hold for a
deadlock to occur:
(i.) Mutual exclusion:
Each resource is either currently assigned to

exactly one process or is available


At least one resource must be held in a

nonsharable mode. i.e.


Only one process at a time can use the resource.
If another process requests the resource, it must
be delayed until the resource has been released.
Lecture 11 CSC302 Oerating System 13
(ii.) Hold and wait condition:
Processes currently holding resources granted

earlier can request new resources


There must exist a process holding at least one

resource and waiting to acquire additional


resources that are currently being held by other
processes.
(iii.) Non preemption condition:
Resources previously granted cannot be forcibly

taken away from a process. They must be


explicitly released by the process holding them.
A resource can be released only voluntary by the
process holding it, after it has completed its task.

Lecture 11 CSC302 Oerating System 14


(iv.) Circular wait condition:
There must be a circular chain of two or more

processes, each of which is waiting for a resource


held by the next member of the chain. i.e.
There must exist a set {p , p ,,p ] of waiting
0 1 n
processes such that pi is waiting for the resource
that is held by pi+1 and pn is waiting for the
resource held by p0.
All of these conditions must hold for a
deadlock to occur.
If one or more of these conditions is absent,
no deadlock can occur.

Lecture 11 CSC302 Oerating System 15


Deadlock Management:

In general three main strategies are used for


dealing with deadlocks:
Just ignore the problem altogether pretend that
deadlocks never occur in the system.
Detection and recovery allow the system to
enter a deadlock state and then recover.
Use a protocol to ensure that the system will
never enter a deadlock state:
Dynamic avoidance by careful resource allocation
Prevention, by structurally negating one of the four
required conditions

Lecture 11 CSC302 Oerating System 16


Deadlock prevention is a set of methods for
ensuring that at least one of the necessary
conditions dont hold.
Deadlock avoidance requires the operating
system to be given, in advance, additional
information concerning which resources a
process will request and use during its
lifetime.

Lecture 11 CSC302 Oerating System 17


The ostrich algorithm:
The simplest approach is the Ostrich algorithm: stick your head
in the sand and pretend there is no problem. Different people
react to this algorithm in different ways. Some people say that it
is totally unacceptable and that deadlocks must be prevented at
all costs.
In the UNIX operating system there are potentially many
deadlocks that are not even detected, let alone, automatically
broken.
The total number of processes in the system is determined by
the number of entries in the process table. Thus process table
slots are finite resources. If a FORK fails because the table is
full, reasonable approach is to wait a random time and try again.

Lecture 11 CSC302 Oerating System 18


Now suppose that a UNIX system has 100 process
slots. Ten processes are running and each needs to
create 12 sub-processes. After each process has
created 9 processes, the original 10 processes and
the new 90 processes have exhausted the table.
Each of the 10 original processes now sits in an
endless loop forking and failing - a deadlock.
Nonetheless, the probability of this happening is
very small.
The UNIX approach is to ignore such problems on
the assumption that processes and users will prefer
an occasional deadlock rather than having
restrictions.

Lecture 11 CSC302 Oerating System 19


Detection and Recovery:

When this technique is used, the system


does not do anything except monitor the
requests and release of resources.
Every time a resource is requested or
released, the resource graph is updated and
a check is made to see if any cycles exist.
If a cycle exists => DEADLOCK
Resource graph??
Recovery??

Lecture 11 CSC302 Oerating System 20


Deadlock Prevention:

The strategy is to impose suitable restrictions


on processes so that deadlocks are
structurally impossible.
The four conditions stated earlier provide a
clue to some possible solutions. If we can
ensure that at least one of these conditions is
never satisfied, then deadlocks will be
impossible.

Lecture 11 CSC302 Oerating System 21


The mutual exclusion condition:
If no resources were ever assigned exclusively to a

single process, no deadlocks would occur.


However, doing this would lead to chaos, for

instance, in the case of a printer.


By spooling printer output, several processes can

generate output at the same time. In this model, the


only process that actually requests the physical
printer is the printer daemon. Since the daemon
never requests any other resources, we can
eliminate deadlocks for the printer.
Unfortunately, not all devices can be spooled.

Lecture 11 CSC302 Oerating System 22


The hold-and-wait condition:
If we can prevent processes that hold resources

from waiting for more resources, we can eliminate


deadlocks.
One way is to require that all processes request all

their resources before starting execution.


If everything were available, the process would be allocated
whatever they needed and could run to completion.
If one ore more resources were busy, nothing would be
allocated and the process would just wait.
The problem with this approach is that many processes
would not know what they required until they have started
running.

Lecture 11 CSC302 Oerating System 23


A slightly different approach to break the
hold-and-wait condition is to require a
process requesting for a resource to first
temporarily release all the resources it
currently hold.
Only if the request is successful can it get the
original resources back.

Lecture 11 CSC302 Oerating System 24


Non preemption condition:
It is impossible to tackle this condition.

If a process has been assigned the printer

and it is in the middle of printing its output,


forcibly taking away the printer because a
needed plotter is not available will lead to a
mess.

Lecture 11 CSC302 Oerating System 25


The circular wait condition:
can be eliminated in several ways.
One way is to simply say that a process is entitled to a
single resource at a time. If it needs a second one, it must
release the first one. For a process that needs to copy a
large file from tape to disk, this is not acceptable.
Another way is to provide a global numbering of all
resources in the system, for example, as shown below. The
rule is that:
processes can request for resources whenever they want

to, but all the requests must be made in a certain


numerical (canonical) order.
A process may first request a printer and then a tape

drive, but it may not request a plotter then a printer.


CD-ROM => Printer => Plotter =>Tape Drive => Robot Arm

Lecture 11 CSC302 Oerating System 26


Deadlock Recovery:

When deadlocks occur, and are detected, the


system needs to recover from the deadlock
state.
Two possibilities:
Inform the operator that a deadlock has occurred,
and let the operator deal with the deadlock
manually.
Let the system recover from the deadlock
automatically.

Lecture 11 CSC302 Oerating System 27


Deadlock recovery

Two main options (solutions) exist for


breaking deadlocks:
Process termination.
Resource preemption.

Lecture 11 CSC302 Oerating System 28


Process Termination:

Simply abort one or more processes to break


the circular wait.
Two approaches:
Abort all deadlocked processes.
Abort one process at a time until the deadlock
cycle is eliminated.
In both cases, the system reclaims all
resources allocated to the terminated
processes.
Lecture 11 CSC302 Oerating System 29
Another crude method is to periodically check
to see if there are any processes that have
been continuously blocked for more than say,
1 hour.
Such processes are then killed.
This strategy is common in large main frame
computers.

Lecture 11 CSC302 Oerating System 30


Resource Preemption:

Successively preempt some resources from


processes and give these resources to other
processes, until the deadlock cycle is broken.
Three issues need to be addressed:
Selecting a victim
Which resource and which process?
Order of preemption v/s cost?
Number of resources a deadlocked process is holding?
Time remaining for a process to terminate?

Lecture 11 CSC302 Oerating System 31


Rollback:
Process must be rolled back to a safe state.
Options:
Abort the process and then restart it.
Roll back the process only as far as necessary.
Starvation:
How do we ensure that starvation doesnt occur?
We must ensure a process can be picked as a
victim only a small, finite number of times.

Lecture 11 CSC302 Oerating System 32

You might also like