SlideShare a Scribd company logo
1 of 29
Deadlock Prevention, Avoidance,
and Detection
The Deadlock problem
 In a computer system deadlocks arise when
members of a group of processes which
hold resources are blocked indefinitely
from access to resources held by other
processes within the group.
Deadlock example
 Pi requests one I/O controller and the
system allocates one.
 Pj requests one I/O controller and again the
system allocates one.
 Pi wants another I/O controller but has to
wait since the system ran out of I/O
controllers.
 Pj wants another I/O controller and waits.
Conditions for deadlocks
 Mutual exclusion. No resource can be shared by
more than one process at a time.
 Hold and wait. There must exist a process that is
holding at least one resource and is waiting to
acquire additional resources that are currently
being held by other processes.
 No preemption. A resource cannot be preempted.
 Circular wait. There is a cycle in the wait-for
graph.
An example
City A City B
bridge
City A City B
river river
bridge
Graph-theoretic models
 Wait-for graph.
 Resource-allocation graph.
Wait-for graph
P1
P4
P5
P2
P3
Resource allocation graph
P1 P2
P3
r1 r2
Resource allocation graph
Without deadlock
P1 P2
P3
With deadlock
Wait-for graph and Resource-
allocation graph conversion
 Any resource allocation graph with a single
copy of resources can be transferred to a
wait-for graph.
P1
P2
P3
P1
P2
P3
Strategies for handling
deadlocks
 Deadlock prevention. Prevents deadlocks by
restraining requests made to ensure that at least
one of the four deadlock conditions cannot occur.
 Deadlock avoidance. Dynamically grants a
resource to a process if the resulting state is safe.
A state is safe if there is at least one execution
sequence that allows all processes to run to
completion.
 Deadlock detection and recovery. Allows
deadlocks to form; then finds and breaks them.
Two types of deadlocks
 Resource deadlock: uses AND condition.
AND condition: a process that requires resources
for execution can proceed when it has acquired
all those resources.
 Communication deadlock: uses OR condition.
OR condition: a process that requires resources
for execution can proceed when it has acquired at
least one of those resources.
 P-out-of –Q condition which means that a
process simultaneously requests Q
resources and remains blocked until it is
granted any P of those resources.
 AND-OR model, which may specify any
combination of AND and OR models.
E.g. a AND (b OR c).
Deadlock conditions
 The condition for deadlock in a system using the
AND condition is the existence of a cycle.
 The condition for deadlock in a system using the
OR condition is the existence of a knot.
A knot (K) consists of a set of nodes such that for
every node a in K, all nodes in K and only the
nodes in K are reachable from node a.
Example: OR condition
P1 P2
P3
P4
P5
P1 P2
P3
P4
P5
No deadlock Deadlock
Deadlock Prevention
 1. A process acquires all the needed resources
simultaneously before it begins its execution,
therefore breaking the hold and wait condition.
 E.g. In the dining philosophers’ problem, each
philosopher is required to pick up both forks at
the same time. If he fails, he has to release the
fork(s) (if any) he has acquired.
 Drawback: over-cautious.
 2. All resources are assigned unique numbers. A process
may request a resource with a unique number I only if it is
not holding a resource with a number less than or equal to
I and therefore breaking the circular wait condition.
 E.g. In the dining philosophers problem, each philosopher
is required to pick a fork that has a larger id than the one
he currently holds. That is, philosopher P5 needs to pick
up fork F5 and then F1; the other philosopher Pi should
pick up fork Fi followed by Fi-1.
 Drawback: over-cautions.
 3. Each process is assigned a unique priority number. The
priority numbers decide whether process Pi should wait
for process Pj and therefore break the non-preemption
condition.
 E.g. Assume that the philosophers’ priorities are based on
their ids, i.e., Pi has a higher priority than Pj if i <j. In this
case Pi is allowed to wait for Pi+1 for I=1,2,3,4. P5 is not
allowed to wait for P1. If this case happens, P5 has to
abort by releasing its acquired fork(s) (if any).
 Drawback: starvation. The lower priority one may always
