SlideShare a Scribd company logo
Deadlocks
A deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
Mahershi Bhavsar
160160107005
Operating Systems
Necessary Conditions
A deadlock situation can arise if the following conditions hold simultaneously in a system.
• Mutual Exclusion:
• Hold and Wait:
• No Pre-emption:
• Circular Wait:
At least one resource must be held in a non sharable mode, that is, only one process at
a time can access the resource.
A process must be holding at least one resource while waiting to acquire additional
resource.
Resources cannot be pre-empted. A resource can only be released voluntarily by the
process holding it.
A set of waiting processes must exist such that each process is waiting for a resource
already acquired by some other process in the set.
If all the four conditions holds true, deadlock occurs.
Resource Allocation Graph
A resource allocation graph tracks which resource is hold by what process and which process is
waiting for a resource of a particular type. It is a very powerful and simple tool to illustrate how a set of
processes can deadlock.
• If a process is using a resource, an arrow is drawn from the resource
node to the process node.
• If a process is requesting a resource, an arrow is drawn from the process
node to resource node.
Resource Allocation Graph
If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one
instance, then the process will deadlock.
In the given example, a deadlock occurs.
Process 1 waits for a resource already held by Process 3 and
Process 3 waits for a resource held by Process 2. Further,
Process 2 is waiting for a resource held by Process 1.
In the given example, only one instance of each resource is
considered. In real life there may be more than one instances
and the situation completely differs.
Methods for Handling Deadlocks
Deadlocks can be dealt with by one of the three ways stated below:
1. Using a protocol to ensure never entering a deadlock i.e. deadlock avoidance or prevention.
2. Detect and recover from a deadlocked situation.
3. Pretension. Pretend that deadlocks never occur in the system. (More like faith in God)
Deadlock Prevention
Deadlock can be prevented by preventing any one of the four necessary conditions for deadlock from
holding true.
1. Mutual Exclusion:
2. Hold and Wait:
It is not possible to prevent mutual exclusion because some resource needs to be non –
sharable, such as printer.
To ensure this condition doesn’t hold true, we must make sure a process doesn’t hold a
resource while requesting another. All the required resources must be requested and
allocated before the execution of the process begins. This also leads to wastage of
resources that remain idle until used but yet already allocated. Another protocol allows a
process to request resources only when it has none. Before a process requests
additional resources, it must release all currently allocated resources.
3. No Pre-emption:
4. Circular Wait:
To prevent this condition, if a process is holding some resource and requests additional
resources that cannot be immediately delivered, then all the resources being currently
held are pre-empted. These pre-empted resources are added to the list of resources
the process is waiting for. This protocol is often applied to resources whose state can
be easily saved and restored later.
Each resource will be assigned a number. A process can request resources only in the
increasing order or numbering. This ensures no request leads to a loop in the resource
allocation graph.
Deadlock Avoidance
Deadlock prevention is an efficient way to prevent deadlock but not efficient utilization of resources. It
leads to low device utilization and reduced system throughput.
An alternative method is to avoid deadlocks by having additional information of resources requested
and allocated. The simplest and most useful model requires each process to state maximum number
of resources of each type will it require.
Safe State:
A state of system is safe if it can allocate resource to each process in a certain order and still prevent
a deadlock.
Banker’s Algorithm is used to avoid deadlock. It confirms the system to be in safe system if the
requested resources are allocated.
Banker’s Algorithm
• An algorithm to avoid deadlocks
• When a process enters the system, it must declare the maximum instances of each type of
resource it may need. This number may not exceed the total number of resources of a particular
type in the system.
• Several data structures are maintained to implement this algorithm.
1. Available:
2. Max:
A vector of length m indicates the number of available resources. (A vector is a container type
in programming languages, it basically acts as dynamic array).
A v a i l a b l e [ j ] = k
k instances of resource type Rj are available.
A matrix (n x m) that defines maximum demand of each process.
M a x [ i ] [ j ] = k
Process Pi may request at most k instances of Rj
3. Allocation:
4. Max:
A matrix (n x m) that defines the number of allocation of each type of resource currently
allocated to a process.
A l l o c a t i o n [ i ] [ j ] = k
k instances of Rj are allocated to Pi
A matrix (n x m) that defines remaining resource need of each process.
N e e d [ i ] [ j ] = k
Pi may need k instances if Rj
Request Resource Algorithm:
1. If R e q u e s t i ≤ N e e d i , go to step 2. Otherwise, process exceeded its maximum claim.
2. If R e q u e s t i ≤ A v a i l a b l e i , go to step 3. Otherwise, Pi must wait, since resources are not available.
3. Pretend to allocate resource to check for resulting state.
A v a i l a b l e = A v a i l a b l e – R e q u e s t i
A l l o c a t i o n i = A l l o c a t i o n i + R e q u e s t
N e e d i = N e e d i – R e q u e s t i
If the resulting resource state is safe, the transaction is completed.
Process Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Illustration of Banker’s Algorithm
Let the system currently be in the state shown. Suppose now P1 requests one additional instance of resource A
and two additional instances of resource C, so R e q u e s t i = ( 1 , 0 , 2 ) .
We first check that R e q u e s t i ≤ A v a i l a b l e , that is ( 1 , 0 , 2 ) ≤ ( 3 , 3 , 2 ) which is true. We then pretend to
allocate the requested resources leading to a new state as follows
The obtained state is safe, hence we can immediately grant the newly requested resources to the process.
However a further request of (3,3,0) by P4 cannot be granted, since the resources are not available.
Safe State is determined by viewing all positive values in Available column.
Process Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Deadlock Detection
If a system does not provide deadlock avoidance or deadlock prevention, it must provide
• An algorithm to examine the state of the system, deadlock detection.
• An algorithm to recover from deadlock.
Deadlock can be easily detected using the resource allocation graph. If a single instance of resources are present,
deadlock can be detected by a presence of a cycle in the resource allocation graph.
For a system with several instances of a resource type, this method is not sufficient. An algorithm is designed to
detect deadlock in system with multiple instances of resources of a type.
The algorithm is similar to Banker’s Algorithm. It uses several data structures such as Available, Allocation and
Request.
The working of algorithm is shown below.
1. Let Work and Finish be vectors of length m and n respectively.
Work = Available. (initialization)
if A llocation i ≠ 0 , F in ish [i] = false
E lse, F in ish [i] = tru e
2. Find an index i such that
Finish[i] == false
Req u est i ≤ Work
if no such I exists, go to step 4.
3 . Work = Work + A llocation i
F in ish [i] = tru e
Go to Step 2.
4. If F in ish [i] == false , then the system is in a deadlocked state.
Deadlock Recovery
When a detection algorithm detects a deadlock, deadlock is manually or automatically handled to recover the safe
state of the system. There are two options of breaking a deadlock, abort one or more processes or to pre-empt
some resources.
Process Termination
Abort all deadlocked processes:
This method does break the deadlock but with a great expense. This method involves terminating all the processes
involved in the deadlock state.
Abort one process at a time until deadlock cycle is eliminated:
This method is more efficient than terminating all the processes. A deadlock detection algorithm is implemented to
determine which process to be terminated. Processes whose termination incurs minimum cost are terminated.
Resource Pre-emption
Successive pre-emption of resources to deal with deadlock. Three issues needs to be addressed in
this method.
1. Selecting a victim:
2. Rollback:
3. Starvation:
Selection of the resource to be pre-empted. Also involves determining the order of pre-
emption to minimize the cost. Cost factor includes various parameters such as number
of resources a deadlocked process holds or the mount of time the process has
consumed so far.
A process whose resource are pre-empted is rolled back up to a safe state from where
it can restart its execution.
Selecting victim based on cost parameters lead to starvation of the most expensive
process as it is always picked as the victim. The solution to prevent starvation is to
include number of roll backs for a process in the cost factor. A process must be chosen
as victim accordingly.
Mahershi Bhavsar
160160107005
Operating Systems

