SlideShare a Scribd company logo
1 of 11
Operating Systems
1
A Case Study
On
Deadlock
4th
CSE ‘S’
Prepared by
Yash P. Dhol
Asimhusen N.Saiyad
Jay Pandya
Submitted to
Prof. Nikul Virapariya
Operating Systems
2“Crises and deadlocks when they occur have at least this advantage that
they force us to think.”- Jawaharlal Nehru (1889 - 1964)
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”.
An example from US Kansas law:
“When two trains approach each other at a crossing, both shall come to a full
stop and neither shall start up again until the other has gone.”
For another example,
Two processes each want to record a scanned document on a CD.Process A
requests permission to use the scanner and is granted it.Process B is
programmed differently and requests the CD recorder first and is also granted
it.Now A asks for the CD recorder, but the request is denied until B releases
it.Unfortunately, instead of releasing the CD recorder B asks for the
scanner.At this point both processes are blocked and will remain so forever.
This situation is called a deadlock.
Deadlocks can occur in a variety of situations besides requesting dedicated
I/O devices.
For example If process A locks record R1 and process B locks record R2, and
then each process tries to lock the other one's record, we also have a
deadlock. Thus deadlocks can occur on hardware resources or on software
resources.
Preemptable and Nonpreemptable Resources
Resources come in two flavors: Preemptable and Nonpreemptable. A
preemptable resource is one that can be taken away from the process with no
ill effects. Memory is an example of a preemptable resource. On the other
hand, a nonpreemptable resource is one that cannot be taken away from
process (without causing ill effect). For example, CD resources are no
preemptable at an arbitrary moment. Reallocating resources can resolve
deadlocks that involve preemptable resources. Deadlocks that involve
nonpreemptable resources are difficult to deal with.
Necessary Conditions for Deadlock:
Mutual Exclusion Condition:
Explanation: At least one resource (thread) must be held in a non-shareable
mode, that is, only one process at a time claims exclusive control of the
resource.If another process requests that resource, the requesting process
must be delayed until the resource has been released.
Hold and Wait Condition:
Operating Systems
3
Explanation: There must exist a process that is holding a resource already
allocated to it while waiting for additional resource that are currently being
held by other processes.
No-Preemptive Condition:
Explanation: Resources cannot be removed from the processes are used to
completion or released voluntarily by the process holding it.
Circular Wait Condition:
Explanation: There exists a set {T1, …, Tn} of waiting threads
• T1 is waiting for a resource that is held by T2
• T2 is waiting for a resource that is held by T3
• …
• Tn is waiting for a resource that is held by T1
T1
T2
R2 R1
Operating Systems
4
That means no Deadlock occurs..
Operating Systems
5
Ways of Handling Deadlock:
• Deadlock Prevention
• Deadlock Detection
• Deadlock Recovery
• Deadlock Avoidance
Deadlock Prevention:
Remove the possibility of deadlock occurring by denying one of the four
necessary conditions:
– Mutual Exclusion (Can we share everything?)
Operating Systems
6
Removing the mutual exclusion condition means that no process
will have exclusive access to a resource.
– Hold & Wait
The hold & wait or resource holding conditions may be removed
by requiring processes to request all the resources they will need
before starting up.
– No preemption
The No preemption condition may also be difficult or impossible
to avoid as a process has to be able to have a resource for a
certain amount of time, or the processing outcome may be
inconsistent.
However, inability to enforce preemption may interfere with
a priority algorithm.
– Circular Wait
The final condition is the circular wait condition. Approaches that
avoid circular waits include disabling interrupts during critical
sections.
Deadlock Detection:
Deadlock Detection with One Resource of Each
Type
Operating Systems
7
1. Process A holds R and wants S.
2. Process B holds nothing but wants T.
3. Process C holds nothing but wants S.
4. Process D holds U and wants V.
5. Process E holds T and wants V.
6. Process F holds W and wants S.
7. Process G holds V wants U.
Deadlock Recovery:
• Recovery through preemption
– take a resource from some other process
• Recovery through rollback
– checkpoint a process periodically
– use this saved state
– restart the process if it is found deadlocked
• Recovery through killing processes
– crudest but simplest way to break a deadlock
– kill one of the processes in the deadlock cycle
– the other processes get its resources
Operating Systems
8
– choose process that can be rerun from the beginning
Deadlock Avoidance:
This approach to the deadlock problem anticipates deadlock before it actually
occurs.
This method differs from deadlock prevention, which guarantees that deadlock
cannot occur by denying one of the necessary conditions of deadlock.
If the necessary conditions for a deadlock are in place, it is still possible to
avoid deadlock by being careful when resources are allocated.
Perhaps the most famous deadlock avoidance algorithm, due to Dijkstra
[1965], is the Banker’s algorithm.
So named because the process is analogous to that used by a banker in
deciding if a loan can be safely made.
Safe State The key to a state being safe is that there is at least one way for all
users to finish. In other analogy, the state of figure 2 is safe because with 2
units left, the banker can delay any request except C's,
thus letting C finish and release all four resources. With four units in hand, the
banker can let either D or B have the necessary units and so on.
Customers Used Max
A 1 6
B 1 5
C 2 4
D 4 7
Available Units= 2
Operating Systems
9
Unsafe State Consider what would happen if a request from B for one more
unit were grantedin above We would have following situation
Available Units = 1
Customers Used Max
A 1 6
B 2 5
C 2 4
D 4 7
This is an unsafe state. If all the customers namely A, B, C, and D asked for
their maximum loans, then banker could not satisfy any of them and we would
have a deadlock.
• Three resource allocation states
– safe
– safe
– unsafe
Operating Systems
10
The Banker’s Algoritham for Multiple Resources:
E = (6, 3, 4, 2) P = (5, 3, 2, 2) A = (1, 0, 2, 0)
Notice that A = E - P
Going thru this algorithm with the foregoing data, we see that process D's
requirements are smaller than A, so we virtually terminate D and add its
resources back into the available pool:
E = (6, 3, 4, 2)
P = (5, 3, 2, 2) - (1, 1, 0, 1) = (4, 2, 2, 1)
A = (1, 0, 2, 0) + (1, 1, 0, 1) = (2, 1, 2, 1)
Now, A's requirements are less than A, so do the same thing with A:
P = (4, 2, 2, 1) - (3, 0, 1, 1) = (1, 2, 1, 0)
A = (2, 1, 2, 1) + (3, 0, 1, 1) = (5, 1, 3, 2)
At this point, we see that there are no remaining processes that can't be satisfied
from available resources, so the illustrated state is safe.
Operating Systems
11
Thank you

