SlideShare a Scribd company logo
1 of 53
OSes: 6. Deadlock 1
Operating Systems
 Objectives
– describe deadlock, and forms of prevention,
avoidance, detection, and recovery
Certificate Program in Software Development
CSE-TC and CSIM, AIT
September -- November, 2003
6. Deadlock
(Ch. 7, S&G)
ch 8 in the 6th ed.
OSes: 6. Deadlock 2
Contents
1. What is Deadlock?
2. Dealing with Deadlock
3. Deadlock Prevention
4. Deadlock Avoidance
5. Deadlock Detection
6. Deadlock Recovery
OSes: 6. Deadlock 3
1. What is Deadlock?
 An example from US Kansas law:
– “When two trains approach each other at a
crossing, both shall come to a full stop and
neither shall start up again until the other has
gone.”
OSes: 6. Deadlock 4
In Picture Form: VUW CS 305
Neither truck can proceed.
OSes: 6. Deadlock 5
1.1. System Deadlock
 A process must request a resource before
using it, and must release the resource after
finishing with it.
 A set of processes is in a deadlock state
when every process in the set is waiting for
a resource that can only be released by
another process in the set.
OSes: 6. Deadlock 6
1.2. Necessary Conditions for Deadlock
 Mutual Exclusion
– at least one resource must be held in
non-shareable mode
 Hold and Wait
– a process is holding a resource and waiting
for others
continued
OSes: 6. Deadlock 7
 No Preemption
– only the process can release its held resource
 Circular Wait
– {P0, P1, …, Pn}
– Pi is waiting for the resource held by Pi+1;
Pn is waiting for the resource held by P0
OSes: 6. Deadlock 8
1.3. Resource Allocation Graph
 A set of processes {P0, P1, …}
 A set of resource types {R1, R2, …},
together with instances of those types.
Pj
Rk
OSes: 6. Deadlock 9
Edge Notation
 Pi  Rj
– process i has requested an instance of resource j
– called a request edge
 Rj  Pi
– an instance of resource j has been assigned to
process i
– called an assignment edge
OSes: 6. Deadlock 10
Example Graph Fig. 7.1, p.211
R1
P1 P2 P3
R3
R2
R4
OSes: 6. Deadlock 11
Finding a Deadlock
 If the graph has no cycles then there are no
deadlock processes.
 If there is a cycle, then there may be a
deadlock.
OSes: 6. Deadlock 12
Graph with a Deadlock Fig. 7.2, p.212
R1
P1 P2 P3
R3
R2
R4
OSes: 6. Deadlock 13
Graph without a Deadlock
Fig. 7.3, p.213
R1
P1
P2
P3
R2
P4
OSes: 6. Deadlock 14
2. Deadling with Deadlocks
 Stop a deadlock ever occuring
– deadlock prevention
 disallow at least one of the necessary conditions
– deadlock avoidance
 see a deadlock coming and alter the
process/resource allocation strategy
continued
OSes: 6. Deadlock 15
 Deadlock detection and recovery
 Ignore the problem
– done by most OSes, including UNIX
– cheap solution
– infrequent, manual reboots may be acceptable
OSes: 6. Deadlock 16
3. Deadlock Prevention
 Eliminate one (or more) of:
– mutual exclusion
– hold and wait
– no preemption (i.e. have preemption)
– circular wait
OSes: 6. Deadlock 17
3.1. Eliminate Mutual Exclusion
 Shared resources do not require mutual
exclusion
– e.g. read-only files
 But some resources cannot be shared (at the
same time)
– e.g. printers
OSes: 6. Deadlock 18
3.2. Eliminate Hold & Wait
 One approach requires that each process be
allocated all of its resources before it begins
executing
– eliminates the wait possibility
 Alternatively, only allow a process to
request resources when it currently has none
– eliminates the hold possibility
OSes: 6. Deadlock 19
3.3. Eliminate “No Preemption”
 Make a process automatically release its
current resources if it cannot obtain all the
ones it wants
– restart the process when it can obtain everything
 Alternatively, the desired resources can be
preempted from other waiting processes
OSes: 6. Deadlock 20
3.4. Eliminate Circular Wait
 Impose a total ordering on all the resource
types, and force each process to request
resources in increasing order.
 Another approach: require a process to
