SlideShare a Scribd company logo
1 of 28
Rushdi Shams, Dept of CSE, KUET 1
Operating SystemsOperating Systems
DeadlocksDeadlocks
Version 1.0
Rushdi Shams, Dept of CSE, KUET 2
Definition of Deadlock
When two trains approach each other at crossing,
both shall come to a full stop and neither shall start
up again until the other has gone (Kansas Legislature
for railroads in 1920)
A process requests resources
If the resources are not available at that time, the
process enters into waiting state
It never again change state because resources it
requested are requested by other waiting processes
Rushdi Shams, Dept of CSE, KUET 3
How processes utilize resources
If a system has 2 CPUs, then it is said, the system has
a resource type CPU of 2 instances
A process must request a resource before using it
And it must release after using it
A process may request as many resources as it
requires to carry out its designated task
Number of requesting resources MUST not exceed
the total number of available resources
So, a process utilizes resources with three sequences:
request-use-release
Rushdi Shams, Dept of CSE, KUET 4
Deadlock Characterization
Mutual exclusion: only one process at a time can
use a resource.
Hold and wait: a process holding at least one
resource is waiting to acquire additional resources
held by other processes.
No preemption: Process releases resources
voluntarily, you cannot force it.
Circular wait: there exists a set {P0, P1, …, P0} of
waiting processes such that P0is waiting for a resource
that is held by P1, P1 is waiting for a resource that is
held by
Rushdi Shams, Dept of CSE, KUET 5
Resource-Allocation Graph
• A set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the
processes in the system.
R = {R1, R2, …, Rm}, the set consisting of all
resource types in the system.
request edge – directed edge P1 → Rj
assignment edge – directed edge Rj → Pi
Rushdi Shams, Dept of CSE, KUET 6
Resource-Allocation Graph
Process
Resource Type with 4 instances
Pi requests instance of Rj
Pi is holding an instance of Rj Pi
Pi
Rj
Rj
Rushdi Shams, Dept of CSE, KUET 7
Resource Allocation Graph
Rushdi Shams, Dept of CSE, KUET 8
Resource Allocation Graph With A
Deadlock
Rushdi Shams, Dept of CSE, KUET 9
Graph With A Cycle But No
Deadlock
Rushdi Shams, Dept of CSE, KUET 10
Resource Allocation Graph
If graph contains no cycles then no deadlock.
If graph contains a cycle then
if only one instance per resource type, then deadlock.
if several instances per resource type, possibility of
deadlock
Rushdi Shams, Dept of CSE, KUET 11
Methods for handling Deadlocks
We can prevent deadlock- which is still a dream, but
soon have a chance to be real
We can avoid deadlocks- by using protocols, ensuring
that the system will never enter a deadlock state
We can allow the system to enter a deadlock state,
detect it and recover
We can use Ostrich Approach
Rushdi Shams, Dept of CSE, KUET 12
Ostrich Approach
Ostrich is not the name of the inventor of the
approach, it is simply an ostrich.
When ostriches run, they just run!  They pay very
little interest in their surroundings.
It means, Ostrich Algorithm in deadlocks is simply
ignoring the problem… pretending it will never occur.
UNIX runs with this approach.
Rushdi Shams, Dept of CSE, KUET 13
Deadlock Prevention
Mutual Exclusion – not required for sharable
resources; must hold for nonsharable resources.
Hold and Wait – must guarantee that whenever a
process requests a resource, it does not hold any other
resources.
Require process to request and be allocated all its
resources before it begins execution, or allow process to
request resources only when the process has none.
Low resource utilization; starvation possible.
Rushdi Shams, Dept of CSE, KUET 14
Deadlock Prevention
No Preemption –
• If a process that is holding some resources requests
another resource that cannot be immediately allocated
to it, then all resources currently being held are
released.
• Preempted resources are added to the list of resources
for which the process is waiting.
• Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
Rushdi Shams, Dept of CSE, KUET 15
Deadlock Avoidance
• Requires that the system has some additional a priori
information available.
Simplest and most useful model requires that each process
declare the maximum number of resources of each type that it
may need.
The deadlock-avoidance algorithm dynamically examines the
resource-allocation state to ensure that there can never be a
circular-wait condition.
Resource-allocation state is defined by the number of available
and allocated resources, and the maximum demands of the
processes.
Rushdi Shams, Dept of CSE, KUET 16
Safe State
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
System is in safe state if there exists a sequence <P1, P2, …, Pn> of
ALL the processes is the systems such that for each Pi, the
resources that Pican still request can be satisfied by currently
available resources + resources held by all the Pj, with j < i.
That is:
If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished.
When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its needed resources, and so on.
Rushdi Shams, Dept of CSE, KUET 17
Basic Facts
If a system is in safe state, then no deadlocks.
If a system is in unsafe state, then possibility of
deadlock.
Avoidance means ensuring that a system will never
enter an unsafe state.
Rushdi Shams, Dept of CSE, KUET 18
Rushdi Shams, Dept of CSE, KUET 19
Deadlock Avoidance: on
Overhead Projector
Rushdi Shams, Dept of CSE, KUET 20
Deadlock Detection & Recovery
 If a system does not impose deadlock prevention
