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

Deadlock Prevention And Avoidance

Last Updated : 07 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Deadlock is a condition where two or more processes try to access the critical section at the same time but fail to access it simultaneously or get stuck while accessing the critical section.

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) must move through intersections (resources) without getting into a gridlock.

Necessary Conditions for Deadlock

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

Please refer Conditions for Deadlock in OS for details.

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 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

Deadlock avoidance ensures that a resource request is only granted if it won’t lead to deadlock, either immediately or in the future. Since the kernel can’t predict future process behavior, it uses a conservative approach. Each process declares the maximum number of resources it may need. The kernel allows requests in stages, checking for potential deadlocks before granting them. A request is granted only if no deadlock is possible; otherwise, it stays pending. This approach is conservative, as a process may finish without using the maximum resources it declared.

Banker’s Algorithm

Bankers’ Algorithm is a resource allocation and deadlock avoidance algorithm which tests all the requests 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: Below is an example of a Banker’s algorithm

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.

Processes (need resources):
A B C D
P1 2 1 0 1
P2 0 2 0 1
P3 0 1 4 0

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

What are techniques used to avoid Deadlock?

These are the main techniques used to avoid deadlock

  • Resource ordering
  • Banker’s algorythm
  • Resource pre-allocation

What did Resource ordering do in Deadlock avoidance?

  • Assigns a unique order to all resources: Each resource in the system is assigned a unique identifier or number.
  • Processes must request resources in ascending order: A process must request resources in the order of their assigned numbers.
  • Prevents circular wait: By enforcing this order, the possibility of a circular wait condition is eliminated.
  • Example: If resource A has a lower order than resource B, a process must request resource A before requesting resource B.
  • Limitations: May not be suitable for all systems, as it can impose restrictions on resource allocation and may not always be feasible to assign a strict order to all resources.


Next Article

Similar Reads

three90RightbarBannerImg