SlideShare a Scribd company logo
DEADLOCK
IN OPERATING SYSTEM
AGENDA
 Introduction
 Example Diagram
 Conditions necessary for Deadlock
 Mutual Exclusion
 Hold & Wait
 No Preemption
 Circular Wait
 Ending Notes
2
Definition:
A deadlock is a situation where a set of processes is blocked because
each process is holding a resource and waiting for another resource
acquired by some other process.
Example:
when two trains are coming toward each other on the same track and
there is only one track, none of the trains can move once they are in
front of each other. This is a practical example of deadlock.
Introduction
How does deadlock occur in the operating system
Example Diagram
Conditions necessary for Deadlock
 Mutual Exclusion
 Hold & Wait
 No Preemption
 Circular Wait
Mutual Exclusion
Mutual exclusion condition requires at least one resource must be
in non-shareable mode, which means that only one process can
use the resource at any given time.
Example:
Full screen process in our computer cannot be used by two apps
simultaneously.
Process 1
Resource 1
only used by
Hold & Wait
The hold and wait condition specifies that a process must be holding
at least one resource while waiting for other processes to release
resources that are currently held by other processes.
No Preemption
A resource cannot be taken from a process unless the process releases the
resource. If preemption was allowed then deadlock would never occur
because then no process would have been able to hold a resource for long
amount of time.
Example:
If your speaker is running an audio and after some time you click on some
other audio it starts playing but in case no preemption was allowed we
would have to wait for the first audio to end and if in case it was on loop we
will end up in a deadlock.
Circular Wait
A set of processes waiting for each
other in circular form.
Deadlock detection
Deadlock detection is the process of finding out whether any process
are stuck in loop or not. There are several algorithms like:
 Resource Allocation Graph (RAG)
A resource allocation graphs shows which resource is held by which
process and which process is waiting for a resource of a specific kind.
 Banker’s Algorithm
The Banker’s Algorithm is a resource allocation and deadlock avoidance
algorithm that tests for safety by simulating the allocation for the
predetermined maximum possible amounts of all resources, then
makes an “s-state” check to test for possible activities, before deciding
whether allocation should be allowed to continue.
Resource Allocation Graph (RAG)
The graph contains vertices and edges.
Types of vertices in RAG:
In RAG vertices are two types-
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. It is also two types:
 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.