and avoidance mechanism, then a deadlock
situation MAY occur.
 In this circumstance, the system must provide
1. An algorithm to detect deadlock
2. Recovery Mechanism
Rushdi Shams, Dept of CSE, KUET 21
Detection Mechanism 1
We assume that the resource has only one instance.
Maintain wait-for graph (WFG)
Nodes are processes.
Pi → Pj if Pi is waiting for Pj.
Periodically searches for a cycle in the graph. If there
is a cycle, there exists a deadlock.
Rushdi Shams, Dept of CSE, KUET 22
Detection Mechanism 1
Rushdi Shams, Dept of CSE, KUET 23
Detection Mechanism 2
We assume that resources have more than one instance
Five processes P0 through P4;three resource types
A (7 instances), B (2 instances), and C (6 instances).
Snapshot at time T0:
Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Sequence <P0, P2, P3, P4, P1> will result in
Rushdi Shams, Dept of CSE, KUET 24
Detection Algorithm Usage
When should we invoke the detection algorithm?
Depends on two factors-
How often a deadlock is likely to occur?
How many processes will be affected by deadlock when
it happens?
How many processes will be needed to roll back?
Normally detection algorithm is used -
Whenever a request is not served
In every hour
Whenever CPU utilization drops under 40%
Rushdi Shams, Dept of CSE, KUET 25
Recovery Mechanism: Process
Termination
Abort all deadlocked process
Clearly will break the cycle but at a great price
Processes may have computed for a long time and now
the values are going to be lost 
Abort one process at a time until the deadlock cycle is
eliminated
Considerable overhead
Rushdi Shams, Dept of CSE, KUET 26
Recovery Mechanism: Process
Termination
In which order should we choose to abort?
Priority of the process.
How long process has computed, and how much longer
to completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be terminated.
Is process interactive or batch?
Rushdi Shams, Dept of CSE, KUET 27
Recovery Mechanism: Resource
Preemption
Selecting a victim – minimize cost.
Rollback – return to some safe state, restart process
for that state.
Starvation – same process may always be picked as
victim, include number of rollback in cost factor.
Rushdi Shams, Dept of CSE, KUET 28
Reference
Operating Systems Concept (6th
Edition)
by Silberschatz, Galvin and Gagne

More Related Content

What's hot

Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)Shayek Parvez
 
Deadlock and memory management -- Operating System
Deadlock and memory management -- Operating SystemDeadlock and memory management -- Operating System
Deadlock and memory management -- Operating SystemEktaVaswani2
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating systemSara Ali
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-SystemsVenkata Sreeram
 
Lecture5
Lecture5Lecture5
Lecture5jntu
 

