The Wayback Machine - https://web.archive.org/web/20240706045528/https://www.geeksforgeeks.org/deadlock-prevention/
Open In App

Deadlock Prevention And Avoidance

Last Updated : 26 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

When two or more processes try to access the critical section at the same time and they fail to access simultaneously or stuck while accessing the critical section then this condition is known as Deadlock.

Deadlock prevention and avoidance are strategies used in computer systems to ensure that different processes can run smoothly without getting stuck waiting for each other forever. Think of it like a traffic system where cars (processes) need to move through intersections (resources) without getting into a gridlock. In this article, we are going to discuss deadlock prevention and avoidance strategies in detail.

Deadlock Characteristics

 The deadlock has the following characteristics:

  • Mutual Exclusion
  • Hold and Wait
  • No Preemption
  • Circular Wait

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.
 

Hold and Wait

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.

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. Sometimes, if there are fewer processes, we can quickly spot a deadlock in the system by looking at the graph rather than the tables we use in Banker’s algorithm. Deadlock avoidance can also be done with Banker’s Algorithm. 

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

  • Max needs of resources by each process. 
  • Currently, allocated resources by each process. 
  • Max free available resources in the system.

The request will only be granted under the below condition

  • If the request made by the process is less than equal to the max needed for that process. 
  • 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

Conclusion

Operating Systems employ deadlock avoidance to prevent deadlock by using the Banker’s algorithm or the resource allocation graph. Deadlock avoidance works by informing the operating system of the resources needed by the process to finish execution, and the operating system then determines whether or not the requirements can be met.

The System is said to be in a Safe state if all the resources required by the Process are satisfied with the resources that are currently available. The System is said to be in an unsafe state if all of the resource requirements of the Process cannot be met by the available resources in any way. Deadlock prevention is more strict than Deadlock Avoidance. 

Frequently Asked Questions on Deadlock Prevention And Avoidance – FAQs

What is deadlock prevention? 

Deadlock prevention is a way to make sure that a deadlock, where processes get stuck waiting for each other and can’t move forward, never happens in a computer system. It sets up rules to manage how resources are used, so processes can’t get into a situation where they are stuck.

What is deadlock avoidance? 

Deadlock avoidance is a method used in computer systems to ensure that processes do not enter into a deadlock situation. Instead of just setting rules like in deadlock prevention, deadlock avoidance continuously monitors the state of the system and makes decisions on-the-fly to keep processes from getting stuck.

What are the 4 conditions for deadlock?

Four conditions of deadlock are:

  • Mutual Exclusion
  • Hold and Wait
  • No Preemption
  • Circular Wait


Previous Article
Next Article

Similar Reads

Difference between Deadlock Prevention and Deadlock Avoidance
1. Deadlock Prevention : Deadlock prevention means to block at least one of the four conditions required for deadlock to occur. If we are able to block any one of them then deadlock can be prevented. The four conditions which need to be blocked are:- Mutual ExclusionHold and WaitNo PreemptionCircular Wait Spooling and non-blocking synchronization a
2 min read
Introduction to TimeStamp and Deadlock Prevention Schemes in DBMS
Deadlock occurs when each transaction T in a schedule of two or more transactions waiting for some item locked by some other transaction T' in the set. Thus, both end up in a deadlock situation, waiting for the other to release the lock on the item. Deadlocks are a common problem and we have introduced the problem while solving the Concurrency Cont
7 min read
Zombie Processes and their Prevention
Zombie state: When a process is created in UNIX using fork() system call, the parent process is cloned. If the parent process calls wait() system call, then the execution of the parent is suspended until the child is terminated. At the termination of the child, a 'SIGCHLD' signal is generated by the kernel which is delivered to the parent. Parent,
5 min read
Deadlock Detection And Recovery
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
5 min read
Difference between Deadlock and Starvation in OS
Deadlock: Deadlock occurs when each process holds a resource and wait for other resource held by any other process. Necessary conditions for deadlock to occur are Mutual Exclusion, Hold and Wait, No Preemption and Circular Wait. In this no process holding one resource and waiting for another get executed. For example, in the below diagram, Process
2 min read
Relation Between Number of Processes and Number of Resources to Prevent Deadlock
A set of blocked processes where each process is holding some resource and waiting to acquire some other resource held by another process in the set. This situation is called deadlock in the operating system. Deadlock can be prevented if the number of resource instances in a system is significantly larger than the number of processes asking for a r
3 min read
Deadlock, Starvation, and Livelock
Prerequisite - Deadlock and Starvation  Livelock occurs when two or more processes continually repeat the same interaction in response to changes in the other processes without doing any useful work. These processes are not in the waiting state, and they are running concurrently. This is different from a deadlock because in a deadlock all processes
6 min read
Operating Systems | Deadlock | Question 1
Suppose n processes, P1, â€Ķ. Pn share m identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process Pi is Si, where Si > 0. Which one of the following is a sufficient condition for ensuring that deadlock does not occur? (A) A (B) B (C) C (D) D Answer: (C) Explanation: See Question 4 on foll
1 min read
Operating Systems | Deadlock | Question 2
Consider the following snapshot of a system running n processes. Process i is holding Xi instances of a resource R, 1 <= i <= n. currently, all instances of R are occupied. Further, for all i, process i has placed a request for an additional Yi instances while holding the Xi instances it already has. There are exactly two processes p and q su
1 min read
Recovery from Deadlock in Operating System
In today's world of computer systems and multitasking environments, deadlock is an undesirable situation that can bring operations to a grinding halt. When multiple processes compete for exclusive access to resources and end up in a circular waiting pattern, a deadlock occurs. To maintain the smooth functioning of an operating system, it is crucial
7 min read