0% found this document useful (0 votes)
24 views61 pages

Unit Iii

A deadlock occurs when a set of processes are blocked because each process holds a resource and waits for another resource held by another process in the set, resulting in a circular wait. The four necessary conditions for deadlock are: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlock can be avoided by preventing one of the necessary conditions, such as allowing preemption or assigning priority to resources to prevent circular wait. Banker's algorithm is a deadlock avoidance technique that tests for safe states by comparing available resources to maximum requested resources.

Uploaded by

Mr.Ashok S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views61 pages

Unit Iii

A deadlock occurs when a set of processes are blocked because each process holds a resource and waits for another resource held by another process in the set, resulting in a circular wait. The four necessary conditions for deadlock are: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlock can be avoided by preventing one of the necessary conditions, such as allowing preemption or assigning priority to resources to prevent circular wait. Banker's algorithm is a deadlock avoidance technique that tests for safe states by comparing available resources to maximum requested resources.

Uploaded by

Mr.Ashok S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

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

more processes that hold some resources and wait for


resources held by other(s). 
 A set of blocked processes each holding a resource and

waiting to acquire a resource held by another process in the


set.
 For example, in the below diagram, Process 1 is holding

Resource 1 and waiting for resource 2 which is acquired by


process 2, and process 2 is waiting for resource 1. 
Deadlocks

 Waiting for something for infinite time in


which there is no progress for waiting process.
 Process waits for one another’s action

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

using it and must release the resource after


using it.
 A process may request as many resources as it

requires to carry out its task.


 The number of resources requested may not

exceed the total number of resources available


in the system.
1. REQUEST
2. USE
3. RELEASE
Deadlocks
Deadlocks
Deadlocks
DEALOCK CHARACTERIZATION
NECESSARY CONDITIONS (Coffman Conditions)
1. MUTUAL EXCLUSION:
Only one process at a time can use the resource. At least one
resource must be held in non sharable mode. If another process
requests that resource, the requesting process must be delayed
until the resource has been released.
2. HOLD AND WAIT:
A process must be holding at least one resource and waiting to
acquire additional resources that are currently being held by
other processes.
3. NO PREEMPTION:
 Resources can not be preempted.
 A resource can be released only voluntarily by the
process holding it, after that process has completed
its task.
4. CIRCULAR WAIT:
 A set {P0,P1,…..Pn} of waiting processes must exist
such that P0 is waiting for resource held by P1, P1
is waiting for a resource held by P2,……, Pn-1 is
waiting for a resource held by Pn and Pn is waiting
for a resource held by P0.
Resource Allocation Graph (RAG) 
 Resource allocation graph is explained to us what is the
state of the system in terms of processes and resources.
 Like how many resources are available, how many are

allocated and what is the request of each process. 


 Deadlocks can be described more precisely in terms of a

directed graph called a system resource allocation graph.


 Everything can be represented in terms of the diagram.

One of the advantages of having a diagram is, sometimes it


is possible to see a deadlock directly by using RAG.
 We know that any graph contains vertices and edges. So

RAG also contains vertices and edges.


 two type:
 1. Process vertex – Every process will be represented as

a process vertex. Generally, the process will be


represented with a circle.
 2. Resource vertex – Every resource will be represented

as a resource vertex.  the resource will be represented


with a rectangle.
 two type:

1. Single instance type resource 


2. Multi instance type resource
 There are two types of edges in RAG –
1. Assign Edge
2. Request Edge
 Example-1
 Let's consider 3 processes P1, P2 and P3, and

two types of resources R1 and R2. The


resources are having 1 instance each.
 According to the graph, R1 is being used by P1,

P2 is holding R2 and waiting for R1, P3 is


waiting for R1 as well as R2.
The graph is deadlock free since no cycle is being formed in the graph.
Example-2
 P= {P1, P2, P3}
 R= {R1, R2, R3, R4}
 E= {P1->R1, P2->R3, R1->P2, R2->P2, R2->P1,

R3 ->P3}
 Resource instances:

◦ One instance of resource type R1,


◦ Two instances of resource type R2,
◦ One instance of resource type R3,
◦ Two instances of resource type R4
 Process states
