Unit 4 Deadlocks
Deadlock :
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. A Deadlock is a situation where each of the computer process
waits for a resource which is being assigned to some another process. In this situation, none of the process gets
executed since the resource it needs, is held by some other process which is also waiting for some other resource to be
released.
A process in operating system uses resources in the following way.
1. Requests a resource
2. Use the resource
3. Releases the resource
Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions)
Mutual Exclusion: Two or more resources are non-shareable (Only one process can use at a time)
Hold and Wait: A process is holding at least one resource and waiting for resources.
No Preemption: A resource cannot be taken from a process unless the process releases the resource.
Circular Wait: A set of processes waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance:
Prevention:
The idea is to not let the system into a deadlock state. This system will make sure that above
mentioned four conditions will not arise. These techniques are very costly so we use this in cases
where our priority is making a system deadlock-free.
One can zoom into each category individually, Prevention is done by negating one of the above-
mentioned necessary conditions for deadlock. Prevention can be done in four different ways:
1. Eliminate mutual exclusion 3. Allow preemption
2. Solve hold and Wait 4. Circular wait Solution
Avoidance:
Avoidance is kind of futuristic. By using the strategy of “Avoidance”, we have to make an assumption.
We need to ensure that all information about resources that the process will need is known to us
before the execution of the process. We use Banker’s algorithm (Which is in turn a gift from Dijkstra) to
avoid deadlock.
In prevention and avoidance, we get the correctness of data but performance decreases.
2) Deadlock detection and recovery: If Deadlock prevention or avoidance is not applied to the software then we can
handle this by deadlock detection and recovery. which consist of two phases:
1. In the first phase, we examine the state of the process and check whether there is a deadlock or not in the
system.
2. If found deadlock in the first phase then we apply the algorithm for recovery of the deadlock.
In Deadlock detection and recovery, we get the correctness of data but performance decreases.
Recovery from Deadlock
1. Manual Intervention:
When a deadlock is detected, one option is to inform the operator and let them handle the situation manually. While
this approach allows for human judgment and decision-making, it can be time-consuming and may not be feasible in
large-scale systems.
2. Automatic Recovery:
An alternative approach is to enable the system to recover from deadlock automatically. This method involves breaking
the deadlock cycle by either aborting processes or preempting resources. Let’s delve into these strategies in more
detail.
Recovery from Deadlock: Process Termination:
1. Abort all deadlocked processes:
This approach breaks the deadlock cycle, but it comes at a significant cost. The processes that were aborted may
have executed for a considerable amount of time, resulting in the loss of partial computations. These computations
may need to be recomputed later.
2. Abort one process at a time:
Instead of aborting all deadlocked processes simultaneously, this strategy involves selectively aborting one process
at a time until the deadlock cycle is eliminated. However, this incurs overhead as a deadlock-detection algorithm
must be invoked after each process termination to determine if any processes are still deadlocked.
Factors for choosing the termination order:
– The process’s priority
– Completion time and the progress made so far
– Resources consumed by the process
– Resources required to complete the process
– Number of processes to be terminated
– Process type (interactive or batch)
Recovery from Deadlock: Resource Preemption:
1. Selecting a victim:
Resource preemption involves choosing which resources and processes should be preempted to break the deadlock. The selection order aims
to minimize the overall cost of recovery. Factors considered for victim selection may include the number of resources held by a deadlocked
process and the amount of time the process has consumed.
2. Rollback:
If a resource is preempted from a process, the process cannot continue its normal execution as it lacks the required resource. Rolling back the
process to a safe state and restarting it is a common approach. Determining a safe state can be challenging, leading to the use of total
rollback, where the process is aborted and restarted from scratch.
3. Starvation prevention:
To prevent resource starvation, it is essential to ensure that the same process is not always chosen as a victim. If victim selection is solely
based on cost factors, one process might repeatedly lose its resources and never complete its designated task. To address this, it is advisable
to limit the number of times a process can be chosen as a victim, including the number of rollbacks in the cost factor.
3) Deadlock ignorance: If a deadlock is very rare, then let it happen and reboot the system. This is the approach that both Windows and UNIX
take. we use the ostrich algorithm for deadlock ignorance.
In Deadlock, ignorance performance is better than the above two methods but the correctness of data.
Safe State:
A safe state can be defined as a state in which there is no deadlock. It is achievable if:
● If a process needs an unavailable resource, it may wait until the same has been released by a process to which it has already been
allocated. if such a sequence does not exist, it is an unsafe state.
● All the requested resources are allocated to the process.
What is Distributed System Deadlocks?
Deadlocks in distributed systems are similar to deadlocks in single-processor systems. They are not easy
to avoid, prevent, detect, and harder to cure when tracked down because all the relevant information is
scattered over many machines. Some systems, such as distributed database systems, can be extremely
serious, so it is important to understand how they differ from ordinary deadlocks.
There are two kinds of distributed deadlocks, communication deadlocks, and resource deadlocks. A
communication deadlock occurs when process A is trying to send a message to process B, which is trying
to send one to process C, which is trying to send one to A. There are various scenarios in which this
situation leads to a deadlock, such as no buffers being available. A resource deadlock occurs when
processes fight exclusive access to I/O devices, files, locks, or other resources.
Various strategies are used to handle deadlocks. Dealing with deadlocks can be done in the following
ways, such as:
1. Ignore the problem
2. Detection
3. Prevention
4. Avoidance
Resources Deadlock
In resource deadlocks, processes can simultaneously wait for several resources and cannot proceed until they have acquired
all those resources. A set of processes is resource deadlocked if each process in the set requests resources held by another
process in the set, and it must receive all of the requested resources before it can become unblocked. This is called resource
deadlock.
For example, suppose a system is running 2 processes, P1 & P2, and they want the resources R1 & R2. The resource allocation
graph is shown below:
But here, the P1 process holds resource R1 & waits to get R2 resource while
process P2 holds resource R2 & waits to get R1 resource. In this way, P1
waits for P2 to complete & P2 waits for P1 to get complete &
hence a deadlock.
What is Communication Deadlocks?
Processes in this deadlock wait to communicate with other processes in a group of processes. A waiting process can
unblock on receiving communication from any of these processes. If each process in the set is waiting to communicate
with another process in the set, and no process in the set ever begins any additional communication until it receives the
communication for which it is waiting, the set is communication-Deadlocked.
For Example: Suppose, process A waits to get a message from process B, Process B waits to get a message from process
C, and Process C waits to get a message from process A and hence a deadlock. The TFW diagram is shown below:
Users in a distributed system (DDBS) access the database's data objects by executing transactions. A transaction can be
thought of as a series of reads and writes performed on data objects. A database's data objects can be considered
resources that are acquired (through locking) and released (through unlocking) by transactions. In DDBS, wait for the
graph is referred to as transaction-wait-for-graph (TWF Graph)
Difference between Resource Deadlock and Communication Deadlock
Deadlock detection and recovery is the process of detecting and resolving deadlocks in an operating system. A deadlock
occurs when two or more processes are blocked, waiting for each other to release the resources they need. This can
lead to a system-wide stall, where no process can make progress.
There are two main approaches to deadlock detection and recovery:
1. Prevention: The operating system takes steps to prevent deadlocks from occurring by ensuring that the
system is always in a safe state, where deadlocks cannot occur. This is achieved through resource allocation
algorithms such as the Banker’s Algorithm.
2. Detection and Recovery: If deadlocks do occur, the operating system must detect and resolve them. Deadlock
detection algorithms, such as the Wait-For Graph, are used to identify deadlocks, and recovery algorithms,
such as the Rollback and Abort algorithm, are used to resolve them. The recovery algorithm releases the
resources held by one or more processes, allowing the system to continue to make progress.
Deadlock Detection :
1. If resources have a single instance –
In this case for Deadlock detection, we can run an algorithm to check for the cycle
in the Resource Allocation Graph. The presence of a cycle in the graph is a
sufficient condition for deadlock.
In the above diagram, resource 1 and resource 2 have single instances. There is a cycle R1 P1 R2 P2. So,
→ → →
Deadlock is Confirmed.
2. If there are multiple instances of resources –
Detection of the cycle is necessary but not a sufficient condition for deadlock detection, in this case, the system may
or may not be in deadlock varies according to different situations.
3. Wait-For Graph Algorithm –
The Wait-For Graph Algorithm is a deadlock detection algorithm used to detect deadlocks in a system where
resources can have multiple instances. The algorithm works by constructing a Wait-For Graph, which is a directed
graph that represents the dependencies between processes and resources.
Deadlock Recovery :
A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is a time and space-consuming
process. Real-time operating systems use Deadlock recovery.
1. Killing the process –
Killing all the processes involved in the deadlock. Killing process one by one. After killing each process check for
deadlock again and keep repeating the process till the system recovers from deadlock. Killing all the processes one by
one helps a system to break circular wait conditions.
2. Resource Preemption –
Resources are preempted from the processes involved in the deadlock, and preempted resources are allocated to other
processes so that there is a possibility of recovering the system from the deadlock. In this case, the system goes into
starvation.
3. Concurrency Control – Concurrency control mechanisms are used to prevent data inconsistencies in systems with
multiple concurrent processes. These mechanisms ensure that concurrent processes do not access the same data at the
same time, which can lead to inconsistencies and errors. Deadlocks can occur in concurrent systems when two or more
processes are blocked, waiting for each other to release the resources they need. This can result in a system-wide stall,
where no process can make progress. Concurrency control mechanisms can help prevent deadlocks by managing
access to shared resources and ensuring that concurrent processes do not interfere with each other.
Advantages of Deadlock Detection and Recovery in Operating Systems:
1. Improved System Stability: Deadlocks can cause system-wide stalls, and detecting and resolving deadlocks can help to
improve the stability of the system.
2. Better Resource Utilization: By detecting and resolving deadlocks, the operating system can ensure that resources are
efficiently utilized and that the system remains responsive to user requests.
3. Better System Design: Deadlock detection and recovery algorithms can provide insight into the behavior of the system and
the relationships between processes and resources, helping to inform and improve the design of the system.
Disadvantages of Deadlock Detection and Recovery in Operating Systems:
4. Performance Overhead: Deadlock detection and recovery algorithms can introduce a significant overhead in terms of
performance, as the system must regularly check for deadlocks and take appropriate action to resolve them.
5. Complexity: Deadlock detection and recovery algorithms can be complex to implement, especially if they use advanced
techniques such as the Resource Allocation Graph or Timestamping.
6. False Positives and Negatives: Deadlock detection algorithms are not perfect and may produce false positives or negatives,
indicating the presence of deadlocks when they do not exist or failing to detect deadlocks that do exist.
7. Risk of Data Loss: In some cases, recovery algorithms may require rolling back the state of one or more processes, leading to
data loss or corruption.
Deadlocks and  Deadlock Detection  Other Issues
Deadlock Avoidance
A deadlock avoidance policy grants a resource request only if it can establish that granting the request cannot
lead to a deadlock either immediately or in the future. The kernal lacks detailed knowledge about future behavior
of processes, so it cannot accurately predict deadlocks. To facilitate deadlock avoidance under these conditions, it
uses the following conservative approach: Each process declares the maximum number of resource units of each
class that it may require. The kernal permits a process to request these resource units in stages- i.e. a few
resource units at a time- subject to the maximum number declared by it and uses a worst case analysis technique
to check for the possibility of future deadlocks. A request is granted only if there is no possibility of deadlocks;
otherwise, it remains pending until it can be granted. This approach is conservative because a process may
complete its operation without requiring the maximum number of units declared by it.
Resource Allocation Graph
The resource allocation graph (RAG) is used to visualize the system’s current state as a graph. The Graph includes all processes, the
resources that are assigned to them, as well as the resources that each Process requests.
Banker’s Algorithm
Bankers’s Algorithm is a resource allocation and deadlock avoidance algorithm which test all the request made by processes for
resources, it checks for the safe state, and after granting a request system remains in the safe state it allows the request, and if there is
no safe state it doesn’t allow the request made by the process.
Inputs to Banker’s Algorithm
1. Max needs of resources by each process.
2. Currently, allocated resources by each process.
3. Max free available resources in the system.
The request will only be granted under the below condition
4. If the request made by the process is less than equal to the max needed for that process.
5. If the request made by the process is less than equal to the freely available resource in the system.
Timeouts: To avoid deadlocks caused by indefinite waiting, a timeout mechanism can be used to limit the amount of time a process can
wait for a resource. If the help is unavailable within the timeout period, the process can be forced to release its current resources and try
again later.
Example:
Total resources in system:
A B C D
6 5 7 6
The total number of resources are
Available system resources are:
A B C D
3 1 1 2
Available resources are
Processes (currently allocated resources):
A B C D
P1 1 2 2 1
P2 1 0 3 3
P3 1 2 1 0
Maximum resources we have for a process
Processes (maximum resources):
A B C D
P1 3 3 2 2
P2 1 2 3 4
P3 1 3 5 0
Need = Maximum Resources Requirement – Currently Allocated Resources.
Need = maximum resources - currently allocated resources.
Processes (need resources):
A B C D
P1 2 1 0 1
P2 0 2 0 1
P3 0 1 4 0
Deadlock Prevention
We can prevent a Deadlock by eliminating any of the above four conditions.
Eliminate Mutual Exclusion: It is not possible to dis-satisfy the mutual exclusion because some resources, such as the
tape drive and printer, are inherently non-shareable.
Eliminate Hold and wait: Allocate all required resources to the process before the start of its execution, this way hold
and wait condition is eliminated but it will lead to low device utilization. for example, if a process requires a printer at a
later time and we have allocated a printer before the start of its execution printer will remain blocked till it has
completed its execution. The process will make a new request for resources after releasing the current set of resources.
This solution may lead to starvation.
Eliminate No Preemption : Preempt resources from the process
when resources are required by other high-priority processes.
Eliminate Circular Wait : Each resource will be assigned a numerical number. A process can request the resources to
increase/decrease. order of numbering. For Example, if the P1 process is allocated R5 resources, now next time if P1
asks for R4, R3 lesser than R5 such a request will not be granted, only a request for resources more than R5 will be
granted.
Detection and Recovery: Another approach to dealing with deadlocks is to detect and recover from them when they
occur. This can involve killing one or more of the processes involved in the deadlock or releasing some of the
resources they hold.
Deadlocks and  Deadlock Detection  Other Issues