More Related Content

PPTX
Deadlock and Banking Algorithm
PDF
Deadlock in Distributed Systems
PDF
Deadlock in distribute system by saeed siddik
PDF
Sucet os module_3_notes
PDF
Methods for handling deadlocks
PPTX
Chapter 4
PDF
The implementation of Banker's algorithm, data structure and its parser
PPTX
Deadlock
Deadlock and Banking Algorithm
Deadlock in Distributed Systems
Deadlock in distribute system by saeed siddik
Sucet os module_3_notes
Methods for handling deadlocks
Chapter 4
The implementation of Banker's algorithm, data structure and its parser
Deadlock

What's hot (20)

PDF
Deadlock
PPTX
Deadlock detection & prevention
PPT
Chapter 7 - Deadlocks
PPTX
Deadlock avoidance and prevention .. computer networking
PPTX
Gp1242 007 oer ppt
PDF
DOC
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
PPTX
Deadlocks in operating system
PPT
Mch7 deadlock
PPT
Operating System Deadlock Galvin
PPT
Deadlock
PPT
Mca ii os u-3 dead lock & io systems
PPTX
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
PDF
Distributed deadlock
PPTX
Operating system Dead lock
PPTX
Deadlocks
PPT
Os module 2 d
PPTX
Operating system - Deadlock
Deadlock
Deadlock detection & prevention
Chapter 7 - Deadlocks
Deadlock avoidance and prevention .. computer networking
Gp1242 007 oer ppt
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlocks in operating system
Mch7 deadlock
Operating System Deadlock Galvin
Deadlock
Mca ii os u-3 dead lock & io systems
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Distributed deadlock
Operating system Dead lock
Deadlocks
Os module 2 d
Operating system - Deadlock
Ad

