You are on page 1of 6

DEADLOCK IN OPERATING SYSTEMS

In operating systems, processes use different resources in the following ways:

1 Requests for resources

2. Make use of the resources

3. Releases (terminate) it after use

RESOURCES:

Some of the resources are as follows:

 Hardware

 Database

 Files

 I/O

 Etc

Deadlock occurs when a set of processes are blocked because each process is holding a resource
and waiting for a resource which is in the possession of another process.

SIMPLE DEADLOCK SCENARIO/SITUATION

Fig 1 Let’s consider a simple and common real situation as


in fig 1. In a road traffic situation A1, A2, B1,B2,C1,C2, D1 and D2 are cars . Cars A1, B1, C1
and D1 are holding the resources (that is the point where they are. C1 is waiting for D1 to move
so it can move; similarly D1 is waiting for C1 to move so it can move; it applies to A1 and B1
hence a deadlock (no movement as they’ve seized where they are and waiting for other
resources).

Fig 2

Let A1 be process 1, A2 be process 2, R1 be resource 1 and R2 resource 2 as in Fig 2.

The diagram can be interpreted as:

1. A1 is holding R2 and has requested for R1 which it needs to complete

2. Similarly, A2 is holding R1and requested for R2

3. A1 is waiting for A2 to release R1 whereas A2 is also waiting for A1 to release R2 ;


implying A1 cannot have access to R1 and A2 also cannot have access to R2 and this is a
simple or basic deadlock situation.

For deadlock to happen or take place, there are four necessary conditions that needs to be
satisfied. The following are the four necessary conditions for deadlock to occur:

1. Mutual Exclusion

2. Hold and Wait

3. No preemption

4. Circular Wait

MUTUAL EXCLUSION

Fig 3
X – R has been assigned to A1 by OS

Y – A2 cannot access R because R cannot be shared

In mutual exclusion one or more resources available cannot be shared ex. Printer; i.e. to say only
one process can use it at a time. Considering the fig 3, A1 and A2 are two different processes and
R being a resource both A1 and A2 need. In our scenario R has been assigned to A1 OR
implying A2 cannot access it since R is not shareable (mutual exclusion). Not until A1 releases
R, A2 can never have access to R leading to what is known as deadlock situation.

HOLD AND WAIT

Fig 4

Considering Fig 4

W – Resource R2 has been assigned to process A1

Z – Resource R3 has been assigned to process A1

In this scenario, a process is holding at least one resource and waiting for other resources

X – A1 is holding R2 and R3; A1 has requested for another resource R1 and is waiting for R1 to
be released by A2.

It will not release R2 and R3 to any other process not until after getting R1. That is to say, if
it has even finished using R2 and R3, it will not release them not until it gets R3. A1 will not
get access to R1 as A2 is holding it due to mutual exclusion.

A1 is waiting for R1 whiles holding unto R2 and R3. A1 will release R2 and R3 only after
getting R1 even if it has finished using R2 and R3

Y – A2 is holding R1 because R1 has been assigned to A2


NO PREEMPTION

Fig 5

In this case, a resource assigned to a process by the OS cannot be taken away from it unless the
process releases it.

Z – R has been assigned to A1 i.e. A1 is holding R and will only release it after using (execution)
it. The OS cannot interrupt it because the condition is pre-emptive

CIRCULAR WAIT

Under this condition, a set of processes are waiting for each other in a circular form.

Considering a simple circular wait scenario, let A1 and A2 be processes and R1 and R2
resources from Fig

Fig 6

Considering Fig 6 where A1 , A2 are processes whereas R1 and R2 are resources

X – A1 requests R1 (it needs it) but R1 has been assigned to A2 so A1 cannot have access to it.
Y – A2 holds R1

Z – A2 requests for R2 that it needs

W – A1 holds R2

Under this condition A1 requests for R1 but cannot have access to it R1 has been assigned to A2.
Similarly A2 requests for resource R2 but cannot have access to it because it has been assigned
by the operating system to the process A1. You can see it is in a circular form.

NB: For deadlock to occur, all the four conditions should occur simultaneously.

HOW (METHODS) TO HANDLE DEADLOCK

Generally there are three main methods/ways or strategies to handle deadlock, namely:

1. Deadlock Prevention or avoidance

2. Deadlock detection and recovery

3. Ignoring problem all together

DEADLOCK PREVENTION OR AVOIDANCE

The idea in this case is to make sure the system does not get to the deadlock state/stage.

DEADLOCK DETECTION AND RECOVERY

Is to make deadlock to occur then preempt the system to handle the situation

IGNORING PROBLEM ALL TOGETHER

When deadlock is seen or observed to be rare and also not critical, you allow it to occur (you
may pretend that deadlock can never occur) after which system is rebooted.

STRATEGIES TO HANDLE DEADLOCK

1. Pre – emption --- In this case resource from one process can be taken and given to another for
it to complete. This sometimes causes problems.

2. Roll Back --- in this case, records of each process is kept from time to time and in
situation where deadlock is possible, the system is rolled back to the last recorded               state
(or check point) to restart from there by the allocation of different resource.               Even
though there will be loss of progress (time), as a result of the               restarting, the allocation of
the different resource(s) is required to avoid the re-               occurrence of deadlock.

3. Killing of one or more Processes --- In this case, one or more processes are killed, but
remember, after killing a process, you have to restart it and cannot guarantee if deadlock
will not re-occur after the restart. It’s the simplest or easiest.

You might also like