More Related Content

PPTX
Deadlocks
PPTX
Unit iv: Deadlocks
PPTX
Lecture 4 Deadlocks in operating systems.pptx
PPTX
Algorithm 4Chapter Four- Deadlock (5).pptx
PPTX
Os presentation final.pptxjjjjjdakajwsjjdhdfjff
PPTX
Ch 4 deadlock
PPT
Chapter 03
PPTX
dead lock in operating system Presentation1.pptx
Deadlocks
Unit iv: Deadlocks
Lecture 4 Deadlocks in operating systems.pptx
Algorithm 4Chapter Four- Deadlock (5).pptx
Os presentation final.pptxjjjjjdakajwsjjdhdfjff
Ch 4 deadlock
Chapter 03
dead lock in operating system Presentation1.pptx

Similar to Deadlocks and Deadlock Detection Other Issues (20)

PPT
3 (2).ppt
PPTX
DistributedDeadlock on distriburtedNetwork.pptx
PPTX
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
PDF
OS-Part-06.pdf
PPTX
Deadlockproj.pptx
PPTX
operating system
PPTX
operating system
PPTX
Deadlocks2
PPTX
Deadlocks
PPTX
operating system memory management with example
PPT
3 (1) [Autosaved].ppt
PPT
Deadlocks
PPT
A ppt on deadlock in operating systems for the better explanation
PPTX
chapter06-new.pptx
PPTX
Deadlock
PPTX
OS Presentation 1 (1).pptx
PPTX
Deadlock detection & prevention
PPTX
Deadlock
PDF
osvishal-160830131208 (1).pdf
3 (2).ppt
DistributedDeadlock on distriburtedNetwork.pptx
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
OS-Part-06.pdf
Deadlockproj.pptx
operating system
operating system
Deadlocks2
Deadlocks
operating system memory management with example
3 (1) [Autosaved].ppt
Deadlocks
A ppt on deadlock in operating systems for the better explanation
chapter06-new.pptx
Deadlock
OS Presentation 1 (1).pptx
Deadlock detection & prevention
Deadlock
osvishal-160830131208 (1).pdf
Ad