release larger numbered resources when it
obtains a smaller numbered resource.
OSes: 6. Deadlock 21
4. Deadlock Avoidance
 In deadlock avoidance, the necessary
conditions are untouched.
 Instead, extra information about resources is
used by the OS to do better forward
planning of process/resource allocation
– indirectly avoids circular wait
OSes: 6. Deadlock 22
4.1. Safe States
 An OS is in a safe state if there is a
safe sequence of process executions
<P1, P2, …, Pn>.
 In a safe sequence, each Pi can satisfy its
resource requests by using the currently
available resources and (if necessary) the
resources held by Pj (j < i)
– only when Pj has finished
OSes: 6. Deadlock 23
Safe State Implications
 A safe state cannot lead to deadlock.
 An unsafe state may lead to deadlock.
 Deadlock is avoided by always
keeping the system in a safe state
– this may reduce resource utilization
Deadlock
Unsafe
Safe
Fig. 7.4, p.218
OSes: 6. Deadlock 24
Example 1
 Max no. of resources: 12 tape drives
 Max needs Current Allocation
P0 10 5
P1 4 2
P2 9 2
 Currently, there are 3 free tape drives
 The OS is in a safe state, since <P1, P0, P2>
is a safe sequence.
p.218
OSes: 6. Deadlock 25
Example 2
 Same as last slide, but P2 now has 3 tape
drives allocated currently.
 Max needs Current Allocation
P0 10 5
P1 4 2
P2 9 3
 The OS is in an unsafe state.
OSes: 6. Deadlock 26
4.2. Using Resource Allocation Graphs
 Assume a resource type only has one
instance.
 Add a claim edge:
– Pi  Rj
– process Pi may request a resource Rj in the future
– drawn as a dashed line
continued
OSes: 6. Deadlock 27
 When the resource is actually requested,
the claim edge is changed to a request
edge.
 When an assignment is released, the
assignment edge is changed back to a
claim edge.
continued
OSes: 6. Deadlock 28
 All resources must be claimed before
system start-up.
 An unsafe state is caused by a cycle in the
resource allocation graph.
OSes: 6. Deadlock 29
Example Figs 7.5, 7.6, p.220-221
R1
R2
P2
P1
R1
R2
P2
P1
R2 allocation to P2
creates an unsafe state
OSes: 6. Deadlock 30
4.3. Banker’s Algorithm
 Assume that:
– a resource can have multiple instances
– the OS has N processes, M resource types
 Initially, each process must declare the
maximum no. of resources it will need.
 Calculate a safe sequence if possible.
OSes: 6. Deadlock 31
Banker Data Structures
 Available[M]
– no. of available resource instances for each
resource type
– e.g. Available[j] == k means K Rj’s
 Max[N][M]
– max demand of each process
– e.g. max[i][j] == k means Pi wants k Rj’s
continued
OSes: 6. Deadlock 32
 Work[M]
– no. of resource instances available for work
(by all processes)
– e.g. Work[j] == k means K Rj’s are
available
 Finish[N]
– record of finished processes
– e.g. Pi is finished if Finish[i] == true
continued
OSes: 6. Deadlock 33
 Allocation[N][M]
– no. of resource instances allocated to each process
– e.g. Allocation[i][j] == k
means Pi currently has k Rj’s
 Need[N][M]
– no. of resource instances still needed by each process
– e.g. Need[i][j] == k
means Pi still needs k Rj’s
– Need[i][j] == Max[i][j] - Allocation[i][j]
continued
OSes: 6. Deadlock 34
 Request[N][M]
– no. of resource instances currently requested by
each process
– e.g. Request[i][j] == k
means Pi has requested k Rj’s
OSes: 6. Deadlock 35
Vectors
 Allocation[i]
– resources currently allocated to Pi
 Need[i]
– resources still needed by Pi
 Request[i]
– resources currently requested by Pi
shorthand for
referring to the
2D data structures
OSes: 6. Deadlock 36
The Safety Algorithm
1 Vector Copy: Work := Available; Finish := false
2 Find i such that Pi hasn’t finished but could:
Finish[i] == false
Need[i] <= Work
If no suitable i, go to step 4.
3 Assume Pi completes:
Work := Work + Allocation[i]
Finish[i] := true
go to step 2
4 If for all i Finish[i] == true then Safe-State
VUW CS 305;
p221-222
OSes: 6. Deadlock 37
Safety Example
 Resource Type Instances
A 10
B 5
C 7
p.222
continued
OSes: 6. Deadlock 38
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
 The OS is in a safe state since
<P1, P3, P4, P2, P0> is a safe sequence.
OSes: 6. Deadlock 39
Request Resource Algorithm
1 If (Need[i] < Request[i]) then max-error
2 While (Available < Request[i]) do wait
3 Construct a new state by:
Available = Available - Request[i]
Allocation[i] = Allocation[i] + Request [i]
Need[i] = Need[i] - Request [i]
4 If (new state is not safe) then
restore and wait
VUW CS 305;
p.222
OSes: 6. Deadlock 40
Request Example 1
 At some time, P1 requests an additional
1 A instance and 2 C instances
– i.e. Request[1] == (1, 0, 2)
 Does this lead to a safe state?
– Available >= Request[1] so continue
– generate new state and test for safety
OSes: 6. Deadlock 41
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 3
 The OS is in a safe state since
<P1, P3, P4, P0, P2> is a safe sequence.
OSes: 6. Deadlock 42
Further Request Examples
 From this state, P4 requests a further (3,3,0)
– cannot be granted, since insufficient resources
 Alternatively, P0 requests a further (0,2,0)
– should not be granted since the resulting state is
unsafe
OSes: 6. Deadlock 43
5. Deadlock Detection
 If there are no prevention or avoidance
mechanisms in place, then deadlock may occur.
 Deadlock detection should return enough
information so the OS can recover.
 How often should the detection algorithm be
executed?
OSes: 6. Deadlock 44
5.1. Wait-for Graph
 Assume that each resource has only one
instance.
 Create a wait-for graph by removing the
resource types nodes from a resource
allocation graph.
 Deadlock exists if and only if the wait-for
graph contains a cycle.
OSes: 6. Deadlock 45
Example Fig. 7.7, p.225
R1
P2
R3 R4
R2 R5
P1 P3
P5
P4
P2
P1 P3
P5
P4
OSes: 6. Deadlock 46
5.2. Banker’s Algorithm Variation
 If a resource type can have multiple
instances, then an algorithm very similar to
the banker’s algorithm can be used.
 The algorithm investigates every possible
allocation sequence for the processes that
remain to be completed.
OSes: 6. Deadlock 47
Detection Algorithm
1 Vector Copy: Work := Available; Finish := false
2 Find i such that Pi hasn’t finished but could:
Finish[i] == false
Request[i] <= Work
If no suitable i, go to step 4.
3 Assume Pi completes:
Work := Work + Allocation[i]
Finish[i] := true
go to step 2
4 If Finish[i] == false then Pi is deadlocked
VUW CS 305;
p.225
OSes: 6. Deadlock 48
Example 1
 Resource Type Instances
A 7
B 2
C 6
p.226
continued
OSes: 6. Deadlock 49
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
 The OS is not in a deadlocked state since
<P0, P2, P3, P1, P4> is a safe sequence.
OSes: 6. Deadlock 50
Example 2
 Change P2 to request 1 C instance
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 1
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
 The OS is deadlocked.
OSes: 6. Deadlock 51
6. Deadlock Recovery
 Tell the operator
 System-based recovery:
– abort one or more processes in the circular wait
– preempt resources in one or more deadlocked
processes
OSes: 6. Deadlock 52
6.1. Process Termination
 Abort all deadlocked processes, or
 Abort one process at a time until the
deadlocked cycle disappears
– not always easy to abort a process
– choice should be based on minimum cost
OSes: 6. Deadlock 53
6.2. Resource Preemption
 Issues:
– how to select a resource
(e.g. by using minimum cost)
– how to rollback the process which has just lost
its resources
– avoiding process starvation

More Related Content

Similar to 6. Deadlock.ppt

Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptamadayshwan
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptjbri1395
 
Os case study word
Os case study wordOs case study word
Os case study wordDhol Yash
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxssusere16bd9
 
CSS430_Deadlock.pptx
CSS430_Deadlock.pptxCSS430_Deadlock.pptx
CSS430_Deadlock.pptxamadayshwan
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidancewahab13
 
Deadlock in Distributed Systems
Deadlock in Distributed SystemsDeadlock in Distributed Systems
Deadlock in Distributed SystemsPritom Saha Akash
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock GalvinSonali Chauhan
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.pptKAnurag2
 
Galvin-operating System(Ch8)
Galvin-operating System(Ch8)Galvin-operating System(Ch8)
Galvin-operating System(Ch8)dsuyal1
 

Similar to 6. Deadlock.ppt (20)

Sucet os module_3_notes
Sucet os module_3_notesSucet os module_3_notes
Sucet os module_3_notes
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
 
DeadlockMar21.ppt
DeadlockMar21.pptDeadlockMar21.ppt
DeadlockMar21.ppt
 
Chapter 5(five).pdf
Chapter 5(five).pdfChapter 5(five).pdf
Chapter 5(five).pdf
 
Os case study word
Os case study wordOs case study word
Os case study word
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
 
CSS430_Deadlock.pptx
CSS430_Deadlock.pptxCSS430_Deadlock.pptx
CSS430_Deadlock.pptx
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
 
Deadlock in Distributed Systems
Deadlock in Distributed SystemsDeadlock in Distributed Systems
Deadlock in Distributed Systems
 
Deadlocks
 Deadlocks Deadlocks
Deadlocks
 
Dead Lock
Dead LockDead Lock
Dead Lock
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
Bankers
BankersBankers
Bankers
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.ppt
 
Deadlock
DeadlockDeadlock
Deadlock
 
Operating System
Operating SystemOperating System
Operating System
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
 
Galvin-operating System(Ch8)
Galvin-operating System(Ch8)Galvin-operating System(Ch8)
Galvin-operating System(Ch8)
 

More from Senthil Vit

Switching Problems.pdf
Switching Problems.pdfSwitching Problems.pdf
Switching Problems.pdfSenthil Vit
 
AsymptoticNotations.ppt
AsymptoticNotations.pptAsymptoticNotations.ppt
AsymptoticNotations.pptSenthil Vit
 
First Best and Worst Fit.pptx
First Best and Worst Fit.pptxFirst Best and Worst Fit.pptx
First Best and Worst Fit.pptxSenthil Vit
 
File Implementation Problem.pptx
File Implementation Problem.pptxFile Implementation Problem.pptx
File Implementation Problem.pptxSenthil Vit
 
Design Issues of an OS.ppt
Design Issues of an OS.pptDesign Issues of an OS.ppt
Design Issues of an OS.pptSenthil Vit
 
Operating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptxOperating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptxSenthil Vit
 
Virtualization.pptx
Virtualization.pptxVirtualization.pptx
Virtualization.pptxSenthil Vit
 
Traffic-Monitoring.ppt
Traffic-Monitoring.pptTraffic-Monitoring.ppt
Traffic-Monitoring.pptSenthil Vit
 
ALPHA_BETA_EXAMPLE_PROB
ALPHA_BETA_EXAMPLE_PROBALPHA_BETA_EXAMPLE_PROB
ALPHA_BETA_EXAMPLE_PROBSenthil Vit
 

More from Senthil Vit (14)

Switching Problems.pdf
Switching Problems.pdfSwitching Problems.pdf
Switching Problems.pdf
 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.ppt
 
AsymptoticNotations.ppt
AsymptoticNotations.pptAsymptoticNotations.ppt
AsymptoticNotations.ppt
 
snort.ppt
snort.pptsnort.ppt
snort.ppt
 
First Best and Worst Fit.pptx
First Best and Worst Fit.pptxFirst Best and Worst Fit.pptx
First Best and Worst Fit.pptx
 
File Implementation Problem.pptx
File Implementation Problem.pptxFile Implementation Problem.pptx
File Implementation Problem.pptx
 
Design Issues of an OS.ppt
Design Issues of an OS.pptDesign Issues of an OS.ppt
Design Issues of an OS.ppt
 
Operating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptxOperating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptx
 
deadlock.ppt
deadlock.pptdeadlock.ppt
deadlock.ppt
 
Virtualization.pptx
Virtualization.pptxVirtualization.pptx
Virtualization.pptx
 
Traffic-Monitoring.ppt
Traffic-Monitoring.pptTraffic-Monitoring.ppt
Traffic-Monitoring.ppt
 
Lect_2.pptx
Lect_2.pptxLect_2.pptx
Lect_2.pptx
 
os4-2_cop.ppt
os4-2_cop.pptos4-2_cop.ppt
os4-2_cop.ppt
 