be rolled back. Solution is to raise the priority every time
it is victimized.
 4. Practically it is impossible to provide a
method to break the mutual exclusion
condition since most resources are
intrinsically non-sharable, e.g., two
philosophers cannot use the same fork at
the same time.
A Deadlock Prevention Example
Wait-die
 Wants Resource Hold Resource
 Old process ----- Young process
 10 20
 Waits
 Wants resource Holds resource
 Young process 20 Old process 10
 Dies
 Wait-die is a non-preemptive method.
 Wound-wait
 Wants resource Hold resource
 Old process 10 Young process 20
 Preempts
 Wants resource Hold resource
 Young process 20 Old process 10
 Waits
An example
Process id priority 1st request
time
length Retry
interval
P1 2 1 1 1
P2 1 1.5 2 1
P3 4 2.1 2 2
P4 5 3.3 1 1
P5 3 4.0 2 3
Deadlock Avoidance
Four resources ABCD. A has 6 instances, B has 3 instances, C
Has 4 instances and D has 2 instances.
Process Allocation Max
ABCD ABCD
P1 3011 4111
P2 0100 0212
P3 1110 4210
P4 1101 1101
P5 0000 2110
Is the current state safe?
If P5 requests for (1,0,1,0), can this be granted?
Deadlock Detection and
Recovery
 Centralized approaches
 Distributed approaches
 Hierarchical approaches
Centralized approaches
Machine 0 Machine 1 Coordinator Coordinator
A S
R
B
Holds
Wants
Holds
C
T
S
Wants
Holds
C
T
S
A
R
B
C
T
S
A
R
B
B releases R and then B wants T.
But B wants T reaches coordinator first
and results in false deadlock.
Distributed approaches
 A copy of the global wait-for graph is kept
at each site with the result that each site has
a global view of the system.
 The global wait-for graph is divided and
distributed to different sites.
Chandy-Misra-Haas distributed
deadlock detection algorithm
0 1 2 3
4
5
6
7
8
Machine 0 Machine 1 Machine 2
(0,8,0)
(0,2,3)
(0,4,6)
(0,5,7)
Hierarchical approaches
 In hierarchical deadlock detection algorithms, sites are arranged
hierarchically in a tree. A site detects deadlocks involving only its descendant
sites.
 For example, let A, B and C be controllers such that C is the lowest common
ancestor of A and B. Suppose that node Pi appears in the local wait-for graph
of controllers A and B. Then Pi must also appear in the local wait-for graph
as:

  Controller of C.
  Every controller in the path from C to A.
  Every controller in the path from C to B.

 In addition, if Pi and Pj appear in the wait-for graph of controller D and there
exists a path from Pi to Pj in the wait-for graph of one of the children of D,
then an edge (Pi, Pj) must be in the wait-for graph of D.

More Related Content

Similar to Deadlocks.ppt

Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
Sonali Chauhan
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
C.U
 
Lecture5
Lecture5Lecture5
Lecture5
jntu
 

Similar to Deadlocks.ppt (20)

Ice
IceIce
Ice
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
7 web
7 web7 web
7 web
 
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
 
OS_Ch8
OS_Ch8OS_Ch8
OS_Ch8
 
OSCh8
OSCh8OSCh8
OSCh8
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
Deadlock detection & prevention
Deadlock detection & preventionDeadlock detection & prevention
Deadlock detection & prevention
 
Deadlock
DeadlockDeadlock
Deadlock
 
Chapter 5(five).pdf
Chapter 5(five).pdfChapter 5(five).pdf
Chapter 5(five).pdf
 
Sucet os module_3_notes
Sucet os module_3_notesSucet os module_3_notes
Sucet os module_3_notes
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
 
deadlock in OS.pptx
deadlock in OS.pptxdeadlock in OS.pptx
deadlock in OS.pptx
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
 
Lecture5
Lecture5Lecture5
Lecture5
 
Deadlock in Distributed Systems
Deadlock in Distributed SystemsDeadlock in Distributed Systems
Deadlock in Distributed Systems
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks prefinal
Deadlocks prefinalDeadlocks prefinal
Deadlocks prefinal
 
Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456
 
Bankers
BankersBankers
Bankers
 

More from jamilmalik19 (11)

Evolution of Database Technology.pptx
Evolution of Database Technology.pptxEvolution of Database Technology.pptx
Evolution of Database Technology.pptx
 
Evolution of Database Technology.pptx
Evolution of Database Technology.pptxEvolution of Database Technology.pptx
Evolution of Database Technology.pptx
 
CD Store Management.pptx
CD Store Management.pptxCD Store Management.pptx
CD Store Management.pptx
 
bankers-algorithm2.pptx
bankers-algorithm2.pptxbankers-algorithm2.pptx
bankers-algorithm2.pptx
 
Exception Handling ,templates in C++
Exception Handling ,templates in C++Exception Handling ,templates in C++
Exception Handling ,templates in C++
 
Introduction to DSA
Introduction to DSAIntroduction to DSA
Introduction to DSA
 
Wildcard In database
Wildcard In databaseWildcard In database
Wildcard In database
 
Angular momentum
Angular momentumAngular momentum
Angular momentum
 
What is IDE in C++?
What is IDE in C++?What is IDE in C++?
What is IDE in C++?
 
introduction to C++.pptx
introduction to C++.pptxintroduction to C++.pptx
introduction to C++.pptx
 
Data Types and variables in C++.pptx
Data Types and variables in C++.pptxData Types and variables in C++.pptx
Data Types and variables in C++.pptx
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Deadlocks.ppt

  • 2. The Deadlock problem  In a computer system deadlocks arise when members of a group of processes which hold resources are blocked indefinitely from access to resources held by other processes within the group.
  • 3. Deadlock example  Pi requests one I/O controller and the system allocates one.  Pj requests one I/O controller and again the system allocates one.  Pi wants another I/O controller but has to wait since the system ran out of I/O controllers.  Pj wants another I/O controller and waits.
  • 4. Conditions for deadlocks  Mutual exclusion. No resource can be shared by more than one process at a time.  Hold and wait. There must exist a process that is holding at least one resource and is waiting to acquire additional resources that are currently being held by other processes.  No preemption. A resource cannot be preempted.  Circular wait. There is a cycle in the wait-for graph.
  • 5. An example City A City B bridge City A City B river river bridge
  • 6. Graph-theoretic models  Wait-for graph.  Resource-allocation graph.
  • 8. Resource allocation graph P1 P2 P3 r1 r2 Resource allocation graph Without deadlock P1 P2 P3 With deadlock
  • 9. Wait-for graph and Resource- allocation graph conversion  Any resource allocation graph with a single copy of resources can be transferred to a wait-for graph. P1 P2 P3 P1 P2 P3
  • 10. Strategies for handling deadlocks  Deadlock prevention. Prevents deadlocks by restraining requests made to ensure that at least one of the four deadlock conditions cannot occur.  Deadlock avoidance. Dynamically grants a resource to a process if the resulting state is safe. A state is safe if there is at least one execution sequence that allows all processes to run to completion.  Deadlock detection and recovery. Allows deadlocks to form; then finds and breaks them.
  • 11. Two types of deadlocks  Resource deadlock: uses AND condition. AND condition: a process that requires resources for execution can proceed when it has acquired all those resources.  Communication deadlock: uses OR condition. OR condition: a process that requires resources for execution can proceed when it has acquired at least one of those resources.
  • 12.  P-out-of –Q condition which means that a process simultaneously requests Q resources and remains blocked until it is granted any P of those resources.  AND-OR model, which may specify any combination of AND and OR models. E.g. a AND (b OR c).
  • 13. Deadlock conditions  The condition for deadlock in a system using the AND condition is the existence of a cycle.  The condition for deadlock in a system using the OR condition is the existence of a knot. A knot (K) consists of a set of nodes such that for every node a in K, all nodes in K and only the nodes in K are reachable from node a.
  • 14. Example: OR condition P1 P2 P3 P4 P5 P1 P2 P3 P4 P5 No deadlock Deadlock
  • 15. Deadlock Prevention  1. A process acquires all the needed resources simultaneously before it begins its execution, therefore breaking the hold and wait condition.  E.g. In the dining philosophers’ problem, each philosopher is required to pick up both forks at the same time. If he fails, he has to release the fork(s) (if any) he has acquired.  Drawback: over-cautious.
  • 16.  2. All resources are assigned unique numbers. A process may request a resource with a unique number I only if it is not holding a resource with a number less than or equal to I and therefore breaking the circular wait condition.  E.g. In the dining philosophers problem, each philosopher is required to pick a fork that has a larger id than the one he currently holds. That is, philosopher P5 needs to pick up fork F5 and then F1; the other philosopher Pi should pick up fork Fi followed by Fi-1.  Drawback: over-cautions.
  • 17.  3. Each process is assigned a unique priority number. The priority numbers decide whether process Pi should wait for process Pj and therefore break the non-preemption condition.  E.g. Assume that the philosophers’ priorities are based on their ids, i.e., Pi has a higher priority than Pj if i <j. In this case Pi is allowed to wait for Pi+1 for I=1,2,3,4. P5 is not allowed to wait for P1. If this case happens, P5 has to abort by releasing its acquired fork(s) (if any).  Drawback: starvation. The lower priority one may always be rolled back. Solution is to raise the priority every time it is victimized.
  • 18.  4. Practically it is impossible to provide a method to break the mutual exclusion condition since most resources are intrinsically non-sharable, e.g., two philosophers cannot use the same fork at the same time.
  • 19. A Deadlock Prevention Example Wait-die  Wants Resource Hold Resource  Old process ----- Young process  10 20  Waits
  • 20.  Wants resource Holds resource  Young process 20 Old process 10  Dies  Wait-die is a non-preemptive method.
  • 21.  Wound-wait  Wants resource Hold resource  Old process 10 Young process 20  Preempts
  • 22.  Wants resource Hold resource  Young process 20 Old process 10  Waits
  • 23. An example Process id priority 1st request time length Retry interval P1 2 1 1 1 P2 1 1.5 2 1 P3 4 2.1 2 2 P4 5 3.3 1 1 P5 3 4.0 2 3
  • 24. Deadlock Avoidance Four resources ABCD. A has 6 instances, B has 3 instances, C Has 4 instances and D has 2 instances. Process Allocation Max ABCD ABCD P1 3011 4111 P2 0100 0212 P3 1110 4210 P4 1101 1101 P5 0000 2110 Is the current state safe? If P5 requests for (1,0,1,0), can this be granted?
  • 25. Deadlock Detection and Recovery  Centralized approaches  Distributed approaches  Hierarchical approaches
  • 26. Centralized approaches Machine 0 Machine 1 Coordinator Coordinator A S R B Holds Wants Holds C T S Wants Holds C T S A R B C T S A R B B releases R and then B wants T. But B wants T reaches coordinator first and results in false deadlock.
  • 27. Distributed approaches  A copy of the global wait-for graph is kept at each site with the result that each site has a global view of the system.  The global wait-for graph is divided and distributed to different sites.
  • 28. Chandy-Misra-Haas distributed deadlock detection algorithm 0 1 2 3 4 5 6 7 8 Machine 0 Machine 1 Machine 2 (0,8,0) (0,2,3) (0,4,6) (0,5,7)
  • 29. Hierarchical approaches  In hierarchical deadlock detection algorithms, sites are arranged hierarchically in a tree. A site detects deadlocks involving only its descendant sites.  For example, let A, B and C be controllers such that C is the lowest common ancestor of A and B. Suppose that node Pi appears in the local wait-for graph of controllers A and B. Then Pi must also appear in the local wait-for graph as:    Controller of C.   Every controller in the path from C to A.   Every controller in the path from C to B.   In addition, if Pi and Pj appear in the wait-for graph of controller D and there exists a path from Pi to Pj in the wait-for graph of one of the children of D, then an edge (Pi, Pj) must be in the wait-for graph of D.