More from Guna Dhondwad (20)

DOCX
Unit 5 Web Application and Hacking Techniques.docx
DOCX
Unit 4 InformationGathering and Network Discovery.docx
DOCX
Unit 3 Networking Basics of Ethical Hacking.docx
DOCX
unit 2 Intoduction to Tools and Platforms.docx
DOCX
Unit 1 Introduction to Cyber Securiyt Attacks.docx
PPTX
Unit -1 cryptography contiues with algorithm.pptx
PPTX
Unit 1 Network Fundamentals and Security .pptx
PPTX
Unit 3 -Symmetric Key Cryptographic Algorithms – I.pptx
PPTX
Network and Internet Security.pptx
PPTX
Computer Security Essentials.pptx
DOCX
Concurrency : Mutual Exclusion and Synchronization
PDF
Process And Scheduling Algorithms in os
DOCX
A brief introduction about an operating system and its architecture
PPTX
operating systems and it's services.pptx
PPTX
Introductions to Problem solvings.pptx
PPTX
Introduction to computer and its fundamentals.pptx
PPTX
Internet Networking with its working.pptx
PPTX
Wi-Fi Hacking with the help of various tools.pptx
PPTX
Web Application Hacking tools .pptx
PPTX
Privilege Escalation in Ethical Hacking.pptx
Unit 5 Web Application and Hacking Techniques.docx
Unit 4 InformationGathering and Network Discovery.docx
Unit 3 Networking Basics of Ethical Hacking.docx
unit 2 Intoduction to Tools and Platforms.docx
Unit 1 Introduction to Cyber Securiyt Attacks.docx
Unit -1 cryptography contiues with algorithm.pptx
Unit 1 Network Fundamentals and Security .pptx
Unit 3 -Symmetric Key Cryptographic Algorithms – I.pptx
Network and Internet Security.pptx
Computer Security Essentials.pptx
Concurrency : Mutual Exclusion and Synchronization
Process And Scheduling Algorithms in os
A brief introduction about an operating system and its architecture
operating systems and it's services.pptx
Introductions to Problem solvings.pptx
Introduction to computer and its fundamentals.pptx
Internet Networking with its working.pptx
Wi-Fi Hacking with the help of various tools.pptx
Web Application Hacking tools .pptx
Privilege Escalation in Ethical Hacking.pptx
Ad