Resource Allocation Graph (RAG)
Resource Allocation Graph (RAG)
Types of Edges in RAG:
There are two types of edges in RAG –
1. Assignment Edge: If you already assign a resource to a process then
it is called Assignment edge. A directed edge from resource type Rj to
process Pi is denoted by Rj ->Pi, it signifies that an instance of resource
type Rj has been allocated to process Pi.
2. Requested Edge: It means in future the process might want
some resource to complete the execution, that is called requested edge.
A directed edge from process Pi to resource type Rj is denoted by Pi -
>Rj. It signifies that process Pi has requested an instance of resource
type Rj and is currently waiting for that resource.
Resource Allocation Graph (RAG)
Resource Allocation Graph (RAG)
Example 1 (Single instances RAG):
If there is a cycle in the
Resource Allocation Graph
and each resource in the
cycle provides only one
instance, then the
processes will be in
deadlock. For example, if
process P1 holds resource
R1, process P2 holds
resource R2 and process P1
is waiting for R2 and
process P2 is waiting for
R1, then process P1 and
process P2 will be in
Resource Allocation Graph (RAG)
Example 2 (Single instances RAG):
Here’s another example,
that shows Processes P1
and P2 acquiring
resources R1 and R2 while
process P3 is waiting to
acquire both resources. In
this example, there is no
deadlock because there is
no circular dependency.
So cycle in single-instance
resource type is the
sufficient condition for
deadlock.
Resource Allocation Graph (RAG)
Example 3 (Multi instances RAG):
From this 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,
we need to construct the
allocation matrix and
request matrix.
Resource Allocation Graph (RAG)
The total number of processes are three; P1, P2 & P3 and the total number of
resources are two; R1 & R2.
Allocation matrix –
For constructing the allocation matrix, just go to the resources and see to
which process it is allocated.
R1 is allocated to P1, therefore write 1 in allocation matrix and similarly, R2 is
allocated to P2 as well as P3 and for the remaining element just write 0.
Request matrix –
In order to find out the request matrix, you have to go to the process and see
the outgoing edges.
P1 is requesting resource R2, so write 1 in the matrix and similarly, P2
requesting R1 and for the remaining element write 0.
So now available resource is = (0, 0).
Resource Allocation Graph (RAG)
Process
Allocation Request
Resource Resource
R1 R2 R1 R2
P1 1 0 0 1
P2 0 1 1 0
P3 0 1 0 0
So, there is no deadlock in
this RAG. Even though there
is a cycle, still there is no
deadlock. Therefore in multi-
instance resource cycle is not
sufficient condition for
deadlock
Resource Allocation Graph (RAG)
This example is
the same as the
previous example
except that, the
process P3
requesting for
resource R1.
Example 4 (Multi instances
RAG):
Resource Allocation Graph (RAG)
Process
Allocation Request
Resource Resource
R1 R2 R1 R2
P1 1 0 0 1
P2 0 1 1 0
P3 0 1 1 0
So, 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.
Therefore, every cycle in a multi-instance resource type graph is not a
deadlock, if there has to be a deadlock, there has to be a cycle. So, in
case of RAG with multi-instance resource type, the cycle is a necessary
condition for deadlock, but not sufficient.
Banker’s Algorithm
The following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of
resource types.
Available: It is a 1-d array of size ‘m’ indicating the number of available
resources of each type.
Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max: It is a 2-d array of size ‘n×m’ that defines the maximum demand of each
process in a system.
Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource
type Rj.
Allocation: It is a 2-d array of size ‘n×m’ that defines the number of resources
of each type currently allocated to each process.
Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of
resource type Rj
Need: It is a 2-d array of size ‘n×m’ that indicates the remaining resource need
of each process.
Banker’s Algorithm
Banker’s algorithm consists of a Safety algorithm and a Resource request
algorithm.
Safety Algorithm
1) Let Work and Finish be vectors of
length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Resource request Algorithm
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error
condition, since the process has
exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait,
since the resources are not available.
3) Have the system pretend to have
allocated the requested resources to
process Pi by modifying the state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
Recovery from Deadlock
Process Termination
Abort all deadlocked processes
Abort one process at a time until the deadlock cycle is eliminated In which
order should we choose to abort?
 Priority of the process
 How long process has computed, and how much longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?
Resource Preemption
Selecting a victim – minimize cost
Rollback – return to some safe state, restart process for that state
Starvation – same process may always be picked as victim, include number
of rollback in cost factor
THANK
YOU
Srimita Coomar
srimita99@gmail.com

More Related Content

PPTX
Using Resource Allocation Graph, it can be easily detected whether system is ...
PPT
Os module 2 d
PPTX
UNIT-3 DEADLOCK PART-1 08-06-2022 (1).pptx
PPTX
Gp1242 007 oer ppt
PDF
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
PPTX
Deadlock Detection Algorithm
PPT
Chapter 7 - Deadlocks
Using Resource Allocation Graph, it can be easily detected whether system is ...
Os module 2 d
UNIT-3 DEADLOCK PART-1 08-06-2022 (1).pptx
Gp1242 007 oer ppt
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
Deadlock Detection Algorithm
Chapter 7 - Deadlocks

Similar to Deadlock and avoidance in Operating System.pptx (20)

PPTX
Module 3 Deadlocks.pptx
PPT
Deadlock
PPT
PPTX
6. Deadlock_1640227623705.pptx
PPT
Operating System
PDF
deadlocks for Engenerring for he purpose
PPTX
OSLec14&15(Deadlocksinopratingsystem).pptx
PPTX
protection and security in operating systems
PPTX
Chapter 4
PPTX
Unit III-deadlock (1).pptx operating systems deadlocks
PPT
PPT
Lecture 8 - Memory Management. It deals with memory management
PPTX
Deadlock
PDF
Deadlock_Operating system presentation .pdf
PPT
Mch7 deadlock
PPTX
OS Module-3 (2).pptx
PDF
Chapter 5(five).pdf
PPTX
Lecture 6- Deadlocks.pptx
PPT
Deadlock principles in operating systems
Module 3 Deadlocks.pptx
Deadlock
6. Deadlock_1640227623705.pptx
Operating System
deadlocks for Engenerring for he purpose
OSLec14&15(Deadlocksinopratingsystem).pptx
protection and security in operating systems
Chapter 4
Unit III-deadlock (1).pptx operating systems deadlocks
Lecture 8 - Memory Management. It deals with memory management
Deadlock
Deadlock_Operating system presentation .pdf
Mch7 deadlock
OS Module-3 (2).pptx
Chapter 5(five).pdf
Lecture 6- Deadlocks.pptx
Deadlock principles in operating systems
Ad

Recently uploaded (20)

PPTX
additive manufacturing of ss316l using mig welding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Lecture Notes Electrical Wiring System Components
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Project quality management in manufacturing
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Geodesy 1.pptx...............................................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
additive manufacturing of ss316l using mig welding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Sustainable Sites - Green Building Construction
CYBER-CRIMES AND SECURITY A guide to understanding
Lecture Notes Electrical Wiring System Components
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Structs to JSON How Go Powers REST APIs.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Project quality management in manufacturing
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Geodesy 1.pptx...............................................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Ad