More Related Content

What's hot

Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Gyanmanjari Institute Of Technology
 
Distributed concurrency control
Distributed concurrency controlDistributed concurrency control
Distributed concurrency controlBinte fatima
 
Database , 12 Reliability
Database , 12 ReliabilityDatabase , 12 Reliability
Database , 12 ReliabilityAli Usman
 
Distributed system notes unit I
Distributed system notes unit IDistributed system notes unit I
Distributed system notes unit INANDINI SHARMA
 
Concurrency control
Concurrency controlConcurrency control
Concurrency controlvarsha singh
 
Consensus in distributed computing
Consensus in distributed computingConsensus in distributed computing
Consensus in distributed computingRuben Tan
 
file sharing semantics by Umar Danjuma Maiwada
file sharing semantics by Umar Danjuma Maiwada file sharing semantics by Umar Danjuma Maiwada
file sharing semantics by Umar Danjuma Maiwada umardanjumamaiwada
 
Deadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddikDeadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddikSaeed Siddik
 
Security in distributed systems
Security in distributed systems Security in distributed systems
Security in distributed systems Haitham Ahmed
 
Distributed shared memory shyam soni
Distributed shared memory shyam soniDistributed shared memory shyam soni
Distributed shared memory shyam soniShyam Soni
 
Overview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesOverview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesMeghaj Mallick
 
Concurrency control
Concurrency  controlConcurrency  control
Concurrency controlJaved Khan
 

What's hot (20)

Deadlock Presentation
Deadlock PresentationDeadlock Presentation
Deadlock Presentation
 
Data link layer
Data link layer Data link layer
Data link layer
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
 
Distributed concurrency control
Distributed concurrency controlDistributed concurrency control
Distributed concurrency control
 
Database , 12 Reliability
Database , 12 ReliabilityDatabase , 12 Reliability
Database , 12 Reliability
 