Similar to Deadlock (20)

PPTX
7308346-Deadlock.pptx
PDF
Chapter 5(five).pdf
PPT
Principles of Operating system and types
PPTX
Module 3 Deadlocks.pptx
PPT
Lecture 8 - Memory Management. It deals with memory management
PPT
Operating System
PPTX
deadlocks.pptx
PPT
14th November - Deadlock Prevention, Avoidance.ppt
PPT
A ppt on deadlock in operating systems for the better explanation
PPTX
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
PPTX
OSLec14&15(Deadlocksinopratingsystem).pptx
PDF
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
PPT
Deadlocks
PDF
deadlocks for Engenerring for he purpose
PPTX
Methods for handling deadlock
PPT
DeadlockMar21.ppt
PDF
CH07.pdf
PDF
Ch7 deadlocks
PPT
Dead Lock
PPTX
Deadlock - An Operating System Concept.pptx
7308346-Deadlock.pptx
Chapter 5(five).pdf
Principles of Operating system and types
Module 3 Deadlocks.pptx
Lecture 8 - Memory Management. It deals with memory management
Operating System
deadlocks.pptx
14th November - Deadlock Prevention, Avoidance.ppt
A ppt on deadlock in operating systems for the better explanation
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
OSLec14&15(Deadlocksinopratingsystem).pptx
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
Deadlocks
deadlocks for Engenerring for he purpose
Methods for handling deadlock
DeadlockMar21.ppt
CH07.pdf
Ch7 deadlocks
Dead Lock
Deadlock - An Operating System Concept.pptx
Ad

Recently uploaded (20)

PPTX
web development for engineering and engineering
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
Project quality management in manufacturing
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
PPT on Performance Review to get promotions
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
web development for engineering and engineering
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Lesson 3_Tessellation.pptx finite Mathematics
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Project quality management in manufacturing
UNIT 4 Total Quality Management .pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Structs to JSON How Go Powers REST APIs.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CH1 Production IntroductoryConcepts.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Construction Project Organization Group 2.pptx
PPT on Performance Review to get promotions
Strings in CPP - Strings in C++ are sequences of characters used to store and...