Deadlock and avoidance in Operating System.pptx

  • 2. AGENDA  Introduction  Example Diagram  Conditions necessary for Deadlock  Mutual Exclusion  Hold & Wait  No Preemption  Circular Wait  Ending Notes 2
  • 3. Definition: A deadlock is a situation where a set of processes is blocked because each process is holding a resource and waiting for another resource acquired by some other process. Example: when two trains are coming toward each other on the same track and there is only one track, none of the trains can move once they are in front of each other. This is a practical example of deadlock. Introduction
  • 4. How does deadlock occur in the operating system Example Diagram
  • 5. Conditions necessary for Deadlock  Mutual Exclusion  Hold & Wait  No Preemption  Circular Wait
  • 6. Mutual Exclusion Mutual exclusion condition requires at least one resource must be in non-shareable mode, which means that only one process can use the resource at any given time. Example: Full screen process in our computer cannot be used by two apps simultaneously. Process 1 Resource 1 only used by
  • 7. Hold & Wait The hold and wait condition specifies that a process must be holding at least one resource while waiting for other processes to release resources that are currently held by other processes.
  • 8. No Preemption A resource cannot be taken from a process unless the process releases the resource. If preemption was allowed then deadlock would never occur because then no process would have been able to hold a resource for long amount of time. Example: If your speaker is running an audio and after some time you click on some other audio it starts playing but in case no preemption was allowed we would have to wait for the first audio to end and if in case it was on loop we will end up in a deadlock.
  • 9. Circular Wait A set of processes waiting for each other in circular form.
  • 10. Deadlock detection Deadlock detection is the process of finding out whether any process are stuck in loop or not. There are several algorithms like:  Resource Allocation Graph (RAG) A resource allocation graphs shows which resource is held by which process and which process is waiting for a resource of a specific kind.  Banker’s Algorithm The Banker’s Algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.
  • 11. Resource Allocation Graph (RAG) The graph contains vertices and edges. Types of vertices in RAG: In RAG vertices are two types- 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. It is also two types:  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.
  • 13. Resource Allocation Graph (RAG) Types of Edges in RAG: There are two types of edges in RAG – 1. Assignment Edge: If you already assign a resource to a process then it is called Assignment edge. A directed edge from resource type Rj to process Pi is denoted by Rj ->Pi, it signifies that an instance of resource type Rj has been allocated to process Pi. 2. Requested Edge: It means in future the process might want some resource to complete the execution, that is called requested edge. A directed edge from process Pi to resource type Rj is denoted by Pi - >Rj. It signifies that process Pi has requested an instance of resource type Rj and is currently waiting for that resource.
  • 15. Resource Allocation Graph (RAG) Example 1 (Single instances RAG): If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one instance, then the processes will be in deadlock. For example, if process P1 holds resource R1, process P2 holds resource R2 and process P1 is waiting for R2 and process P2 is waiting for R1, then process P1 and process P2 will be in
  • 16. Resource Allocation Graph (RAG) Example 2 (Single instances RAG): Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2 while process P3 is waiting to acquire both resources. In this example, there is no deadlock because there is no circular dependency. So cycle in single-instance resource type is the sufficient condition for deadlock.
  • 17. Resource Allocation Graph (RAG) Example 3 (Multi instances RAG): From this 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, we need to construct the allocation matrix and request matrix.
  • 18. Resource Allocation Graph (RAG) The total number of processes are three; P1, P2 & P3 and the total number of resources are two; R1 & R2. Allocation matrix – For constructing the allocation matrix, just go to the resources and see to which process it is allocated. R1 is allocated to P1, therefore write 1 in allocation matrix and similarly, R2 is allocated to P2 as well as P3 and for the remaining element just write 0. Request matrix – In order to find out the request matrix, you have to go to the process and see the outgoing edges. P1 is requesting resource R2, so write 1 in the matrix and similarly, P2 requesting R1 and for the remaining element write 0. So now available resource is = (0, 0).
  • 19. Resource Allocation Graph (RAG) Process Allocation Request Resource Resource R1 R2 R1 R2 P1 1 0 0 1 P2 0 1 1 0 P3 0 1 0 0 So, there is no deadlock in this RAG. Even though there is a cycle, still there is no deadlock. Therefore in multi- instance resource cycle is not sufficient condition for deadlock
  • 20. Resource Allocation Graph (RAG) This example is the same as the previous example except that, the process P3 requesting for resource R1. Example 4 (Multi instances RAG):
  • 21. Resource Allocation Graph (RAG) Process Allocation Request Resource Resource R1 R2 R1 R2 P1 1 0 0 1 P2 0 1 1 0 P3 0 1 1 0 So, 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. Therefore, every cycle in a multi-instance resource type graph is not a deadlock, if there has to be a deadlock, there has to be a cycle. So, in case of RAG with multi-instance resource type, the cycle is a necessary condition for deadlock, but not sufficient.
  • 22. Banker’s Algorithm The following Data structures are used to implement the Banker’s Algorithm: Let ‘n’ be the number of processes in the system and ‘m’ be the number of resource types. Available: It is a 1-d array of size ‘m’ indicating the number of available resources of each type. Available[ j ] = k means there are ‘k’ instances of resource type Rj Max: It is a 2-d array of size ‘n×m’ that defines the maximum demand of each process in a system. Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj. Allocation: It is a 2-d array of size ‘n×m’ that defines the number of resources of each type currently allocated to each process. Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj Need: It is a 2-d array of size ‘n×m’ that indicates the remaining resource need of each process.
  • 23. Banker’s Algorithm Banker’s algorithm consists of a Safety algorithm and a Resource request algorithm. Safety Algorithm 1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work = Available Finish[i] = false; for i=1, 2, 3, 4….n 2) Find an i such that both a) Finish[i] = false b) Needi <= Work if no such i exists goto step (4) 3) Work = Work + Allocation[i] Finish[i] = true goto step (2) 4) if Finish [i] = true for all i then the system is in a safe state Resource request Algorithm 1) If Requesti <= Needi Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim. 2) If Requesti <= Available Goto step (3); otherwise, Pi must wait, since the resources are not available. 3) Have the system pretend to have allocated the requested resources to process Pi by modifying the state as follows: Available = Available – Requesti Allocationi = Allocationi + Requesti Needi = Needi– Requesti
  • 24. Recovery from Deadlock Process Termination Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated In which order should we choose to abort?  Priority of the process  How long process has computed, and how much longer to completion  Resources the process has used  Resources process needs to complete  How many processes will need to be terminated  Is process interactive or batch? Resource Preemption Selecting a victim – minimize cost Rollback – return to some safe state, restart process for that state Starvation – same process may always be picked as victim, include number of rollback in cost factor