SlideShare a Scribd company logo
4
Most read
5
Most read
13
Most read
Deadlock Detection Algorithm
Introduction
What are Deadlocks ? ‘ Any blocked process which cannot be
resolved unless there is some outside intervention’.
In operating system deadlocks can be visualized where processes
have some resources held by them and are waiting for some resources
to fulfill their completion which are instead being held by some other
blocked process.
A very simple real-world example is of the two cars halted on
both sides of a bridge which has only width for one car to pass. As both
cars are holding each end of the bridge so neither can pass unless and
until one car backs up and clears the road so that the other car pass and
vice versa.
Deadlock Classification
Mutual Exclusion : If one process is holding a resource required by other
processes that resource must wait until it is released by the process.
Hold and Wait : Processes are allowed to hold one or more resources and
waiting for additional resources held by other processes.
No Pre-Emption : Resources are released voluntarily, neither another process
nor the OS can force a process to release a resource.
Circular Wait : There may exist a set of waiting processes such that P0 is
waiting for a 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
We can model deadlock conditions using a directed graph called a Resource Allocation Graph (RAG).
Lets understand some basics:
Two Kinds of Nodes :
Boxes : Represent Resources
Circles : Represent Processes
Two Kinds of (Directed) Edges:
Request Edge : from thread to resource, indicates that the thread has requested the resource and is
waiting to acquire it.
Assignment Edge : from resource instance to thread, indicates the thread is holding the resource
instance.
When a request is made, a request edge is added
On fulfillment the request edge is transformed into and assignment edge.
When a resource is released by a process, the assignment edge is deleted.
RAG with Single Resource Instance
R1 R2 R1 R2
P1 P2 P3
R3 R4
P1 P2 P3
R3 R4
A closed cycle in RAG with single resource instance is mandatory for deadlock.
RAG with Multiple Resource Instances
R1 R2 R1 R2
P1 P2 P3
R3 R4
P1 P2 P3
R3 R4
A Knot is mandatory for a deadlock. (Knot is created when strongly connected
subgraphs with no outgoing edges are formed).
Prevention and Avoidance
Prevention by eliminating one of the four conditions
Acquire all resources before proceeding (No wait while Hold)
Allow Preemption (Eliminate 3d Condition)
Prioritize Processes and Assign Resources in order of Priorities (No Circular
Wait)
Avoidance can be done by allowing resource requests only if they don’t lead to
deadlock condition. In other words, Safe state : An order to entertain resource
requests so that all processes can be completed.
Drawbacks:
Resource requirements for all processes must be known in advance.
Resource request set is known and fixed,
Complex Analysis for every request.
Deadlock Detection
Detection :
Issues
Maintenance of WFG (Wait for Graphs)
Search of WFG for deadlocks
Requirements
Progress No undetected deadlocks
Safety No false(phantom) deadlocks
Resolution
Roll Back one or more processes to break dependencies in WFG to resolve the
situation.
Single Instance for Each
Resource Type
Maintain wait-for Graph
Nodes are processes
Pi-> Pj if Pi is waiting for Pj
Periodically invoke an algorithm that searches for a cycle in the graph. If
there is a cycle then there is or will be a deadlock.
An algorithm to detect a cycle in graph requires an order of n2
Operations, where n is the number of vertices in the graph.
RAG& WFG
R1 R4
P1 P2 P3
P5
R3
P4 R5
R2
P1 P2 P3
P5
P4
Corresponding Wait for Graph
Resource Allocation Graph
Several Instances of a Resource
Type
Available : A vector of length m indicates the number of available
resources of each type.
Allocation: An n x m matrix defines the number of resources of each type
currently allocated to each process.
Request: An n x m matrix indicates the current request of each process.
If Request[ij]=k, then process Pi is requesting k more instances of
resource type Rj.
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize :
(a) Work=Available
(b) For i=1,2,…,n if Allocationi ≠ 0, then
Finish[i]=false; otherwise, Finish[i]=true.
2. Find an index i such that both:
(a) Finish[i]==false
(b) Requesti≤ Work
3. Work = Work +Allocationi
Finish[i]=true
go to step 2.
4. If Finish[i]==false, for some i, 1 ≤i ≤n, then the system is in the deadlock
state. Moreover, if Finish[i]==false, then Piis deadlocked.
Algorithm requires an order of O(m x n2) operations to detect whether the system is in
deadlocked state.
Example of Detection Algorithm
Five processes Po through P4; three resource types A(7 instances), B(2
instances), and C(6 instances).
Snapshot at time T0:
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
Sequence <P0,P1,P2,P3,P4>will result in Finish[i]=true for all i.
P2 request an additional instance of type C.
Request
A B C
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
State of System?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests.
Deadlock exists, consisting of processes P1,P2,P3 and P4.
Detection-Algorithm Usage
When, and how often, to invoke depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
One for each disjoint cycle.
If detection algorithm is invoked arbitrarily, there may be many cycles in
the resource graph and so we would not be able to tell which of the
many deadlocked processes “caused” the deadlock.
Thank You

More Related Content