ALPHA_BETA_EXAMPLE_PROB
ALPHA_BETA_EXAMPLE_PROBALPHA_BETA_EXAMPLE_PROB
ALPHA_BETA_EXAMPLE_PROB
 

Recently uploaded

CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...Neo4j
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIInflectra
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Chirag Panchal
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmuxevmux96
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaNeo4j
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfSrushith Repakula
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletAndrea Goulet
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksJinanKordab
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Maxim Salnikov
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Andreas Granig
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AIAGATSoftware
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfkalichargn70th171
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNeo4j
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024MulesoftMunichMeetup
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jNeo4j
 

Recently uploaded (20)

Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmux
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with Links
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdf
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMs
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
 

6. Deadlock.ppt

  • 1. OSes: 6. Deadlock 1 Operating Systems  Objectives – describe deadlock, and forms of prevention, avoidance, detection, and recovery Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003 6. Deadlock (Ch. 7, S&G) ch 8 in the 6th ed.
  • 2. OSes: 6. Deadlock 2 Contents 1. What is Deadlock? 2. Dealing with Deadlock 3. Deadlock Prevention 4. Deadlock Avoidance 5. Deadlock Detection 6. Deadlock Recovery
  • 3. OSes: 6. Deadlock 3 1. What is Deadlock?  An example from US Kansas law: – “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.”
  • 4. OSes: 6. Deadlock 4 In Picture Form: VUW CS 305 Neither truck can proceed.
  • 5. OSes: 6. Deadlock 5 1.1. System Deadlock  A process must request a resource before using it, and must release the resource after finishing with it.  A set of processes is in a deadlock state when every process in the set is waiting for a resource that can only be released by another process in the set.
  • 6. OSes: 6. Deadlock 6 1.2. Necessary Conditions for Deadlock  Mutual Exclusion – at least one resource must be held in non-shareable mode  Hold and Wait – a process is holding a resource and waiting for others continued
  • 7. OSes: 6. Deadlock 7  No Preemption – only the process can release its held resource  Circular Wait – {P0, P1, …, Pn} – Pi is waiting for the resource held by Pi+1; Pn is waiting for the resource held by P0
  • 8. OSes: 6. Deadlock 8 1.3. Resource Allocation Graph  A set of processes {P0, P1, …}  A set of resource types {R1, R2, …}, together with instances of those types. Pj Rk
  • 9. OSes: 6. Deadlock 9 Edge Notation  Pi  Rj – process i has requested an instance of resource j – called a request edge  Rj  Pi – an instance of resource j has been assigned to process i – called an assignment edge
  • 10. OSes: 6. Deadlock 10 Example Graph Fig. 7.1, p.211 R1 P1 P2 P3 R3 R2 R4
  • 11. OSes: 6. Deadlock 11 Finding a Deadlock  If the graph has no cycles then there are no deadlock processes.  If there is a cycle, then there may be a deadlock.
  • 12. OSes: 6. Deadlock 12 Graph with a Deadlock Fig. 7.2, p.212 R1 P1 P2 P3 R3 R2 R4
  • 13. OSes: 6. Deadlock 13 Graph without a Deadlock Fig. 7.3, p.213 R1 P1 P2 P3 R2 P4
  • 14. OSes: 6. Deadlock 14 2. Deadling with Deadlocks  Stop a deadlock ever occuring – deadlock prevention  disallow at least one of the necessary conditions – deadlock avoidance  see a deadlock coming and alter the process/resource allocation strategy continued
  • 15. OSes: 6. Deadlock 15  Deadlock detection and recovery  Ignore the problem – done by most OSes, including UNIX – cheap solution – infrequent, manual reboots may be acceptable
  • 16. OSes: 6. Deadlock 16 3. Deadlock Prevention  Eliminate one (or more) of: – mutual exclusion – hold and wait – no preemption (i.e. have preemption) – circular wait
  • 17. OSes: 6. Deadlock 17 3.1. Eliminate Mutual Exclusion  Shared resources do not require mutual exclusion – e.g. read-only files  But some resources cannot be shared (at the same time) – e.g. printers
  • 18. OSes: 6. Deadlock 18 3.2. Eliminate Hold & Wait  One approach requires that each process be allocated all of its resources before it begins executing – eliminates the wait possibility  Alternatively, only allow a process to request resources when it currently has none – eliminates the hold possibility
  • 19. OSes: 6. Deadlock 19 3.3. Eliminate “No Preemption”  Make a process automatically release its current resources if it cannot obtain all the ones it wants – restart the process when it can obtain everything  Alternatively, the desired resources can be preempted from other waiting processes
  • 20. OSes: 6. Deadlock 20 3.4. Eliminate Circular Wait  Impose a total ordering on all the resource types, and force each process to request resources in increasing order.  Another approach: require a process to release larger numbered resources when it obtains a smaller numbered resource.
  • 21. OSes: 6. Deadlock 21 4. Deadlock Avoidance  In deadlock avoidance, the necessary conditions are untouched.  Instead, extra information about resources is used by the OS to do better forward planning of process/resource allocation – indirectly avoids circular wait
  • 22. OSes: 6. Deadlock 22 4.1. Safe States  An OS is in a safe state if there is a safe sequence of process executions <P1, P2, …, Pn>.  In a safe sequence, each Pi can satisfy its resource requests by using the currently available resources and (if necessary) the resources held by Pj (j < i) – only when Pj has finished
  • 23. OSes: 6. Deadlock 23 Safe State Implications  A safe state cannot lead to deadlock.  An unsafe state may lead to deadlock.  Deadlock is avoided by always keeping the system in a safe state – this may reduce resource utilization Deadlock Unsafe Safe Fig. 7.4, p.218
  • 24. OSes: 6. Deadlock 24 Example 1  Max no. of resources: 12 tape drives  Max needs Current Allocation P0 10 5 P1 4 2 P2 9 2  Currently, there are 3 free tape drives  The OS is in a safe state, since <P1, P0, P2> is a safe sequence. p.218
  • 25. OSes: 6. Deadlock 25 Example 2  Same as last slide, but P2 now has 3 tape drives allocated currently.  Max needs Current Allocation P0 10 5 P1 4 2 P2 9 3  The OS is in an unsafe state.
  • 26. OSes: 6. Deadlock 26 4.2. Using Resource Allocation Graphs  Assume a resource type only has one instance.  Add a claim edge: – Pi  Rj – process Pi may request a resource Rj in the future – drawn as a dashed line continued
  • 27. OSes: 6. Deadlock 27  When the resource is actually requested, the claim edge is changed to a request edge.  When an assignment is released, the assignment edge is changed back to a claim edge. continued
  • 28. OSes: 6. Deadlock 28  All resources must be claimed before system start-up.  An unsafe state is caused by a cycle in the resource allocation graph.
  • 29. OSes: 6. Deadlock 29 Example Figs 7.5, 7.6, p.220-221 R1 R2 P2 P1 R1 R2 P2 P1 R2 allocation to P2 creates an unsafe state
  • 30. OSes: 6. Deadlock 30 4.3. Banker’s Algorithm  Assume that: – a resource can have multiple instances – the OS has N processes, M resource types  Initially, each process must declare the maximum no. of resources it will need.  Calculate a safe sequence if possible.
  • 31. OSes: 6. Deadlock 31 Banker Data Structures  Available[M] – no. of available resource instances for each resource type – e.g. Available[j] == k means K Rj’s  Max[N][M] – max demand of each process – e.g. max[i][j] == k means Pi wants k Rj’s continued
  • 32. OSes: 6. Deadlock 32  Work[M] – no. of resource instances available for work (by all processes) – e.g. Work[j] == k means K Rj’s are available  Finish[N] – record of finished processes – e.g. Pi is finished if Finish[i] == true continued
  • 33. OSes: 6. Deadlock 33  Allocation[N][M] – no. of resource instances allocated to each process – e.g. Allocation[i][j] == k means Pi currently has k Rj’s  Need[N][M] – no. of resource instances still needed by each process – e.g. Need[i][j] == k means Pi still needs k Rj’s – Need[i][j] == Max[i][j] - Allocation[i][j] continued
  • 34. OSes: 6. Deadlock 34  Request[N][M] – no. of resource instances currently requested by each process – e.g. Request[i][j] == k means Pi has requested k Rj’s
  • 35. OSes: 6. Deadlock 35 Vectors  Allocation[i] – resources currently allocated to Pi  Need[i] – resources still needed by Pi  Request[i] – resources currently requested by Pi shorthand for referring to the 2D data structures
  • 36. OSes: 6. Deadlock 36 The Safety Algorithm 1 Vector Copy: Work := Available; Finish := false 2 Find i such that Pi hasn’t finished but could: Finish[i] == false Need[i] <= Work If no suitable i, go to step 4. 3 Assume Pi completes: Work := Work + Allocation[i] Finish[i] := true go to step 2 4 If for all i Finish[i] == true then Safe-State VUW CS 305; p221-222
  • 37. OSes: 6. Deadlock 37 Safety Example  Resource Type Instances A 10 B 5 C 7 p.222 continued
  • 38. OSes: 6. Deadlock 38 Allocation Max Available Need A B C A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 7 4 3 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1  The OS is in a safe state since <P1, P3, P4, P2, P0> is a safe sequence.
  • 39. OSes: 6. Deadlock 39 Request Resource Algorithm 1 If (Need[i] < Request[i]) then max-error 2 While (Available < Request[i]) do wait 3 Construct a new state by: Available = Available - Request[i] Allocation[i] = Allocation[i] + Request [i] Need[i] = Need[i] - Request [i] 4 If (new state is not safe) then restore and wait VUW CS 305; p.222
  • 40. OSes: 6. Deadlock 40 Request Example 1  At some time, P1 requests an additional 1 A instance and 2 C instances – i.e. Request[1] == (1, 0, 2)  Does this lead to a safe state? – Available >= Request[1] so continue – generate new state and test for safety
  • 41. OSes: 6. Deadlock 41 Allocation Max Available Need A B C A B C A B C A B C P0 0 1 0 7 5 3 2 3 0 7 4 3 P1 3 0 2 3 2 2 0 2 0 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 3  The OS is in a safe state since <P1, P3, P4, P0, P2> is a safe sequence.
  • 42. OSes: 6. Deadlock 42 Further Request Examples  From this state, P4 requests a further (3,3,0) – cannot be granted, since insufficient resources  Alternatively, P0 requests a further (0,2,0) – should not be granted since the resulting state is unsafe
  • 43. OSes: 6. Deadlock 43 5. Deadlock Detection  If there are no prevention or avoidance mechanisms in place, then deadlock may occur.  Deadlock detection should return enough information so the OS can recover.  How often should the detection algorithm be executed?
  • 44. OSes: 6. Deadlock 44 5.1. Wait-for Graph  Assume that each resource has only one instance.  Create a wait-for graph by removing the resource types nodes from a resource allocation graph.  Deadlock exists if and only if the wait-for graph contains a cycle.
  • 45. OSes: 6. Deadlock 45 Example Fig. 7.7, p.225 R1 P2 R3 R4 R2 R5 P1 P3 P5 P4 P2 P1 P3 P5 P4
  • 46. OSes: 6. Deadlock 46 5.2. Banker’s Algorithm Variation  If a resource type can have multiple instances, then an algorithm very similar to the banker’s algorithm can be used.  The algorithm investigates every possible allocation sequence for the processes that remain to be completed.
  • 47. OSes: 6. Deadlock 47 Detection Algorithm 1 Vector Copy: Work := Available; Finish := false 2 Find i such that Pi hasn’t finished but could: Finish[i] == false Request[i] <= Work If no suitable i, go to step 4. 3 Assume Pi completes: Work := Work + Allocation[i] Finish[i] := true go to step 2 4 If Finish[i] == false then Pi is deadlocked VUW CS 305; p.225
  • 48. OSes: 6. Deadlock 48 Example 1  Resource Type Instances A 7 B 2 C 6 p.226 continued
  • 49. OSes: 6. Deadlock 49 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  The OS is not in a deadlocked state since <P0, P2, P3, P1, P4> is a safe sequence.
  • 50. OSes: 6. Deadlock 50 Example 2  Change P2 to request 1 C instance 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 1 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2  The OS is deadlocked.
  • 51. OSes: 6. Deadlock 51 6. Deadlock Recovery  Tell the operator  System-based recovery: – abort one or more processes in the circular wait – preempt resources in one or more deadlocked processes
  • 52. OSes: 6. Deadlock 52 6.1. Process Termination  Abort all deadlocked processes, or  Abort one process at a time until the deadlocked cycle disappears – not always easy to abort a process – choice should be based on minimum cost
  • 53. OSes: 6. Deadlock 53 6.2. Resource Preemption  Issues: – how to select a resource (e.g. by using minimum cost) – how to rollback the process which has just lost its resources – avoiding process starvation