You are on page 1of 4

11/10/2022

Deadlock
A process in operating system uses resources in the following way.
1. Requests a resource
2. Use the resource
3. Releases the resource
A deadlock is a situation in which more than one process is blocked
because it is holding a resource and also requires some resource that
Operating System is acquired by some other process. Therefore, none of the processes
gets executed.

Unit-III

1 2

Example
Necessary Conditions for Deadlock
The four necessary conditions for a deadlock to arise are as follows.
1. Mutual Exclusion: Only one process can use a resource at any
given time i.e. the resources are non-sharable.
2. Hold and wait: A process is holding at least one resource at a
time and is waiting to acquire other resources held by some other
process.
3. No preemption: The resource can be released by a process
voluntarily i.e. after execution of the process. Process 1 holds "Resource 1" and needs "Resource 2" while Process 2
4. Circular Wait: A set of processes are waiting for each other in a holds "Resource 2" and requires "Resource 1". This creates a situation of
deadlock because none of the two processes can be executed. Since the
circular fashion. For example, lets say there are a set of
resources are non-shareable they can only be used by one process at a
processes {P0,P1,P2,P3} such that P0 depends time(Mutual Exclusion). Each process is holding a resource and waiting
on P1, P1 depends on P2, P2 depends on P3 and P3 depends for the other process the release the resource it requires. None of the two
on P0. This creates a circular relation between all these processes processes releases their resources before their execution and this creates a
and they have to wait forever to be executed. circular wait. Therefore, all four conditions are satisfied.

3 4

1
11/10/2022

Methods for handling deadlock


There are three ways to handle deadlock a) Abort one process at a time until the system recovers from the
1) Deadlock prevention or avoidance: deadlock.
a) The idea is to not let the system into a deadlock state. b) Resource Preemption: Resources are taken one by one from a
b) Since deadlock occurs when all the above four conditions are process and assigned to higher priority processes until the
met, we try to prevent any one of them, thus preventing a deadlock is resolved.
deadlock. 3) Ignore the problem altogether: If deadlock is very rare, then let
c) When a process requests a resource, the deadlock avoidance it happen and reboot the system. This is the approach that both
algorithm examines the resource-allocation state. Windows and UNIX take.
d) it requires additional information such as how many resources
of each type is required by a process. If the system enters into
an unsafe state, it has to take a step back to avoid deadlock.

2) Deadlock detection and recovery:


Let deadlock occur, then do preemption to handle it once occurred.
Some ways of recovery are as follows.
a) Aborting all the deadlocked processes. 5 6

Deadlock Prevention : 2. The process will make a new request for resources after
Deadlock can be prevent by eliminating any of the four dead lock releasing the current set of resources. This solution may lead
conditions. to starvation.
Eliminate Mutual Exclusion Eliminate No Preemption
It is not possible to dis-satisfy the mutual exclusion because some Preempt resources from the process when resources required
resources, such as the tape drive and printer, are inherently non- by other high priority processes.
shareable. (spooling) Eliminate Circular Wait
Eliminate Hold and wait Each resource will be assigned with a numerical number. A
1. Allocate all required resources to the process can request the resources increasing/decreasing. order
process before the start of its execution, of numbering.
this way hold and wait condition is For Example, if P1 process is allocated R5 resources, now
eliminated but it will lead to low device next time if P1 ask for R4, R3 lesser than R5 such request will
utilization. for example, if a process not be granted, only request for resources more than R5 will
requires printer at a later time and we be granted.
have allocated printer before the start
of its execution printer will remain
blocked till it has completed its
7 8
execution.

2
11/10/2022

Deadlock Avoidance
1. Resource allocation Graph
2. Banker's Algorithm
Resource Allocation Graph
The resource allocation graph is the pictorial representation of the
state of a system. The resource allocation graph is the complete
information about all the processes which are holding some
resources or waiting for some resources. There are two types of edges in RAG –
1. Assign Edge – If you already assign a resource to a process then it
RAG contains vertices and edges. In RAG vertices are two type –
is called Assign edge.
1. Process vertex – Every process will be represented as a process
2. Request Edge – It means in future the process might want some
vertex. Generally, the process will be represented with a circle.
resource to complete the execution, that is called request edge.
2. Resource vertex – Every resource will be represented as a resource
vertex. It is also two type –
Single instance type resource – It represents as a box, inside the
box, there will be one dot. So the number of dots indicate how many
instances are present of each resource type.
Multi-resource instance type resource – It also represents as a box,
inside the box, there will be many dots present. 9 10

11 12

3
11/10/2022

Banker’s Algorithm Example:


Banker‘s Algorithm is resource allocation and deadlock avoidance Total resources in system: A B C D
algorithm which test all the request made by processes for 6576
resources, it checks for the safe state, if after granting request Available system resources are: A B C D
system remains in the safe state it allows the request and if there is 3112
no safe state it doesn’t allow the request made by the process. Processes (currently allocated resources): A B C D
Inputs to Banker’s Algorithm: P1 1 2 2 1
1. Max need of resources by each process. P2 1 0 3 3
2. Currently, allocated resources by each process. P3 1 2 1 0
3. Max free available resources in the system. Processes (maximum resources): A B C D
The request will only be granted under the below condition: P1 3 3 2 2
1. If the request made by the process is less than equal to max need P2 1 2 3 4
to that process. P3 1 3 5 0
2. If the request made by the process is less than equal to the freely Need = maximum resources - currently allocated resources.
available resource in the system. Processes (need resources): A B C D
P1 2 1 0 1
13
P2 0 2 0 1 14
P3 0 1 4 0

You might also like