Distributed system notes unit I
Distributed system notes unit IDistributed system notes unit I
Distributed system notes unit I
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Consensus in distributed computing
Consensus in distributed computingConsensus in distributed computing
Consensus in distributed computing
 
file sharing semantics by Umar Danjuma Maiwada
file sharing semantics by Umar Danjuma Maiwada file sharing semantics by Umar Danjuma Maiwada
file sharing semantics by Umar Danjuma Maiwada
 
Deadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddikDeadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddik
 
Security in distributed systems
Security in distributed systems Security in distributed systems
Security in distributed systems
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Distributed shared memory shyam soni
Distributed shared memory shyam soniDistributed shared memory shyam soni
Distributed shared memory shyam soni
 
Overview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesOverview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed Databases
 
Operating system Deadlock
Operating system DeadlockOperating system Deadlock
Operating system Deadlock
 
Concurrency control
Concurrency  controlConcurrency  control
Concurrency control
 
Operating System: Deadlock
Operating System: DeadlockOperating System: Deadlock
Operating System: Deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 

Similar to Os case study word

Similar to Os case study word (20)

Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
 
Unit 3 part 2(DEADLOCK)
Unit 3 part 2(DEADLOCK)Unit 3 part 2(DEADLOCK)
Unit 3 part 2(DEADLOCK)
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Module3
Module3Module3
Module3
 
Deadlock
DeadlockDeadlock
Deadlock
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Deadlocks final
Deadlocks finalDeadlocks final
Deadlocks final
 
3.6 Deadlock-operating system unit 3.pptx
3.6 Deadlock-operating system unit 3.pptx3.6 Deadlock-operating system unit 3.pptx
3.6 Deadlock-operating system unit 3.pptx
 
OS 7.pptx
OS 7.pptxOS 7.pptx
OS 7.pptx
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Deadlock and memory management -- Operating System
Deadlock and memory management -- Operating SystemDeadlock and memory management -- Operating System
Deadlock and memory management -- Operating System
 
Chapter06
Chapter06Chapter06
Chapter06
 
Deadlocks by wani zahoor
Deadlocks by wani zahoorDeadlocks by wani zahoor
Deadlocks by wani zahoor
 
operating system
operating systemoperating system
operating system
 
operating system
operating systemoperating system
operating system
 
Deadlocks prefinal
Deadlocks prefinalDeadlocks prefinal
Deadlocks prefinal
 
Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456
 

Recently uploaded

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 