What's hot (11)

Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
Deadlock and memory management -- Operating System
Deadlock and memory management -- Operating SystemDeadlock and memory management -- Operating System
Deadlock and memory management -- Operating System
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
OS_Ch8
OS_Ch8OS_Ch8
OS_Ch8
 
Chapter06
Chapter06Chapter06
Chapter06
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
 
deadlock
deadlockdeadlock
deadlock
 
Lecture5
Lecture5Lecture5
Lecture5
 

Similar to Lecture 11,12 and 13 deadlocks

Similar to Lecture 11,12 and 13 deadlocks (20)

deadlock in OS.pptx
deadlock in OS.pptxdeadlock in OS.pptx
deadlock in OS.pptx
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.ppt
 
Deadlock and Banking Algorithm
Deadlock and Banking AlgorithmDeadlock and Banking Algorithm
Deadlock and Banking Algorithm
 
Deadlock
DeadlockDeadlock
Deadlock
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
 
Os unit 4
Os unit 4Os unit 4
Os unit 4
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocks
 
Deadlock
DeadlockDeadlock
Deadlock
 
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptxosvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
 
Deadlock in Real Time operating Systempptx
Deadlock in Real Time operating SystempptxDeadlock in Real Time operating Systempptx
Deadlock in Real Time operating Systempptx
 
Distributed Operating System_2
Distributed Operating System_2Distributed Operating System_2
Distributed Operating System_2
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
Operating system Deadlock
Operating system DeadlockOperating system Deadlock
Operating system Deadlock
 
Sucet os module_3_notes
Sucet os module_3_notesSucet os module_3_notes
Sucet os module_3_notes
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Deadlock.ppt
Deadlock.pptDeadlock.ppt
Deadlock.ppt
 
Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
 

More from Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

More from Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Recently uploaded

Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Recently uploaded (20)

Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Lecture 11,12 and 13 deadlocks

  • 1. Rushdi Shams, Dept of CSE, KUET 1 Operating SystemsOperating Systems DeadlocksDeadlocks Version 1.0
  • 2. Rushdi Shams, Dept of CSE, KUET 2 Definition of Deadlock When two trains approach each other at crossing, both shall come to a full stop and neither shall start up again until the other has gone (Kansas Legislature for railroads in 1920) A process requests resources If the resources are not available at that time, the process enters into waiting state It never again change state because resources it requested are requested by other waiting processes
  • 3. Rushdi Shams, Dept of CSE, KUET 3 How processes utilize resources If a system has 2 CPUs, then it is said, the system has a resource type CPU of 2 instances A process must request a resource before using it And it must release after using it A process may request as many resources as it requires to carry out its designated task Number of requesting resources MUST not exceed the total number of available resources So, a process utilizes resources with three sequences: request-use-release
  • 4. Rushdi Shams, Dept of CSE, KUET 4 Deadlock Characterization Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: Process releases resources voluntarily, you cannot force it. Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by
  • 5. Rushdi Shams, Dept of CSE, KUET 5 Resource-Allocation Graph • A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn}, the set consisting of all the processes in the system. R = {R1, R2, …, Rm}, the set consisting of all resource types in the system. request edge – directed edge P1 → Rj assignment edge – directed edge Rj → Pi
  • 6. Rushdi Shams, Dept of CSE, KUET 6 Resource-Allocation Graph Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Pi Rj Rj
  • 7. Rushdi Shams, Dept of CSE, KUET 7 Resource Allocation Graph
  • 8. Rushdi Shams, Dept of CSE, KUET 8 Resource Allocation Graph With A Deadlock
  • 9. Rushdi Shams, Dept of CSE, KUET 9 Graph With A Cycle But No Deadlock
  • 10. Rushdi Shams, Dept of CSE, KUET 10 Resource Allocation Graph If graph contains no cycles then no deadlock. If graph contains a cycle then if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock
  • 11. Rushdi Shams, Dept of CSE, KUET 11 Methods for handling Deadlocks We can prevent deadlock- which is still a dream, but soon have a chance to be real We can avoid deadlocks- by using protocols, ensuring that the system will never enter a deadlock state We can allow the system to enter a deadlock state, detect it and recover We can use Ostrich Approach
  • 12. Rushdi Shams, Dept of CSE, KUET 12 Ostrich Approach Ostrich is not the name of the inventor of the approach, it is simply an ostrich. When ostriches run, they just run!  They pay very little interest in their surroundings. It means, Ostrich Algorithm in deadlocks is simply ignoring the problem… pretending it will never occur. UNIX runs with this approach.
  • 13. Rushdi Shams, Dept of CSE, KUET 13 Deadlock Prevention Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. Low resource utilization; starvation possible.
  • 14. Rushdi Shams, Dept of CSE, KUET 14 Deadlock Prevention No Preemption – • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • Preempted resources are added to the list of resources for which the process is waiting. • Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.
  • 15. Rushdi Shams, Dept of CSE, KUET 15 Deadlock Avoidance • Requires that the system has some additional a priori information available. Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.
  • 16. Rushdi Shams, Dept of CSE, KUET 16 Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes is the systems such that for each Pi, the resources that Pican still request can be satisfied by currently available resources + resources held by all the Pj, with j < i. That is: If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. When Pi terminates, Pi+1 can obtain its needed resources, and so on.
  • 17. Rushdi Shams, Dept of CSE, KUET 17 Basic Facts If a system is in safe state, then no deadlocks. If a system is in unsafe state, then possibility of deadlock. Avoidance means ensuring that a system will never enter an unsafe state.
  • 18. Rushdi Shams, Dept of CSE, KUET 18
  • 19. Rushdi Shams, Dept of CSE, KUET 19 Deadlock Avoidance: on Overhead Projector
  • 20. Rushdi Shams, Dept of CSE, KUET 20 Deadlock Detection & Recovery  If a system does not impose deadlock prevention and avoidance mechanism, then a deadlock situation MAY occur.  In this circumstance, the system must provide 1. An algorithm to detect deadlock 2. Recovery Mechanism
  • 21. Rushdi Shams, Dept of CSE, KUET 21 Detection Mechanism 1 We assume that the resource has only one instance. Maintain wait-for graph (WFG) Nodes are processes. Pi → Pj if Pi is waiting for Pj. Periodically searches for a cycle in the graph. If there is a cycle, there exists a deadlock.
  • 22. Rushdi Shams, Dept of CSE, KUET 22 Detection Mechanism 1
  • 23. Rushdi Shams, Dept of CSE, KUET 23 Detection Mechanism 2 We assume that resources have more than one instance Five processes P0 through P4;three resource types A (7 instances), B (2 instances), and C (6 instances). Snapshot at time T0: Allocation Request Available A B C A B C A B C P0 0 1 0 0 0 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2 Sequence <P0, P2, P3, P4, P1> will result in
  • 24. Rushdi Shams, Dept of CSE, KUET 24 Detection Algorithm Usage When should we invoke the detection algorithm? Depends on two factors- How often a deadlock is likely to occur? How many processes will be affected by deadlock when it happens? How many processes will be needed to roll back? Normally detection algorithm is used - Whenever a request is not served In every hour Whenever CPU utilization drops under 40%
  • 25. Rushdi Shams, Dept of CSE, KUET 25 Recovery Mechanism: Process Termination Abort all deadlocked process Clearly will break the cycle but at a great price Processes may have computed for a long time and now the values are going to be lost  Abort one process at a time until the deadlock cycle is eliminated Considerable overhead
  • 26. Rushdi Shams, Dept of CSE, KUET 26 Recovery Mechanism: Process Termination In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch?
  • 27. Rushdi Shams, Dept of CSE, KUET 27 Recovery Mechanism: Resource Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process for that state. Starvation – same process may always be picked as victim, include number of rollback in cost factor.
  • 28. Rushdi Shams, Dept of CSE, KUET 28 Reference Operating Systems Concept (6th Edition) by Silberschatz, Galvin and Gagne