Recently uploaded (20)

PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
plant tissues class 6-7 mcqs chatgpt.pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
MA in English at Shiv Nadar University – Advanced Literature, Language & Rese...
PPTX
Macbeth play - analysis .pptx english lit
PPTX
Education and Perspectives of Education.pptx
PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PDF
Climate and Adaptation MCQs class 7 from chatgpt
PPTX
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
PPTX
2025 High Blood Pressure Guideline Slide Set.pptx
PDF
CRP102_SAGALASSOS_Final_Projects_2025.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PPTX
Climate Change and Its Global Impact.pptx
PDF
International_Financial_Reporting_Standa.pdf
PDF
Comprehensive Lecture on the Appendix.pdf
PPT
REGULATION OF RESPIRATION lecture note 200L [Autosaved]-1-1.ppt
PDF
Civil Department's presentation Your score increases as you pick a category
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Race Reva University – Shaping Future Leaders in Artificial Intelligence
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
Journal of Dental Science - UDMY (2021).pdf
plant tissues class 6-7 mcqs chatgpt.pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
MA in English at Shiv Nadar University – Advanced Literature, Language & Rese...
Macbeth play - analysis .pptx english lit
Education and Perspectives of Education.pptx
0520_Scheme_of_Work_(for_examination_from_2021).pdf
Climate and Adaptation MCQs class 7 from chatgpt
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
2025 High Blood Pressure Guideline Slide Set.pptx
CRP102_SAGALASSOS_Final_Projects_2025.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Climate Change and Its Global Impact.pptx
International_Financial_Reporting_Standa.pdf
Comprehensive Lecture on the Appendix.pdf
REGULATION OF RESPIRATION lecture note 200L [Autosaved]-1-1.ppt
Civil Department's presentation Your score increases as you pick a category
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf

Deadlocks and Deadlock Detection Other Issues

  • 2. Deadlock : 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. A Deadlock is a situation where each of the computer process waits for a resource which is being assigned to some another process. In this situation, none of the process gets executed since the resource it needs, is held by some other process which is also waiting for some other resource to be released. A process in operating system uses resources in the following way. 1. Requests a resource 2. Use the resource 3. Releases the resource Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions) Mutual Exclusion: Two or more resources are non-shareable (Only one process can use at a time) Hold and Wait: A process is holding at least one resource and waiting for resources. No Preemption: A resource cannot be taken from a process unless the process releases the resource. Circular Wait: A set of processes waiting for each other in circular form.
  • 3. Methods for handling deadlock There are three ways to handle deadlock 1) Deadlock prevention or avoidance: Prevention: The idea is to not let the system into a deadlock state. This system will make sure that above mentioned four conditions will not arise. These techniques are very costly so we use this in cases where our priority is making a system deadlock-free. One can zoom into each category individually, Prevention is done by negating one of the above- mentioned necessary conditions for deadlock. Prevention can be done in four different ways: 1. Eliminate mutual exclusion 3. Allow preemption 2. Solve hold and Wait 4. Circular wait Solution Avoidance: Avoidance is kind of futuristic. By using the strategy of “Avoidance”, we have to make an assumption. We need to ensure that all information about resources that the process will need is known to us before the execution of the process. We use Banker’s algorithm (Which is in turn a gift from Dijkstra) to avoid deadlock. In prevention and avoidance, we get the correctness of data but performance decreases.
  • 4. 2) Deadlock detection and recovery: If Deadlock prevention or avoidance is not applied to the software then we can handle this by deadlock detection and recovery. which consist of two phases: 1. In the first phase, we examine the state of the process and check whether there is a deadlock or not in the system. 2. If found deadlock in the first phase then we apply the algorithm for recovery of the deadlock. In Deadlock detection and recovery, we get the correctness of data but performance decreases. Recovery from Deadlock 1. Manual Intervention: When a deadlock is detected, one option is to inform the operator and let them handle the situation manually. While this approach allows for human judgment and decision-making, it can be time-consuming and may not be feasible in large-scale systems. 2. Automatic Recovery: An alternative approach is to enable the system to recover from deadlock automatically. This method involves breaking the deadlock cycle by either aborting processes or preempting resources. Let’s delve into these strategies in more detail.
  • 5. Recovery from Deadlock: Process Termination: 1. Abort all deadlocked processes: This approach breaks the deadlock cycle, but it comes at a significant cost. The processes that were aborted may have executed for a considerable amount of time, resulting in the loss of partial computations. These computations may need to be recomputed later. 2. Abort one process at a time: Instead of aborting all deadlocked processes simultaneously, this strategy involves selectively aborting one process at a time until the deadlock cycle is eliminated. However, this incurs overhead as a deadlock-detection algorithm must be invoked after each process termination to determine if any processes are still deadlocked. Factors for choosing the termination order: – The process’s priority – Completion time and the progress made so far – Resources consumed by the process – Resources required to complete the process – Number of processes to be terminated – Process type (interactive or batch)
  • 6. Recovery from Deadlock: Resource Preemption: 1. Selecting a victim: Resource preemption involves choosing which resources and processes should be preempted to break the deadlock. The selection order aims to minimize the overall cost of recovery. Factors considered for victim selection may include the number of resources held by a deadlocked process and the amount of time the process has consumed. 2. Rollback: If a resource is preempted from a process, the process cannot continue its normal execution as it lacks the required resource. Rolling back the process to a safe state and restarting it is a common approach. Determining a safe state can be challenging, leading to the use of total rollback, where the process is aborted and restarted from scratch. 3. Starvation prevention: To prevent resource starvation, it is essential to ensure that the same process is not always chosen as a victim. If victim selection is solely based on cost factors, one process might repeatedly lose its resources and never complete its designated task. To address this, it is advisable to limit the number of times a process can be chosen as a victim, including the number of rollbacks in the cost factor. 3) Deadlock ignorance: If a deadlock is very rare, then let it happen and reboot the system. This is the approach that both Windows and UNIX take. we use the ostrich algorithm for deadlock ignorance. In Deadlock, ignorance performance is better than the above two methods but the correctness of data. Safe State: A safe state can be defined as a state in which there is no deadlock. It is achievable if: ● If a process needs an unavailable resource, it may wait until the same has been released by a process to which it has already been allocated. if such a sequence does not exist, it is an unsafe state. ● All the requested resources are allocated to the process.
  • 7. What is Distributed System Deadlocks? Deadlocks in distributed systems are similar to deadlocks in single-processor systems. They are not easy to avoid, prevent, detect, and harder to cure when tracked down because all the relevant information is scattered over many machines. Some systems, such as distributed database systems, can be extremely serious, so it is important to understand how they differ from ordinary deadlocks. There are two kinds of distributed deadlocks, communication deadlocks, and resource deadlocks. A communication deadlock occurs when process A is trying to send a message to process B, which is trying to send one to process C, which is trying to send one to A. There are various scenarios in which this situation leads to a deadlock, such as no buffers being available. A resource deadlock occurs when processes fight exclusive access to I/O devices, files, locks, or other resources. Various strategies are used to handle deadlocks. Dealing with deadlocks can be done in the following ways, such as: 1. Ignore the problem 2. Detection 3. Prevention 4. Avoidance
  • 8. Resources Deadlock In resource deadlocks, processes can simultaneously wait for several resources and cannot proceed until they have acquired all those resources. A set of processes is resource deadlocked if each process in the set requests resources held by another process in the set, and it must receive all of the requested resources before it can become unblocked. This is called resource deadlock. For example, suppose a system is running 2 processes, P1 & P2, and they want the resources R1 & R2. The resource allocation graph is shown below: But here, the P1 process holds resource R1 & waits to get R2 resource while process P2 holds resource R2 & waits to get R1 resource. In this way, P1 waits for P2 to complete & P2 waits for P1 to get complete & hence a deadlock.
  • 9. What is Communication Deadlocks? Processes in this deadlock wait to communicate with other processes in a group of processes. A waiting process can unblock on receiving communication from any of these processes. If each process in the set is waiting to communicate with another process in the set, and no process in the set ever begins any additional communication until it receives the communication for which it is waiting, the set is communication-Deadlocked. For Example: Suppose, process A waits to get a message from process B, Process B waits to get a message from process C, and Process C waits to get a message from process A and hence a deadlock. The TFW diagram is shown below: Users in a distributed system (DDBS) access the database's data objects by executing transactions. A transaction can be thought of as a series of reads and writes performed on data objects. A database's data objects can be considered resources that are acquired (through locking) and released (through unlocking) by transactions. In DDBS, wait for the graph is referred to as transaction-wait-for-graph (TWF Graph)
  • 10. Difference between Resource Deadlock and Communication Deadlock
  • 11. Deadlock detection and recovery is the process of detecting and resolving deadlocks in an operating system. A deadlock occurs when two or more processes are blocked, waiting for each other to release the resources they need. This can lead to a system-wide stall, where no process can make progress. There are two main approaches to deadlock detection and recovery: 1. Prevention: The operating system takes steps to prevent deadlocks from occurring by ensuring that the system is always in a safe state, where deadlocks cannot occur. This is achieved through resource allocation algorithms such as the Banker’s Algorithm. 2. Detection and Recovery: If deadlocks do occur, the operating system must detect and resolve them. Deadlock detection algorithms, such as the Wait-For Graph, are used to identify deadlocks, and recovery algorithms, such as the Rollback and Abort algorithm, are used to resolve them. The recovery algorithm releases the resources held by one or more processes, allowing the system to continue to make progress.
  • 12. Deadlock Detection : 1. If resources have a single instance – In this case for Deadlock detection, we can run an algorithm to check for the cycle in the Resource Allocation Graph. The presence of a cycle in the graph is a sufficient condition for deadlock. In the above diagram, resource 1 and resource 2 have single instances. There is a cycle R1 P1 R2 P2. So, → → → Deadlock is Confirmed. 2. If there are multiple instances of resources – Detection of the cycle is necessary but not a sufficient condition for deadlock detection, in this case, the system may or may not be in deadlock varies according to different situations. 3. Wait-For Graph Algorithm – The Wait-For Graph Algorithm is a deadlock detection algorithm used to detect deadlocks in a system where resources can have multiple instances. The algorithm works by constructing a Wait-For Graph, which is a directed graph that represents the dependencies between processes and resources.
  • 13. Deadlock Recovery : A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is a time and space-consuming process. Real-time operating systems use Deadlock recovery. 1. Killing the process – Killing all the processes involved in the deadlock. Killing process one by one. After killing each process check for deadlock again and keep repeating the process till the system recovers from deadlock. Killing all the processes one by one helps a system to break circular wait conditions. 2. Resource Preemption – Resources are preempted from the processes involved in the deadlock, and preempted resources are allocated to other processes so that there is a possibility of recovering the system from the deadlock. In this case, the system goes into starvation. 3. Concurrency Control – Concurrency control mechanisms are used to prevent data inconsistencies in systems with multiple concurrent processes. These mechanisms ensure that concurrent processes do not access the same data at the same time, which can lead to inconsistencies and errors. Deadlocks can occur in concurrent systems when two or more processes are blocked, waiting for each other to release the resources they need. This can result in a system-wide stall, where no process can make progress. Concurrency control mechanisms can help prevent deadlocks by managing access to shared resources and ensuring that concurrent processes do not interfere with each other.
  • 14. Advantages of Deadlock Detection and Recovery in Operating Systems: 1. Improved System Stability: Deadlocks can cause system-wide stalls, and detecting and resolving deadlocks can help to improve the stability of the system. 2. Better Resource Utilization: By detecting and resolving deadlocks, the operating system can ensure that resources are efficiently utilized and that the system remains responsive to user requests. 3. Better System Design: Deadlock detection and recovery algorithms can provide insight into the behavior of the system and the relationships between processes and resources, helping to inform and improve the design of the system. Disadvantages of Deadlock Detection and Recovery in Operating Systems: 4. Performance Overhead: Deadlock detection and recovery algorithms can introduce a significant overhead in terms of performance, as the system must regularly check for deadlocks and take appropriate action to resolve them. 5. Complexity: Deadlock detection and recovery algorithms can be complex to implement, especially if they use advanced techniques such as the Resource Allocation Graph or Timestamping. 6. False Positives and Negatives: Deadlock detection algorithms are not perfect and may produce false positives or negatives, indicating the presence of deadlocks when they do not exist or failing to detect deadlocks that do exist. 7. Risk of Data Loss: In some cases, recovery algorithms may require rolling back the state of one or more processes, leading to data loss or corruption.
  • 16. Deadlock Avoidance A deadlock avoidance policy grants a resource request only if it can establish that granting the request cannot lead to a deadlock either immediately or in the future. The kernal lacks detailed knowledge about future behavior of processes, so it cannot accurately predict deadlocks. To facilitate deadlock avoidance under these conditions, it uses the following conservative approach: Each process declares the maximum number of resource units of each class that it may require. The kernal permits a process to request these resource units in stages- i.e. a few resource units at a time- subject to the maximum number declared by it and uses a worst case analysis technique to check for the possibility of future deadlocks. A request is granted only if there is no possibility of deadlocks; otherwise, it remains pending until it can be granted. This approach is conservative because a process may complete its operation without requiring the maximum number of units declared by it.
  • 17. Resource Allocation Graph The resource allocation graph (RAG) is used to visualize the system’s current state as a graph. The Graph includes all processes, the resources that are assigned to them, as well as the resources that each Process requests. Banker’s Algorithm Bankers’s Algorithm is a resource allocation and deadlock avoidance algorithm which test all the request made by processes for resources, it checks for the safe state, and after granting a request system remains in the safe state it allows the request, and if there is no safe state it doesn’t allow the request made by the process. Inputs to Banker’s Algorithm 1. Max needs of resources by each process. 2. Currently, allocated resources by each process. 3. Max free available resources in the system. The request will only be granted under the below condition 4. If the request made by the process is less than equal to the max needed for that process. 5. If the request made by the process is less than equal to the freely available resource in the system. Timeouts: To avoid deadlocks caused by indefinite waiting, a timeout mechanism can be used to limit the amount of time a process can wait for a resource. If the help is unavailable within the timeout period, the process can be forced to release its current resources and try again later.
  • 18. Example: Total resources in system: A B C D 6 5 7 6 The total number of resources are Available system resources are: A B C D 3 1 1 2 Available resources are Processes (currently allocated resources): A B C D P1 1 2 2 1 P2 1 0 3 3 P3 1 2 1 0
  • 19. Maximum resources we have for a process Processes (maximum resources): A B C D P1 3 3 2 2 P2 1 2 3 4 P3 1 3 5 0 Need = Maximum Resources Requirement – Currently Allocated Resources. Need = maximum resources - currently allocated resources. Processes (need resources): A B C D P1 2 1 0 1 P2 0 2 0 1 P3 0 1 4 0
  • 20. Deadlock Prevention We can prevent a Deadlock by eliminating any of the above four conditions. Eliminate Mutual Exclusion: It is not possible to dis-satisfy the mutual exclusion because some resources, such as the tape drive and printer, are inherently non-shareable. Eliminate Hold and wait: Allocate all required resources to the process before the start of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization. for example, if a process requires a printer at a later time and we have allocated a printer before the start of its execution printer will remain blocked till it has completed its execution. The process will make a new request for resources after releasing the current set of resources. This solution may lead to starvation. Eliminate No Preemption : Preempt resources from the process when resources are required by other high-priority processes.
  • 21. Eliminate Circular Wait : Each resource will be assigned a numerical number. A process can request the resources to increase/decrease. order of numbering. For Example, if the P1 process is allocated R5 resources, now next time if P1 asks for R4, R3 lesser than R5 such a request will not be granted, only a request for resources more than R5 will be granted. Detection and Recovery: Another approach to dealing with deadlocks is to detect and recover from them when they occur. This can involve killing one or more of the processes involved in the deadlock or releasing some of the resources they hold.