Unit Iii
Unit Iii
DEADLOCKS
Deadlocks- Introduction
A deadlock is a situation where a set of processes are blocked
because each process is holding a resource and waiting for
another resource acquired by some other process.
situation occurs in operating systems when there are two or
indefinitely.
A system consists of a finite number of
resources to be distributed among a number
of competing processes.
A process must request a resource before
R3 ->P3}
Resource instances:
break one of the legs of the table then the table will fall
definitely.
Mutual Exclusion
This condition must hold for non-sharable resources. For
example, a printer cannot be simultaneously shared by
several processes. In contrast, Sharable resources do not
require mutually exclusive access and thus cannot be
involved in a deadlock.
For Example: read operation on a file can be done
preemption:
1. If a process is holding some resources and waiting for
other resources, then it should release all previously held
resources and put a new request for the required
resources again. The process can resume once it has all
the required resources.
For example: If a process has resources R1, R2,
and R3 and it is waiting for resource R4, then it has
to release R1, R2, and R3 and put a new request of
all resources again.
2. If a process P1 is waiting for some resource, and
there is another process P2 that is holding that
resource and is blocked waiting for some other
resource. Then the resource is taken from P2 and
allocated to P1. This way process P2 is preempted
and it requests again for its required resources to
resume the task. The above approaches are possible
for resources whose states are easily restored and
saved, such as memory and registers.
Circular Wait
In circular wait, two or more processes wait for
resources in a circular order.
To eliminate circular wait,
we assign a priority to
each resource. A process
can only request resources
in increasing order of
priority.
In the example above,
process P3 is requesting resource R1, which has a number lower
than resource R3 which is already allocated to process P3. So
this request is invalid and cannot be made, as R1 is already
allocated to process P1.
Deadlock avoidance
Deadlock avoidance is a technique used in operating
systems to prevent the situation where two or more
processes are unable to proceed because each is waiting
for one of the others to release a resource.
Deadlock avoidance uses dynamic information, such
P4 0 0 1 4 0 6 5 6 2 14 12 12 0 6 4 2
Total = 3 14 12 12 3 14 12 12
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Process Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2 0 1 0
P2 3 0 3 0 0 0 3 1 3
P3 2 1 1 1 0 0 5 2 4
P4 0 0 2 0 0 2 7 2 4
Process Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2 0 1 0
P2 3 0 3 0 0 1
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Banker’s Safety Algorithm
The algorithm for finding out whether or not a system is in
a safe state can be described as follows:
Step 1: Let Work and Finish be vectors of length ‘m’
and ‘n’ respectively.
m=number of resource
n= number of processes in the system
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n.
Step 2: Find an i’th process for
Finish[i] = false and Requesti <= Work
If no such process is available then go to step 4.
Banker’s Safety Algorithm
Step 3:
Work = Work +Allocation(i)
Finish[i] = true
Go to step 2
Step 4: