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

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

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