Deadlock

  • 1. Deadlocks A deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process. Mahershi Bhavsar 160160107005 Operating Systems
  • 2. Necessary Conditions A deadlock situation can arise if the following conditions hold simultaneously in a system. • Mutual Exclusion: • Hold and Wait: • No Pre-emption: • Circular Wait: At least one resource must be held in a non sharable mode, that is, only one process at a time can access the resource. A process must be holding at least one resource while waiting to acquire additional resource. Resources cannot be pre-empted. A resource can only be released voluntarily by the process holding it. A set of waiting processes must exist such that each process is waiting for a resource already acquired by some other process in the set. If all the four conditions holds true, deadlock occurs.
  • 3. Resource Allocation Graph A resource allocation graph tracks which resource is hold by what process and which process is waiting for a resource of a particular type. It is a very powerful and simple tool to illustrate how a set of processes can deadlock. • If a process is using a resource, an arrow is drawn from the resource node to the process node. • If a process is requesting a resource, an arrow is drawn from the process node to resource node. Resource Allocation Graph
  • 4. If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one instance, then the process will deadlock. In the given example, a deadlock occurs. Process 1 waits for a resource already held by Process 3 and Process 3 waits for a resource held by Process 2. Further, Process 2 is waiting for a resource held by Process 1. In the given example, only one instance of each resource is considered. In real life there may be more than one instances and the situation completely differs.
  • 5. Methods for Handling Deadlocks Deadlocks can be dealt with by one of the three ways stated below: 1. Using a protocol to ensure never entering a deadlock i.e. deadlock avoidance or prevention. 2. Detect and recover from a deadlocked situation. 3. Pretension. Pretend that deadlocks never occur in the system. (More like faith in God)
  • 6. Deadlock Prevention Deadlock can be prevented by preventing any one of the four necessary conditions for deadlock from holding true. 1. Mutual Exclusion: 2. Hold and Wait: It is not possible to prevent mutual exclusion because some resource needs to be non – sharable, such as printer. To ensure this condition doesn’t hold true, we must make sure a process doesn’t hold a resource while requesting another. All the required resources must be requested and allocated before the execution of the process begins. This also leads to wastage of resources that remain idle until used but yet already allocated. Another protocol allows a process to request resources only when it has none. Before a process requests additional resources, it must release all currently allocated resources.
  • 7. 3. No Pre-emption: 4. Circular Wait: To prevent this condition, if a process is holding some resource and requests additional resources that cannot be immediately delivered, then all the resources being currently held are pre-empted. These pre-empted resources are added to the list of resources the process is waiting for. This protocol is often applied to resources whose state can be easily saved and restored later. Each resource will be assigned a number. A process can request resources only in the increasing order or numbering. This ensures no request leads to a loop in the resource allocation graph.
  • 8. Deadlock Avoidance Deadlock prevention is an efficient way to prevent deadlock but not efficient utilization of resources. It leads to low device utilization and reduced system throughput. An alternative method is to avoid deadlocks by having additional information of resources requested and allocated. The simplest and most useful model requires each process to state maximum number of resources of each type will it require. Safe State: A state of system is safe if it can allocate resource to each process in a certain order and still prevent a deadlock. Banker’s Algorithm is used to avoid deadlock. It confirms the system to be in safe system if the requested resources are allocated.
  • 9. Banker’s Algorithm • An algorithm to avoid deadlocks • When a process enters the system, it must declare the maximum instances of each type of resource it may need. This number may not exceed the total number of resources of a particular type in the system. • Several data structures are maintained to implement this algorithm. 1. Available: 2. Max: A vector of length m indicates the number of available resources. (A vector is a container type in programming languages, it basically acts as dynamic array). A v a i l a b l e [ j ] = k k instances of resource type Rj are available. A matrix (n x m) that defines maximum demand of each process. M a x [ i ] [ j ] = k Process Pi may request at most k instances of Rj
  • 10. 3. Allocation: 4. Max: A matrix (n x m) that defines the number of allocation of each type of resource currently allocated to a process. A l l o c a t i o n [ i ] [ j ] = k k instances of Rj are allocated to Pi A matrix (n x m) that defines remaining resource need of each process. N e e d [ i ] [ j ] = k Pi may need k instances if Rj
  • 11. Request Resource Algorithm: 1. If R e q u e s t i ≤ N e e d i , go to step 2. Otherwise, process exceeded its maximum claim. 2. If R e q u e s t i ≤ A v a i l a b l e i , go to step 3. Otherwise, Pi must wait, since resources are not available. 3. Pretend to allocate resource to check for resulting state. A v a i l a b l e = A v a i l a b l e – R e q u e s t i A l l o c a t i o n i = A l l o c a t i o n i + R e q u e s t N e e d i = N e e d i – R e q u e s t i If the resulting resource state is safe, the transaction is completed.
  • 12. Process Allocation Max Available Need A B C A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 7 4 3 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 Illustration of Banker’s Algorithm Let the system currently be in the state shown. Suppose now P1 requests one additional instance of resource A and two additional instances of resource C, so R e q u e s t i = ( 1 , 0 , 2 ) . We first check that R e q u e s t i ≤ A v a i l a b l e , that is ( 1 , 0 , 2 ) ≤ ( 3 , 3 , 2 ) which is true. We then pretend to allocate the requested resources leading to a new state as follows
  • 13. The obtained state is safe, hence we can immediately grant the newly requested resources to the process. However a further request of (3,3,0) by P4 cannot be granted, since the resources are not available. Safe State is determined by viewing all positive values in Available column. Process Allocation Max Available Need A B C A B C A B C A B C P0 0 1 0 7 5 3 2 3 0 7 4 3 P1 3 0 2 3 2 2 0 2 0 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1
  • 14. Deadlock Detection If a system does not provide deadlock avoidance or deadlock prevention, it must provide • An algorithm to examine the state of the system, deadlock detection. • An algorithm to recover from deadlock. Deadlock can be easily detected using the resource allocation graph. If a single instance of resources are present, deadlock can be detected by a presence of a cycle in the resource allocation graph. For a system with several instances of a resource type, this method is not sufficient. An algorithm is designed to detect deadlock in system with multiple instances of resources of a type. The algorithm is similar to Banker’s Algorithm. It uses several data structures such as Available, Allocation and Request.
  • 15. The working of algorithm is shown below. 1. Let Work and Finish be vectors of length m and n respectively. Work = Available. (initialization) if A llocation i ≠ 0 , F in ish [i] = false E lse, F in ish [i] = tru e 2. Find an index i such that Finish[i] == false Req u est i ≤ Work if no such I exists, go to step 4. 3 . Work = Work + A llocation i F in ish [i] = tru e Go to Step 2. 4. If F in ish [i] == false , then the system is in a deadlocked state.
  • 16. Deadlock Recovery When a detection algorithm detects a deadlock, deadlock is manually or automatically handled to recover the safe state of the system. There are two options of breaking a deadlock, abort one or more processes or to pre-empt some resources.
  • 17. Process Termination Abort all deadlocked processes: This method does break the deadlock but with a great expense. This method involves terminating all the processes involved in the deadlock state. Abort one process at a time until deadlock cycle is eliminated: This method is more efficient than terminating all the processes. A deadlock detection algorithm is implemented to determine which process to be terminated. Processes whose termination incurs minimum cost are terminated.
  • 18. Resource Pre-emption Successive pre-emption of resources to deal with deadlock. Three issues needs to be addressed in this method. 1. Selecting a victim: 2. Rollback: 3. Starvation: Selection of the resource to be pre-empted. Also involves determining the order of pre- emption to minimize the cost. Cost factor includes various parameters such as number of resources a deadlocked process holds or the mount of time the process has consumed so far. A process whose resource are pre-empted is rolled back up to a safe state from where it can restart its execution. Selecting victim based on cost parameters lead to starvation of the most expensive process as it is always picked as the victim. The solution to prevent starvation is to include number of roll backs for a process in the cost factor. A process must be chosen as victim accordingly.