0% found this document useful (0 votes)
55 views

Deadlock and Handling of Deadlock by OS

Uploaded by

mdsabeerali04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Deadlock and Handling of Deadlock by OS

Uploaded by

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

Deadlock and handling

of deadlock by OS

-Cherry Khosla
What is a Deadlock?

A C

B
Deadlock
• In a multiprogramming system, a number of processes compete for a
limited number of resources.
• If a resource is not available at that instant of time, then, a process
enters into a waiting state.
• If a process is unable to change its state indefinitely, it is known as a
deadlock situation.
(because the resource(s) requested by
the process is/are held by
some other waiting process)
Necessary conditions for a deadlock
to occur
• Deadlock can arise in the system if following four conditions hold true
simultaneously
• Mutual Exclusion: One or more than one resource are non-
shareable (Only one process can use at a time)
• Hold and Wait: A process is holding at least one resource and
waiting for resources.
• No Preemption: A resource cannot be taken from a process
unless the process releases the resource.
• Circular Wait: A set of processes are waiting for each other in
circular form.
Handling Deadlocks
• Following are the ways to handle deadlock in the system
• Deadlock prevention
• Deadlock avoidance
• Deadlock detection and Recovery
Resource Allocation Graph
• Deadlock can be described more precisely in terms of a directed graph called a
system Resource Allocation Graph(RAG).
• This graph is a diagrammatical representation of a deadlock in the system
• The RAG consists of a set of vertices V and a set of edges E.
• Vertices:
• P=(P1,P2,p3….Pn), the set consisting of all the active processes
• R=(R1,R2,R3….Rn), the set of resource types in the system
• Edges:
• Request Edge
• Assignment Edge
How RAG identifies Deadlock?

Cycle in a resource allocation graph is necessary but not sufficient condition for detection of a deadlock
NOTE: If every resource have only one instance in the RAG, then cycle in the graph is both necessary and
sufficient condition for a deadlock detection.
Question
• In a resource allocation graph, an edge from process Pi to resource Rj
is known as
A) Request Edge
B) Assignment Edge
Example
Is there a deadlock in the system represented
through RAG?
Example
Is there a deadlock in the system represented through RAG?
Example
Deadlock Prevention
• Deadlock prevention provides a set of methods for ensuring that at
least one of the necessary conditions cannot hold.
• Mutual Exclusion
• Use sharable resources
• Hold and wait
• Allocate all the resources initially
• allows a process to request resources only when it
has none.
Deadlock Prevention
• No-preemption
• We first check whether resources are available. If they are, we allocate them.
• If they are not, we check whether they are allocated to some other process
that is waiting for additional resources. If so, we preempt the desired
resources from the waiting process and allocate them to the requesting
process. If the resources are neither available nor held by a waiting process,
the requesting process must wait.
• Circular Wait
• process requests resources in an increasing order of enumeration.
Practice Problem based on
deadlocks
• Consider there are n number of processes in the system P1,P2,P3….Pn
where-
• Process P1 requires x1 units of resources R
• Process P2 requires x2 units of resources R
• Process P3 requires x3 units of resources R and so on.