Recently uploaded (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 

Os case study word

  • 1. Operating Systems 1 A Case Study On Deadlock 4th CSE ‘S’ Prepared by Yash P. Dhol Asimhusen N.Saiyad Jay Pandya Submitted to Prof. Nikul Virapariya
  • 2. Operating Systems 2“Crises and deadlocks when they occur have at least this advantage that they force us to think.”- Jawaharlal Nehru (1889 - 1964) 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”. An example from US Kansas law: “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.” For another example, Two processes each want to record a scanned document on a CD.Process A requests permission to use the scanner and is granted it.Process B is programmed differently and requests the CD recorder first and is also granted it.Now A asks for the CD recorder, but the request is denied until B releases it.Unfortunately, instead of releasing the CD recorder B asks for the scanner.At this point both processes are blocked and will remain so forever. This situation is called a deadlock. Deadlocks can occur in a variety of situations besides requesting dedicated I/O devices. For example If process A locks record R1 and process B locks record R2, and then each process tries to lock the other one's record, we also have a deadlock. Thus deadlocks can occur on hardware resources or on software resources. Preemptable and Nonpreemptable Resources Resources come in two flavors: Preemptable and Nonpreemptable. A preemptable resource is one that can be taken away from the process with no ill effects. Memory is an example of a preemptable resource. On the other hand, a nonpreemptable resource is one that cannot be taken away from process (without causing ill effect). For example, CD resources are no preemptable at an arbitrary moment. Reallocating resources can resolve deadlocks that involve preemptable resources. Deadlocks that involve nonpreemptable resources are difficult to deal with. Necessary Conditions for Deadlock: Mutual Exclusion Condition: Explanation: At least one resource (thread) must be held in a non-shareable mode, that is, only one process at a time claims exclusive control of the resource.If another process requests that resource, the requesting process must be delayed until the resource has been released. Hold and Wait Condition:
  • 3. Operating Systems 3 Explanation: There must exist a process that is holding a resource already allocated to it while waiting for additional resource that are currently being held by other processes. No-Preemptive Condition: Explanation: Resources cannot be removed from the processes are used to completion or released voluntarily by the process holding it. Circular Wait Condition: Explanation: There exists a set {T1, …, Tn} of waiting threads • T1 is waiting for a resource that is held by T2 • T2 is waiting for a resource that is held by T3 • … • Tn is waiting for a resource that is held by T1 T1 T2 R2 R1
  • 4. Operating Systems 4 That means no Deadlock occurs..
  • 5. Operating Systems 5 Ways of Handling Deadlock: • Deadlock Prevention • Deadlock Detection • Deadlock Recovery • Deadlock Avoidance Deadlock Prevention: Remove the possibility of deadlock occurring by denying one of the four necessary conditions: – Mutual Exclusion (Can we share everything?)
  • 6. Operating Systems 6 Removing the mutual exclusion condition means that no process will have exclusive access to a resource. – Hold & Wait The hold & wait or resource holding conditions may be removed by requiring processes to request all the resources they will need before starting up. – No preemption The No preemption condition may also be difficult or impossible to avoid as a process has to be able to have a resource for a certain amount of time, or the processing outcome may be inconsistent. However, inability to enforce preemption may interfere with a priority algorithm. – Circular Wait The final condition is the circular wait condition. Approaches that avoid circular waits include disabling interrupts during critical sections. Deadlock Detection: Deadlock Detection with One Resource of Each Type
  • 7. Operating Systems 7 1. Process A holds R and wants S. 2. Process B holds nothing but wants T. 3. Process C holds nothing but wants S. 4. Process D holds U and wants V. 5. Process E holds T and wants V. 6. Process F holds W and wants S. 7. Process G holds V wants U. Deadlock Recovery: • Recovery through preemption – take a resource from some other process • Recovery through rollback – checkpoint a process periodically – use this saved state – restart the process if it is found deadlocked • Recovery through killing processes – crudest but simplest way to break a deadlock – kill one of the processes in the deadlock cycle – the other processes get its resources
  • 8. Operating Systems 8 – choose process that can be rerun from the beginning Deadlock Avoidance: This approach to the deadlock problem anticipates deadlock before it actually occurs. This method differs from deadlock prevention, which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock. If the necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by being careful when resources are allocated. Perhaps the most famous deadlock avoidance algorithm, due to Dijkstra [1965], is the Banker’s algorithm. So named because the process is analogous to that used by a banker in deciding if a loan can be safely made. Safe State The key to a state being safe is that there is at least one way for all users to finish. In other analogy, the state of figure 2 is safe because with 2 units left, the banker can delay any request except C's, thus letting C finish and release all four resources. With four units in hand, the banker can let either D or B have the necessary units and so on. Customers Used Max A 1 6 B 1 5 C 2 4 D 4 7 Available Units= 2
  • 9. Operating Systems 9 Unsafe State Consider what would happen if a request from B for one more unit were grantedin above We would have following situation Available Units = 1 Customers Used Max A 1 6 B 2 5 C 2 4 D 4 7 This is an unsafe state. If all the customers namely A, B, C, and D asked for their maximum loans, then banker could not satisfy any of them and we would have a deadlock. • Three resource allocation states – safe – safe – unsafe
  • 10. Operating Systems 10 The Banker’s Algoritham for Multiple Resources: E = (6, 3, 4, 2) P = (5, 3, 2, 2) A = (1, 0, 2, 0) Notice that A = E - P Going thru this algorithm with the foregoing data, we see that process D's requirements are smaller than A, so we virtually terminate D and add its resources back into the available pool: E = (6, 3, 4, 2) P = (5, 3, 2, 2) - (1, 1, 0, 1) = (4, 2, 2, 1) A = (1, 0, 2, 0) + (1, 1, 0, 1) = (2, 1, 2, 1) Now, A's requirements are less than A, so do the same thing with A: P = (4, 2, 2, 1) - (3, 0, 1, 1) = (1, 2, 1, 0) A = (2, 1, 2, 1) + (3, 0, 1, 1) = (5, 1, 3, 2) At this point, we see that there are no remaining processes that can't be satisfied from available resources, so the illustrated state is safe.