PPTX
Paging and segmentation
PPTX
Operating system paging and segmentation
PDF
Deadlock Avoidance - OS
PPTX
Deadlock Prevention
PPTX
Dead Lock in operating system
PPTX
Lecture 5 inter process communication
PPT
Contiguous Memory Allocation.ppt
PPTX
INTER PROCESS COMMUNICATION (IPC).pptx
Paging and segmentation
Operating system paging and segmentation
Deadlock Avoidance - OS
Deadlock Prevention
Dead Lock in operating system
Lecture 5 inter process communication
Contiguous Memory Allocation.ppt
INTER PROCESS COMMUNICATION (IPC).pptx

What's hot (20)

PPT
Chapter 7 - Deadlocks
PPT
Memory Management in OS
PPTX
SCHEDULING ALGORITHMS
PPTX
Inter Process Communication
PPTX
Memory management ppt
PPTX
Swapping | Computer Science
PPTX
Types of Addressing modes- COA
PPTX
Computer architecture memory system
PPTX
Deadlock- Operating System
PPTX
Register transfer language
PPT
Classical problem of synchronization
PDF
Deadlock
PPT
Inter-Process communication in Operating System.ppt
PPTX
Bus aribration
PPT
12 process control blocks
PDF
Operating System-Process Scheduling
PPT
Memory hierarchy
PPTX
memory hierarchy
PPTX
Register transfer language
PPTX
MULTILEVEL QUEUE SCHEDULING
Chapter 7 - Deadlocks
Memory Management in OS
SCHEDULING ALGORITHMS
Inter Process Communication
Memory management ppt
Swapping | Computer Science
Types of Addressing modes- COA
Computer architecture memory system
Deadlock- Operating System
Register transfer language
Classical problem of synchronization
Deadlock
Inter-Process communication in Operating System.ppt
Bus aribration
12 process control blocks
Operating System-Process Scheduling
Memory hierarchy
memory hierarchy
Register transfer language
MULTILEVEL QUEUE SCHEDULING
Ad

Similar to Deadlock Detection Algorithm (20)

PPTX
Ch 4 deadlock
PPTX
Gp1242 007 oer ppt
PDF
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
PPTX
Deadlock - An Operating System Concept.pptx
PPTX
UNIT-3 DEADLOCK PART-1 08-06-2022 (1).pptx
PPTX
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
PPTX
Deadlock
PPTX
Algorithm 4Chapter Four- Deadlock (5).pptx
PPTX
Deadlock and avoidance in Operating System.pptx
PPTX
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
PPTX
Module 3 Deadlocks.pptx
PDF
9 deadlock
PPTX
OS Module-3 (2).pptx
PPTX
Deadlock
PPTX
OS Presentation 1 (1).pptx
PPT
Operating System
PPTX
Os presentation final.pptxjjjjjdakajwsjjdhdfjff
PPT
A ppt on deadlock in operating systems for the better explanation
PPT
14th November - Deadlock Prevention, Avoidance.ppt
PPT
Section07-Deadlocks_operating_system.ppt
Ch 4 deadlock
Gp1242 007 oer ppt
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
Deadlock - An Operating System Concept.pptx
UNIT-3 DEADLOCK PART-1 08-06-2022 (1).pptx
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Deadlock
Algorithm 4Chapter Four- Deadlock (5).pptx
Deadlock and avoidance in Operating System.pptx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
Module 3 Deadlocks.pptx
9 deadlock
OS Module-3 (2).pptx
Deadlock
OS Presentation 1 (1).pptx
Operating System
Os presentation final.pptxjjjjjdakajwsjjdhdfjff
A ppt on deadlock in operating systems for the better explanation
14th November - Deadlock Prevention, Avoidance.ppt
Section07-Deadlocks_operating_system.ppt
Ad

More from Mohammad Qureshi (15)

PPTX
Presentation.pptx
PDF
Technical specification software fiscal device 1.1 pra
PPTX
Presentation on Eductaion for BSEd
DOCX
Resignation
PDF
Ptcl Modem User Manual
PDF
PPS
Qacojone The mystery
PPT
Final project(vb)
PPS
Installing (oracle8.05)
PPS
Installing (deveoper6i)
PDF
Situation2
PDF
Final Project Master In Computer Sciences
PDF
Muhammad Nasir Qureshi
PPTX
Supply Chain Management System
PPS
Maple Leaf Cement Factory Ltd, Iskendarabad, Distt: Mianwali
Presentation.pptx
Technical specification software fiscal device 1.1 pra
Presentation on Eductaion for BSEd
Resignation
Ptcl Modem User Manual
Qacojone The mystery
Final project(vb)
Installing (oracle8.05)
Installing (deveoper6i)
Situation2
Final Project Master In Computer Sciences
Muhammad Nasir Qureshi
Supply Chain Management System
Maple Leaf Cement Factory Ltd, Iskendarabad, Distt: Mianwali

Recently uploaded (20)

PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
NewMind AI Monthly Chronicles - July 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Big Data Technologies - Introduction.pptx
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
cuic standard and advanced reporting.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Transforming Manufacturing operations through Intelligent Integrations
NewMind AI Monthly Chronicles - July 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Per capita expenditure prediction using model stacking based on satellite ima...
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Chapter 3 Spatial Domain Image Processing.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
CIFDAQ's Market Insight: SEC Turns Pro Crypto
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
cuic standard and advanced reporting.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