• In worst case
• The number of units that each process holds= One less than its max demand
Two scenarios
1. Maximum number of processes that can participate to ensure no
deadlock
2. Minimum number of units of resources required to ensures no
deadlock
Question
• A system is having 3 user processes each requiring 2 units of resource
R. The minimum number of units of R such that no deadlock will
occur?
a)3
b)5
c)4
d)6
Question
• A system is having 3 user processes P1, P2 and P3 where P1
requires 2 units of resource R, P2 requires 3 units of resource
R, P3 requires 4 units of resource R. The minimum number of
units of R that ensures no deadlock is _____?
Question
• A system is having 10 user processes each requiring 3 units of
resource R. The minimum number of units of R such that no
deadlock will occur _____?
Question
• If there are 6 units of resource R in the system and each
process in the system requires 2 units of resource R, then how
many processes can be present at maximum so that no
deadlock will occur?
Question
• If there are 100 units of resource R in the system and each
process in the system requires 4 units of resource R, then how
many processes can be present at maximum so that no
deadlock will occur?
Question
• What is the minimum number of resources required to ensure that
deadlock will never occur, if there are currently three processes P1,
P2 and P3 running in a system whose maximum demand for the
resources of same type are 3, 4, and 5 respectively.
Deadlock Avoidance
• To implement the deadlock avoidance methods, the prior knowledge
of the resources currently available, the resources currently allocated
to each process, and the future requests and releases of each process
is required.
• The various algorithms that use this approach differ in the amount
and type of information required.
Deadlock Avoidance
• What is safe state?
• A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock.
• More formally, a system is in a safe state only if there exists a safe sequence.
Algorithms/methods to be used
• Single Instance- Resource Allocation Graph
• Multiple Instances-Banker’s Algorithm
Resource Allocation Graph
• This is a variant of previous resource allocation graph.
• In this graph along with request and assignment edges we have claim
edge.
• Claim Edge: A claim edge Pi — > Rj indicates that process P, may request
resource R, at some time in the future.
• It is represented in the graph by a dashed line.
• Like a request edge, a claim edge points from a process to a resource
type.
Claim Edge Representation
Safety Algorithm/Banker’s Algorithm
Safety Algorithm/Banker’s Algorithm
Example
• Total Instances
A=10
B=5
C=7
Practice Problem-1
Using Banker’s algorithm, answer
the following questions:-
i) How many resources of type A, B,
C, D are there?
ii) What are the contents of need
matrix?
iii) Find if the system is in safe state?
If it is, find the safe sequence.
Practice Problem-2
• Assume that there are three resources, A, B, and C. There are 4
processes P0 to P3. At T0 we have the following snapshot of the
system:
Is the system in a safe state? Why or why
not?
Resource Request Algorithm
Step-1 Check request <= need
Goto Step-2
Step-2 Check request<=available
Goto Step-3
Step-3 Available=Availabe-Request
Allocation=Allocation+Request
Need=Need-Request
Step-4 Check if new state is safe or not using Banker’s Algorithm
Example

If the system is in a safe state, can the following requests be granted, why or why not?
a)P1 requests (2,1,1,0)
b)P1 requests (0,2,1,0)
Deadlock Detection
• If deadlock arise in the system, then system has to detect it. In
deadlock detection we use the following methods
• Methods:
• For Single Instance: Wait-for-graph
• For Multiple Instance: Variation of Banker’s Algorithm
Wait-For-Graph
• An edge from Pi to Pj implies that process Pi is waiting for process Pj
to release a resource that Pi needs
• An edge Pi -> Pj exists in a wait-for graph if and only if the
corresponding resource-allocation graph contains two edges Pi -> Rq
and Rq ->Pj for some resource Rq
Wait-For-Graph
Example
R1 R2

P4 P2
P3

R3
P1
Banker’s Algorithm for Deadlock
Detection •Available- A vector of length
m indicates the number of
available resources of each
type.
•Allocation- An n*m matrix
defines the number of resources
of each type currently allocated
to a process. Column represents
resource and resource represent
process.
•Request- An n*m matrix
indicates the current request of
each process. If request[i][j]
equals k then process Pi is
requesting k more instances of
resource type Rj.
Recovery from Deadlock
• When a Deadlock Detection Algorithm determines that a
deadlock has occurred in the system, the system must
recover from that deadlock. There are two approaches
of breaking a Deadlock:
• Process Termination:
• Resource Preemption:
Practice MCQ questions
• A deadlock situation can arise if which of the following conditions hold simultaneously in the system
I)Mutual Exclusion
II)Hold and Wait
III)No Preemption
IV) Circular Wait

Choose the correct option


a) Only I
b) I, II and II
c) Only II and III
d) I, II III and IV
Practice MCQ questions
2. To ensure deadlock never occurs, the system can use
1.Deadlock prevention scheme
2.Deadlock avoidance scheme
3.Deadlock detection and recovery scheme

A)1,2
B)1,2,3
C)3
D)2,3
• A computer system has 6 tape drives with n processes competing for
them. Each process needs 3 tape drives. The maximum value for n
for which the system is guaranteed to be deadlock free.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy