SlideShare a Scribd company logo
Operating Systems: Deadlock
1
Deadlock
• Examples: Traffic Jam :
• Dining Philosophers
• Device allocation
– process 1 requests tape drive 1 & gets it
– process 2 requests tape drive 2 & gets it
– process 1 requests tape drive 2 but is blocked
– process 2 requests tape drive 1 but is blocked
• Semaphores : P(s) P(t)
P(t) P(s)
Operating Systems: Deadlock
2
• I/O spooling disc
– disc full of spooled input
– no room for subsequent output
• Over-allocation of pages in a virtual memory OS
– each process has a allocation of notional pages it must work within
– process acquires pages one by one
– normally does not use its full allocation
– kernel over-allocates total number of notional pages
» more efficient uses of memory
» like airlines overbooking seats
– deadlock may arise
» all processes by mischance approach use of their full allocation
» kernel cannot provide last pages it promised
» partial deadlock also - some processes blocked
– recovery ?
Operating Systems: Deadlock
3
• Resource:
– used by a single process at a single point in time
– any one of the same type can be allocated
• Pre-emptible:
– can be taken away from a process without ill effect
– no deadlocks with pre-emptible resources
• Non-Pre-emptible:
– cannot be taken away without problems
– most resources like this
– deadlock possible
Operating Systems: Deadlock
4
Definition : A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can cause
Necessary conditions for deadlock :
• Mutual Exclusion : each resource is either currently assigned to one
process or is available to be assigned
• Hold and wait : processes currently holding resources granted earlier can
request new resources
• Non-Pre-emption : resources previously granted cannot arbitrarily be
taken away from a process; they must be explicitly released by the process
• Circular wait : there must be a circular chain of two or more processes,
each of which is waiting for a resource held by the next member of the chain
Operating Systems: Deadlock
5
Resource Allocation Modelling using Graphs
Nodes : resource process
Arcs : resource requested :
resource allocated :
Operating Systems: Deadlock
6
Operating Systems: Deadlock
7
Operating Systems: Deadlock
8
• For multiple resources of the same type :
• Deadlock :
• A cycle not sufficient to
imply a deadlock :
Operating Systems: Deadlock
9
Possible Strategies :
• Ignore - the Ostrich or Head-in-the-Sand algorithm
– try to reduce chance of deadlock as far as reasonable
– accept that deadlocks will occur occasionally
» example: kernel table sizes - max number of pages, open files etc.
– MTBF versus deadlock probability ?
– cost of any other strategy may be too high
» overheads and efficiency
Operating Systems: Deadlock
10
• Deadlock Prevention
– negate one of the necessary conditions
• negating Mutual Exclusion :
– example: shared use of a printer
» give exclusive use of the printer to each user in turn wanting to print?
» deadlock possibility if exclusive access to another resource also allowed
» better to have a spooler process to which print jobs are sent
- complete output file must be generated first
– example: file system actions
» give a process exclusive access rights to a file directory
» example: moving a file from one directory to another
- possible deadlock if allowed exclusive access to two directories simultaneously
- should write code so as only to need to access one directory at a time
– solution?
» make resources concurrently sharable wherever possible e.g. read-only access
» most resources inherently not sharable!
Operating Systems: Deadlock
11
Resource Trajectory Graph
Operating Systems: Deadlock
12
Operating Systems: Deadlock
13
• negating Hold and Wait
– process could request all the resources it will ever need at once
» inefficient - not all resources needed all the time
» processes probably will not know in advance what resources they will need
» may have to wait excessive time to get all resources at once - starvation
- high priority processes may cause starvation of low priority processes
Operating Systems: Deadlock
14
– process could release existing resources it holds if it fails to get a new
resource immediately
» try again later
» a form of two phase locking used in databases
– whenever a new resource is needed, the process always releases its
existing resources and asks for all of them at once
– example: a process which copies a file from tape to disc, sorts the file on
disc, and then prints the results on a printer
» could request all three resources at the start
- wasteful of printer first, then tape drive later
» could initially request tape and disc together, do the read and sort, then
release both and finally request disc and printer together to do the printing
- more efficient
- must ensure data stays intact on disc between phases
Operating Systems: Deadlock
15
• negating Non-Pre-emption
– difficult to achieve in practice
» cannot take a printer away from a process in the middle of printing
» cannot take a semaphore away from a process arbitrarily
- might be in the middle of updating a shared area
» cannot take open streams, pipes and sockets away
- process would need to be written very carefully, probably using signals
- very undesirable if possible at all
– occasionally possible :
» processes resident in main memory
» some deadlock occurs such as failure to allocate a page
» one or more processes can be swapped out to disc to release their pages
and allow remaining processes to continue
- as long as they release any other resources they also hold on the way out
» put back on the scheduling queues to be re-admitted to memory later
Operating Systems: Deadlock
16
• negating Circular Wait
– require that a process can only acquire one resource at a time
» example: moving a file from one directory to another
– require processes to acquire resources in a certain order
– example:
» 1: tape drive
» 2: disc drive
» 3: printer
» 4: plotter
» 5: typesetter
– example: semaphores
» semaphores identified by
number claimed in numerical order
Operating Systems: Deadlock
17
• Deadlock Avoidance
– deadlock possible but avoided by careful allocation of resources
– avoid entering unsafe states
– a state is safe if it is not deadlocked and there is a way to satisfy all requests
currently pending by running the processes in some order
– need to know all future requests of processes
Operating Systems: Deadlock
18
• Example: can processes run to completion in some order?
– with 10 units of resource to allocate :
– if A runs first and acquires a further unit :
Operating Systems: Deadlock
19
• avoidance using resource allocation graphs - for one instance resources
– add an extra type of arc - the claim arc to indicate future requests
– when the future request is actually made, convert this to an allocation arc
– then check for loops
Operating Systems: Deadlock
20
Banker’s Algorithm (Dijkstra)
• Single resource
– at each request, consider whether granting will lead to an unsafe state - if so,
deny
– is state after the notional grant still safe?
» are there enough resources to satisfy the demands of some process
» if so, process is notionally allowed to proceed
» in due course, it is assumed to finish and return all its resources
» process closest to its limit is the checked, and the steps repeated
» if all processes can eventually run to completion, state is safe
Operating Systems: Deadlock
21
• Multiple resources
– m types of resource, n processes
– vector comparison :
» A  B if Ai  Bi for 0  i  m
Operating Systems: Deadlock
22
» look for a row in R  A i.e. a process whose requests can be met
» if no such row exists, state is unsafe
» add this row of R into the same row of C and subtract it from A
i.e. notionally allocate the resources to the process
» add this row of C back into A and set the row of C to zero i.e. the process
notionally completes and returns its resources
» repeat these steps until all C is all zero i.e. all processes notionally finished
(initial state is safe) or until a suitable row in R cannot be found (unsafe)
C R
Operating Systems: Deadlock
23
Drawbacks of Banker’s Algorithm
– processes rarely know in advance how many resources they will need
– the number of processes changes as time progresses
– resources once available can disappear
– the algorithm assumes processes will return their resources within a
reasonable time
– processes may only get their resources after an arbitrarily long delay
– practical use is therefore rare!
Operating Systems: Deadlock
24
• Detection and Recovery
– let deadlock occur, then detect and recover somehow
• Methods of Detection - single resources
– search for loops in resource allocation graph
Operating Systems: Deadlock
25
• Depth-first Graph search
– use a list of nodes L and progressively mark arcs
1. For each node N in the graph, perform steps 2-6 with N as starting node
2. Initialise L to empty and designate all arcs as unmarked
3. Add current node to L
» check if node appears twice in L
» if so, graph contains a cycle - algorithm terminates
4. From given node, if there are any unmarked outgoing arcs, goto 5
else goto 6
5. Pick an unmarked outgoing arc and mark it
» follow it to new current node and goto 3
6. Have reached a dead end
» go back to previous node and goto 4
» if this node is the initial node, graph does not contain cycles and
algorithm terminates
Operating Systems: Deadlock
26
Operating Systems: Deadlock
27
• Using an Adjacency Matrix
– adjacency matrix represents single hops
– two-hops :
node(i) -> node(j) :
node(i) -> node(1) and node(1) -> node(j)
or node(i) -> node(2) and node(2) -> node(j)
or node(i) -> node(3) and node(3) -> node(j)
. . . . .
or node(i) -> node(N) and node(N) -> node(j)
– binary (Boolean) matrix multiplication
» and replaces multiplication
» or replaces addition
Operating Systems: Deadlock
28
– 2 hops :
0 1 0 0 0 1 0 0
0 0 1 1 0 0 1 1
1 0 0 1 1 0 0 1
0 0 0 0 0 0 0 0
– 3 hops :
0 0 1 1 0 1 0 0
1 0 0 1 0 0 1 1
0 1 0 0 1 0 0 1
0 0 0 0 0 0 0 0
– 4 hops ? > 4 hops ?
– identifying disjoint cycles ?
– Transitive Closure equivalent to the N matrix multiplications
*
*
Operating Systems: Deadlock
29
• Warshall’s Algorithm for computing Transitive Closure :
– if there is a way to get from node x to node y and a way to get from node y to node z, then
there is a way to get from node x to node z
– if there is a way to get from node x to node y using only nodes with indices less than x and a
way to get from node y to node z , then there is a way to get from from node x to node z using
only nodes with indices less than x+1
for (y=0; y<N; y++) {
for (x=1; x<N; x++) {
if ( A[x,y] ) {
for (z=1; z<N; z++) {
if ( A[y,z] ) A[x.z] = true;
}
}
}
}
Operating Systems: Deadlock
30
• Semaphore loop detection :
Operating Systems: Deadlock
31
• Multiple resources
– apply equivalent of Banker’s algorithm using current resource requests
– any processes unsatisfied are deadlocked
• When to check for deadlock?
– every time a resource request is made
– regularly at fixed time intervals
– when CPU utilisation drops below some threshold
Operating Systems: Deadlock
32
Recovery from Deadlock
• Pre-emption
– take resources from a process and give to others
– how to select a victim?
» order of precedence for pre-empting
» number of resources already held
» how many more will it need to complete?
» amount of CPU time already used
– swapping process out of memory may be sufficient
» but may still hold resources involved in deadlock
– may need to roll pre-empted process back
» back to some safe restart point or go back to beginning
» may need to checkpoint processes
» not convenient for user interaction!
– need to avoid starvation of a low priority process always being pre-empted
» include number of previous pre-emptions as a choice factor
Operating Systems: Deadlock
33
• Process Termination
– drastic ultimate solution
– abort all processes involved in the deadlock
» all resources returned for re-use
– abort processes one by one until deadlock resolved
» how to choose order of precedence?
» will the process need to be rerun?
– aborting a process may cause severe difficulties
» may be in the process of updating a file which will be left inconsistent
– a process gets into an infinite program loop while holding resources
» common situation in practice

More Related Content

Similar to deadlock.ppt

Deadlocks
Deadlocks Deadlocks
Deadlocks
Shijin Raj P
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
sheraz7288
 
06-Deadlocks.ppt
06-Deadlocks.ppt06-Deadlocks.ppt
06-Deadlocks.ppt
ansariparveen06
 
OS 7.pptx
OS 7.pptxOS 7.pptx
OS 7.pptx
ZainabShahzad9
 
deadlock and starvation resources allocation.pptx
deadlock and starvation resources allocation.pptxdeadlock and starvation resources allocation.pptx
deadlock and starvation resources allocation.pptx
Saba651353
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
Nazir Ahmed
 
Distributed Deadlock Detection.ppt
Distributed Deadlock Detection.pptDistributed Deadlock Detection.ppt
Distributed Deadlock Detection.ppt
Babar Kamran Ahmed (LION)
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
KokilaK25
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlockstech2click
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx
shrijanasharma3
 
DeadlockMar21.ppt
DeadlockMar21.pptDeadlockMar21.ppt
DeadlockMar21.ppt
hetrathod001
 
Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
Nusaike Mufthie
 
The Functions of the Operating System
The Functions of the Operating SystemThe Functions of the Operating System
The Functions of the Operating System
andyr91
 
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptxosvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
Bhaskar271887
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptx
JOHNZHOU52
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
AUST
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
shreesha16
 
28 to 29 detection
28 to 29 detection 28 to 29 detection
28 to 29 detection myrajendra
 
ch 2 - DISTRIBUTED DEADLOCK DETECTION.pptx
ch 2 - DISTRIBUTED DEADLOCK DETECTION.pptxch 2 - DISTRIBUTED DEADLOCK DETECTION.pptx
ch 2 - DISTRIBUTED DEADLOCK DETECTION.pptx
Ethiopia Satlliet television
 

Similar to deadlock.ppt (20)

Deadlocks1
Deadlocks1Deadlocks1
Deadlocks1
 
Deadlocks
Deadlocks Deadlocks
Deadlocks
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
06-Deadlocks.ppt
06-Deadlocks.ppt06-Deadlocks.ppt
06-Deadlocks.ppt
 
OS 7.pptx
OS 7.pptxOS 7.pptx
OS 7.pptx
 
deadlock and starvation resources allocation.pptx
deadlock and starvation resources allocation.pptxdeadlock and starvation resources allocation.pptx
deadlock and starvation resources allocation.pptx
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
 
Distributed Deadlock Detection.ppt
Distributed Deadlock Detection.pptDistributed Deadlock Detection.ppt
Distributed Deadlock Detection.ppt
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx
 
DeadlockMar21.ppt
DeadlockMar21.pptDeadlockMar21.ppt
DeadlockMar21.ppt
 
Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
 