o P1 is holding an instance of R2 and waiting for an R1
o P2 is holding an R1 and an R2 and is waiting for an R3
o P3 is holding an R3
Example 3 (Multi-instances RAG)
 From the above example, it is not possible to say
the RAG is in a safe state or in an unsafe state. So
to see the state of this RAG, let’s construct the
allocation matrix and request matrix.
Checking deadlock (safe or not) –
Example 4 (Multi-instances RAG) –
The Available resource
is = (0, 0), but
requirement are (0, 1),
(1, 0) and (1, 0).So you
can’t fulfill any one
requirement.
Therefore, it is in
deadlock.
Methods of handling deadlocks
Various methods to handle deadlock.
1. Deadlock Prevention
2. Deadlock avoidance (Banker's Algorithm)
3. Deadlock detection & recovery
4. Deadlock Ignorance (Ostrich Method)
Deadlock Prevention
 Deadlock prevention is eliminating one of the
necessary conditions of deadlock so that only safe
requests are made to OS and the possibility of deadlock
is excluded before making requests.
 E.g- A table which is standing on its four legs. if we

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

simultaneously by multiple processes, but write


operation cannot. Write operation requires sequential
access, so, some processes have to wait while another
process is doing a write operation.
Hold and Wait
 Hold and wait is a condition in which a process is holding one
resource while simultaneously waiting for another resource that is
being held by another process. The process cannot continue till it gets
all the required resources.
 There are two ways to eliminate hold and wait:-
 By eliminating wait:
 The process specifies the resources it requires in advance so that it
does not have to wait for allocation after execution starts.
 For Example: Process1 declares in advance that it requires both
Resource1 and Resource2
 By eliminating hold:
 The process has to release all resources it is currently holding before
making a new request.
 For Example: Process1 has to release Resource2 and Resource3
before making request for Resource1.
No preemption
 Preemption is temporarily interrupting an executing
task and later resuming it.
 For example, if a process P1 is using a resource and a

high priority process P2 requests for the resource,


process P1 is stopped and the resources are allocated to
P2.
 There are two ways to eliminate this condition by

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

as the current state of the system and the resource


allocation, to ensure that a deadlock never occurs.
 Resource-allocation state is defined by the number of

available and allocated resources, and the maximum


demands of the processes.
 Two kind of states are there.
1. Safe State
2. Unsafe State
Safe State : if the system can allocate all the resources
requested by all the processes without entering into
deadlock.
Unsafe State : If the system cannot fulfill the request of all
processes then the state of the system is called unsafe.

 Deadlock avoidance can be done with Banker’s


Algorithm. 
Banker’s Algorithm 
 Bankers' Algorithm is resource allocation and deadlock
avoidance algorithm which test all the request made by
processes for resources, it checks for the safe state, if after
granting request system remains in the safe state it allows
the request and if there is no safe state it doesn’t allow the
request made by the process. 
 It handles multiple instances of same resource.
 Inputs to Banker’s Algorithm: 

1. Max need of resources by each process. [MAX]


2. Currently, allocated resources by each process. [Allocation]
3. Max free available resources in the system.[Available]
Example:
Let us consider the following snapshot for understanding the
banker's algorithm:
Process Allocation MAX Available Need
A B C D A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0        
P1 1 0 0 0 1 7 5 0                
P2 1 3 5 4 2 3 5 6                
P3 0 6 3 2 0 6 5 2                
P4 0 0 1 4 0 6 5 6                

1.Calculate the content of the need matrix?


2.Check if the system is in a safe state? Fid out safe sequence.
3.Determine the total sum of each type of resource?
Need = Max – Allocation
Available = Allocation + Available
Satisfy = Need <= Available
Process Allocation MAX Available Need
A B C D A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0 0 0 0 0
P1 1 0 0 0 1 7 5 0 1 5 3 2 0 7 5 0
P2 1 3 5 4 2 3 5 6 2 8 8 6 1 0 0 2
P3 0 6 3 2 0 6 5 2 2 14 11 8 0 0 2 0

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

Safe sequence: P0,P2, P3, P4,P1


System is safe : Yes
The total sum of each type of resource: 3 14 12 12
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 Needi <= 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:

If Finish[i] == true; for all 'n' processes, then the


system is in safe state. Otherwise, the system is in unsafe
state.
Deadlock Detection And Recovery
 Deadlock detection and recovery is the process of
detecting and resolving deadlocks in an operating
system.
 Deadlock detection involves identifying when a deadlock

has occurred in the system. This can be done through the


use of algorithms, such as the Banker’s Algorithm or the
Wait-For Graph Algorithm, which check for the presence
of the necessary conditions for deadlock.
 If OS doesn’t apply any mechanism to prevent the

deadlock, the system considers that deadlock will


definitely occur.
Deadlock Detection
 If a system does not employ either a deadlock-
prevention or a deadlock avoidance algorithm, then a
deadlock situation may occur.
 In this environment, the system may provide:

• An algorithm that examines the state of the system


to determine whether a deadlock has occurred.
• An algorithm to recover from the deadlock.
1. Single Instance of Each Resource Type
 If all resources have only a single instance, then we can
define a deadlock detection algorithm that uses a variant of
the resource-allocation graph, called a wait-for graph.
 We obtain this graph from the resource-allocation graph by

removing the resource nodes and collapsing the


appropriate edges.
 More precisely, an edge from Pi to Pj in a wait-for graph

implies that process Pi is waiting for process Pj to release a


resource that Pi needs.
• A deadlock exists in the system if and only if the wait-for graph
contains a cycle.
• To detect deadlocks, the system needs to maintain the wait-for graph
and periodically invoke an algorithm that searches for a cycle in the
graph.
2 Several Instances of a Resource Type
 The wait-for graph scheme is not applicable to a resource-
allocation system with multiple instances of each resource
type. We turn now to a deadlock detection algorithm that is
applicable to such a system.
 The algorithm employs several time-varying data structures

that are similar to those used in the banker’s algorithm.


 The algorithm examine the state of the system and

determines whether the deadlock arrived or not.


Banker’s or Safety algorithm
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      

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:

If Finish[i] == true; for all 'n' processes, then the


system is in safe state. Otherwise, the system is in unsafe
state.
Recovery from Deadlock

Process/Resource Preemption:  Process Termination


Process Termination: Pessimistic Approach  
 To eliminate the deadlock, we can simply kill one or more processes.
For this, we use two methods.
 Abort all the Deadlocked Processes: 
Aborting all the processes will certainly break the
deadlock, but with a great expense. The deadlocked
processes may have computed for a long time and the
result of those partial computations must be discarded and
there is a probability to recalculate them later. 
 Abort one process at a time until deadlock is eliminated: 

Abort one deadlocked process at a time, until deadlock


cycle is eliminated from the system. Due to this method, there
may be considerable overhead, because after aborting each
process, we have to run deadlock detection algorithm to
check whether any processes are still deadlocked. 
Advantages of Process Termination:
 It is a simple method for breaking a deadlock.
 It ensures that the deadlock will be resolved quickly, as

all processes involved in the deadlock are terminated


simultaneously.
 It frees up resources that were being used by the

deadlocked processes, making those resources


available for other processes.
Resource Preemption: Optimistic Approach
 To eliminate deadlocks using resource preemption, we
preempt some resources from processes and give those
resources to other processes. This method will raise three
issues.  
 1. Selecting a victim: 

We must determine which resources and which


processes are to be preempted and also the order to
minimize the cost. 
 2. Rollback: 

We must determine what should be done with the process


from which resources are preempted. One simple idea is
total rollback. That means abort the process and
restart it. 
 Starvation: 
In a system, it may happen that same process is
always picked as a victim. As a result, that process
will never complete its designated task. This situation
is called Starvation and must be avoided. One
solution is that a process must be picked as a victim
only a finite number of times. 
Process Management And
Synchronization
 In a single processor multiprogramming system the
processor switches between the various jobs until to
finish the execution of all jobs. These jobs will share the
processor time to get the simultaneous execution. Here
the overhead is involved in switching back and forth
between processes.

You might also like