Deadlock Detection Algorithm

  • 2. Introduction What are Deadlocks ? ‘ Any blocked process which cannot be resolved unless there is some outside intervention’. In operating system deadlocks can be visualized where processes have some resources held by them and are waiting for some resources to fulfill their completion which are instead being held by some other blocked process. A very simple real-world example is of the two cars halted on both sides of a bridge which has only width for one car to pass. As both cars are holding each end of the bridge so neither can pass unless and until one car backs up and clears the road so that the other car pass and vice versa.
  • 3. Deadlock Classification Mutual Exclusion : If one process is holding a resource required by other processes that resource must wait until it is released by the process. Hold and Wait : Processes are allowed to hold one or more resources and waiting for additional resources held by other processes. No Pre-Emption : Resources are released voluntarily, neither another process nor the OS can force a process to release a resource. Circular Wait : There may exist a set of waiting processes such that P0 is waiting for a 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.
  • 4. Resource Allocation Graph We can model deadlock conditions using a directed graph called a Resource Allocation Graph (RAG). Lets understand some basics: Two Kinds of Nodes : Boxes : Represent Resources Circles : Represent Processes Two Kinds of (Directed) Edges: Request Edge : from thread to resource, indicates that the thread has requested the resource and is waiting to acquire it. Assignment Edge : from resource instance to thread, indicates the thread is holding the resource instance. When a request is made, a request edge is added On fulfillment the request edge is transformed into and assignment edge. When a resource is released by a process, the assignment edge is deleted.
  • 5. RAG with Single Resource Instance R1 R2 R1 R2 P1 P2 P3 R3 R4 P1 P2 P3 R3 R4 A closed cycle in RAG with single resource instance is mandatory for deadlock.
  • 6. RAG with Multiple Resource Instances R1 R2 R1 R2 P1 P2 P3 R3 R4 P1 P2 P3 R3 R4 A Knot is mandatory for a deadlock. (Knot is created when strongly connected subgraphs with no outgoing edges are formed).
  • 7. Prevention and Avoidance Prevention by eliminating one of the four conditions Acquire all resources before proceeding (No wait while Hold) Allow Preemption (Eliminate 3d Condition) Prioritize Processes and Assign Resources in order of Priorities (No Circular Wait) Avoidance can be done by allowing resource requests only if they don’t lead to deadlock condition. In other words, Safe state : An order to entertain resource requests so that all processes can be completed. Drawbacks: Resource requirements for all processes must be known in advance. Resource request set is known and fixed, Complex Analysis for every request.
  • 8. Deadlock Detection Detection : Issues Maintenance of WFG (Wait for Graphs) Search of WFG for deadlocks Requirements Progress No undetected deadlocks Safety No false(phantom) deadlocks Resolution Roll Back one or more processes to break dependencies in WFG to resolve the situation.
  • 9. Single Instance for Each Resource Type Maintain wait-for Graph Nodes are processes Pi-> Pj if Pi is waiting for Pj Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle then there is or will be a deadlock. An algorithm to detect a cycle in graph requires an order of n2 Operations, where n is the number of vertices in the graph.
  • 10. RAG& WFG R1 R4 P1 P2 P3 P5 R3 P4 R5 R2 P1 P2 P3 P5 P4 Corresponding Wait for Graph Resource Allocation Graph
  • 11. Several Instances of a Resource Type Available : A vector of length m indicates the number of available resources of each type. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. Request: An n x m matrix indicates the current request of each process. If Request[ij]=k, then process Pi is requesting k more instances of resource type Rj.
  • 12. Detection Algorithm 1. Let Work and Finish be vectors of length m and n, respectively Initialize : (a) Work=Available (b) For i=1,2,…,n if Allocationi ≠ 0, then Finish[i]=false; otherwise, Finish[i]=true. 2. Find an index i such that both: (a) Finish[i]==false (b) Requesti≤ Work 3. Work = Work +Allocationi Finish[i]=true go to step 2. 4. If Finish[i]==false, for some i, 1 ≤i ≤n, then the system is in the deadlock state. Moreover, if Finish[i]==false, then Piis deadlocked. Algorithm requires an order of O(m x n2) operations to detect whether the system is in deadlocked state.
  • 13. Example of Detection Algorithm Five processes Po through P4; three resource types A(7 instances), B(2 instances), and C(6 instances). Snapshot at time T0: 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 Sequence <P0,P1,P2,P3,P4>will result in Finish[i]=true for all i.
  • 14. P2 request an additional instance of type C. Request A B C P0 0 0 0 P1 2 0 1 P2 0 0 1 P3 1 0 0 P4 0 0 2 State of System? Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests. Deadlock exists, consisting of processes P1,P2,P3 and P4.
  • 15. Detection-Algorithm Usage When, and how often, to invoke depends on: How often a deadlock is likely to occur? How many processes will need to be rolled back? One for each disjoint cycle. If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.