The Functions of the Operating System
The Functions of the Operating SystemThe Functions of the Operating System
The Functions of the Operating System
 
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptxosvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptx
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
28 to 29 detection
28 to 29 detection 28 to 29 detection
28 to 29 detection
 
ch 2 - DISTRIBUTED DEADLOCK DETECTION.pptx
ch 2 - DISTRIBUTED DEADLOCK DETECTION.pptxch 2 - DISTRIBUTED DEADLOCK DETECTION.pptx
ch 2 - DISTRIBUTED DEADLOCK DETECTION.pptx
 

More from Senthil Vit

Switching Problems.pdf
Switching Problems.pdfSwitching Problems.pdf
Switching Problems.pdf
Senthil Vit
 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.ppt
Senthil Vit
 
AsymptoticNotations.ppt
AsymptoticNotations.pptAsymptoticNotations.ppt
AsymptoticNotations.ppt
Senthil Vit
 
snort.ppt
snort.pptsnort.ppt
snort.ppt
Senthil Vit
 
First Best and Worst Fit.pptx
First Best and Worst Fit.pptxFirst Best and Worst Fit.pptx
First Best and Worst Fit.pptx
Senthil Vit
 
File Implementation Problem.pptx
File Implementation Problem.pptxFile Implementation Problem.pptx
File Implementation Problem.pptx
Senthil Vit
 
Design Issues of an OS.ppt
Design Issues of an OS.pptDesign Issues of an OS.ppt
Design Issues of an OS.ppt
Senthil Vit
 
Operating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptxOperating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptx
Senthil Vit
 
Virtualization.pptx
Virtualization.pptxVirtualization.pptx
Virtualization.pptx
Senthil Vit
 
Traffic-Monitoring.ppt
Traffic-Monitoring.pptTraffic-Monitoring.ppt
Traffic-Monitoring.ppt
Senthil Vit
 
Lect_2.pptx
Lect_2.pptxLect_2.pptx
Lect_2.pptx
Senthil Vit
 
6. Deadlock.ppt
6. Deadlock.ppt6. Deadlock.ppt
6. Deadlock.ppt
Senthil Vit
 
os4-2_cop.ppt
os4-2_cop.pptos4-2_cop.ppt
os4-2_cop.ppt
Senthil Vit
 
ALPHA_BETA_EXAMPLE_PROB
ALPHA_BETA_EXAMPLE_PROBALPHA_BETA_EXAMPLE_PROB
ALPHA_BETA_EXAMPLE_PROB
Senthil 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
 
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
 
6. Deadlock.ppt
6. Deadlock.ppt6. Deadlock.ppt
6. Deadlock.ppt
 
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

Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 

Recently uploaded (20)

Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 

deadlock.ppt

  • 1. Operating Systems: Deadlock 1 Deadlock • Examples: Traffic Jam : • Dining Philosophers • Device allocation – process 1 requests tape drive 1 & gets it – process 2 requests tape drive 2 & gets it – process 1 requests tape drive 2 but is blocked – process 2 requests tape drive 1 but is blocked • Semaphores : P(s) P(t) P(t) P(s)
  • 2. Operating Systems: Deadlock 2 • I/O spooling disc – disc full of spooled input – no room for subsequent output • Over-allocation of pages in a virtual memory OS – each process has a allocation of notional pages it must work within – process acquires pages one by one – normally does not use its full allocation – kernel over-allocates total number of notional pages » more efficient uses of memory » like airlines overbooking seats – deadlock may arise » all processes by mischance approach use of their full allocation » kernel cannot provide last pages it promised » partial deadlock also - some processes blocked – recovery ?
  • 3. Operating Systems: Deadlock 3 • Resource: – used by a single process at a single point in time – any one of the same type can be allocated • Pre-emptible: – can be taken away from a process without ill effect – no deadlocks with pre-emptible resources • Non-Pre-emptible: – cannot be taken away without problems – most resources like this – deadlock possible
  • 4. Operating Systems: Deadlock 4 Definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause Necessary conditions for deadlock : • Mutual Exclusion : each resource is either currently assigned to one process or is available to be assigned • Hold and wait : processes currently holding resources granted earlier can request new resources • Non-Pre-emption : resources previously granted cannot arbitrarily be taken away from a process; they must be explicitly released by the process • Circular wait : there must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain
  • 5. Operating Systems: Deadlock 5 Resource Allocation Modelling using Graphs Nodes : resource process Arcs : resource requested : resource allocated :
  • 8. Operating Systems: Deadlock 8 • For multiple resources of the same type : • Deadlock : • A cycle not sufficient to imply a deadlock :
  • 9. Operating Systems: Deadlock 9 Possible Strategies : • Ignore - the Ostrich or Head-in-the-Sand algorithm – try to reduce chance of deadlock as far as reasonable – accept that deadlocks will occur occasionally » example: kernel table sizes - max number of pages, open files etc. – MTBF versus deadlock probability ? – cost of any other strategy may be too high » overheads and efficiency
  • 10. Operating Systems: Deadlock 10 • Deadlock Prevention – negate one of the necessary conditions • negating Mutual Exclusion : – example: shared use of a printer » give exclusive use of the printer to each user in turn wanting to print? » deadlock possibility if exclusive access to another resource also allowed » better to have a spooler process to which print jobs are sent - complete output file must be generated first – example: file system actions » give a process exclusive access rights to a file directory » example: moving a file from one directory to another - possible deadlock if allowed exclusive access to two directories simultaneously - should write code so as only to need to access one directory at a time – solution? » make resources concurrently sharable wherever possible e.g. read-only access » most resources inherently not sharable!
  • 13. Operating Systems: Deadlock 13 • negating Hold and Wait – process could request all the resources it will ever need at once » inefficient - not all resources needed all the time » processes probably will not know in advance what resources they will need » may have to wait excessive time to get all resources at once - starvation - high priority processes may cause starvation of low priority processes
  • 14. Operating Systems: Deadlock 14 – process could release existing resources it holds if it fails to get a new resource immediately » try again later » a form of two phase locking used in databases – whenever a new resource is needed, the process always releases its existing resources and asks for all of them at once – example: a process which copies a file from tape to disc, sorts the file on disc, and then prints the results on a printer » could request all three resources at the start - wasteful of printer first, then tape drive later » could initially request tape and disc together, do the read and sort, then release both and finally request disc and printer together to do the printing - more efficient - must ensure data stays intact on disc between phases
  • 15. Operating Systems: Deadlock 15 • negating Non-Pre-emption – difficult to achieve in practice » cannot take a printer away from a process in the middle of printing » cannot take a semaphore away from a process arbitrarily - might be in the middle of updating a shared area » cannot take open streams, pipes and sockets away - process would need to be written very carefully, probably using signals - very undesirable if possible at all – occasionally possible : » processes resident in main memory » some deadlock occurs such as failure to allocate a page » one or more processes can be swapped out to disc to release their pages and allow remaining processes to continue - as long as they release any other resources they also hold on the way out » put back on the scheduling queues to be re-admitted to memory later
  • 16. Operating Systems: Deadlock 16 • negating Circular Wait – require that a process can only acquire one resource at a time » example: moving a file from one directory to another – require processes to acquire resources in a certain order – example: » 1: tape drive » 2: disc drive » 3: printer » 4: plotter » 5: typesetter – example: semaphores » semaphores identified by number claimed in numerical order
  • 17. Operating Systems: Deadlock 17 • Deadlock Avoidance – deadlock possible but avoided by careful allocation of resources – avoid entering unsafe states – a state is safe if it is not deadlocked and there is a way to satisfy all requests currently pending by running the processes in some order – need to know all future requests of processes
  • 18. Operating Systems: Deadlock 18 • Example: can processes run to completion in some order? – with 10 units of resource to allocate : – if A runs first and acquires a further unit :
  • 19. Operating Systems: Deadlock 19 • avoidance using resource allocation graphs - for one instance resources – add an extra type of arc - the claim arc to indicate future requests – when the future request is actually made, convert this to an allocation arc – then check for loops
  • 20. Operating Systems: Deadlock 20 Banker’s Algorithm (Dijkstra) • Single resource – at each request, consider whether granting will lead to an unsafe state - if so, deny – is state after the notional grant still safe? » are there enough resources to satisfy the demands of some process » if so, process is notionally allowed to proceed » in due course, it is assumed to finish and return all its resources » process closest to its limit is the checked, and the steps repeated » if all processes can eventually run to completion, state is safe
  • 21. Operating Systems: Deadlock 21 • Multiple resources – m types of resource, n processes – vector comparison : » A  B if Ai  Bi for 0  i  m
  • 22. Operating Systems: Deadlock 22 » look for a row in R  A i.e. a process whose requests can be met » if no such row exists, state is unsafe » add this row of R into the same row of C and subtract it from A i.e. notionally allocate the resources to the process » add this row of C back into A and set the row of C to zero i.e. the process notionally completes and returns its resources » repeat these steps until all C is all zero i.e. all processes notionally finished (initial state is safe) or until a suitable row in R cannot be found (unsafe) C R
  • 23. Operating Systems: Deadlock 23 Drawbacks of Banker’s Algorithm – processes rarely know in advance how many resources they will need – the number of processes changes as time progresses – resources once available can disappear – the algorithm assumes processes will return their resources within a reasonable time – processes may only get their resources after an arbitrarily long delay – practical use is therefore rare!
  • 24. Operating Systems: Deadlock 24 • Detection and Recovery – let deadlock occur, then detect and recover somehow • Methods of Detection - single resources – search for loops in resource allocation graph
  • 25. Operating Systems: Deadlock 25 • Depth-first Graph search – use a list of nodes L and progressively mark arcs 1. For each node N in the graph, perform steps 2-6 with N as starting node 2. Initialise L to empty and designate all arcs as unmarked 3. Add current node to L » check if node appears twice in L » if so, graph contains a cycle - algorithm terminates 4. From given node, if there are any unmarked outgoing arcs, goto 5 else goto 6 5. Pick an unmarked outgoing arc and mark it » follow it to new current node and goto 3 6. Have reached a dead end » go back to previous node and goto 4 » if this node is the initial node, graph does not contain cycles and algorithm terminates
  • 27. Operating Systems: Deadlock 27 • Using an Adjacency Matrix – adjacency matrix represents single hops – two-hops : node(i) -> node(j) : node(i) -> node(1) and node(1) -> node(j) or node(i) -> node(2) and node(2) -> node(j) or node(i) -> node(3) and node(3) -> node(j) . . . . . or node(i) -> node(N) and node(N) -> node(j) – binary (Boolean) matrix multiplication » and replaces multiplication » or replaces addition
  • 28. Operating Systems: Deadlock 28 – 2 hops : 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 1 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 – 3 hops : 0 0 1 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 – 4 hops ? > 4 hops ? – identifying disjoint cycles ? – Transitive Closure equivalent to the N matrix multiplications * *
  • 29. Operating Systems: Deadlock 29 • Warshall’s Algorithm for computing Transitive Closure : – if there is a way to get from node x to node y and a way to get from node y to node z, then there is a way to get from node x to node z – if there is a way to get from node x to node y using only nodes with indices less than x and a way to get from node y to node z , then there is a way to get from from node x to node z using only nodes with indices less than x+1 for (y=0; y<N; y++) { for (x=1; x<N; x++) { if ( A[x,y] ) { for (z=1; z<N; z++) { if ( A[y,z] ) A[x.z] = true; } } } }
  • 30. Operating Systems: Deadlock 30 • Semaphore loop detection :
  • 31. Operating Systems: Deadlock 31 • Multiple resources – apply equivalent of Banker’s algorithm using current resource requests – any processes unsatisfied are deadlocked • When to check for deadlock? – every time a resource request is made – regularly at fixed time intervals – when CPU utilisation drops below some threshold
  • 32. Operating Systems: Deadlock 32 Recovery from Deadlock • Pre-emption – take resources from a process and give to others – how to select a victim? » order of precedence for pre-empting » number of resources already held » how many more will it need to complete? » amount of CPU time already used – swapping process out of memory may be sufficient » but may still hold resources involved in deadlock – may need to roll pre-empted process back » back to some safe restart point or go back to beginning » may need to checkpoint processes » not convenient for user interaction! – need to avoid starvation of a low priority process always being pre-empted » include number of previous pre-emptions as a choice factor
  • 33. Operating Systems: Deadlock 33 • Process Termination – drastic ultimate solution – abort all processes involved in the deadlock » all resources returned for re-use – abort processes one by one until deadlock resolved » how to choose order of precedence? » will the process need to be rerun? – aborting a process may cause severe difficulties » may be in the process of updating a file which will be left inconsistent – a process gets into an infinite program loop while holding resources » common situation in practice