Deadlock System model (original) (raw)

Last Updated : 23 Jul, 2025

Overview :
A deadlock occurs when a set of processes is stalled because each process is holding a resource and waiting for another process to acquire another resource. In the diagram below, for example, Process 1 is holding Resource 1 while Process 2 acquires Resource 2, and Process 2 is waiting for Resource 1.

System Model :

Operations :
In normal operation, a process must request a resource before using it and release it when finished, as shown below.

  1. Request -
    If the request cannot be granted immediately, the process must wait until the resource(s) required to become available. The system, for example, uses the functions open(), malloc(), new(), and request ().
  2. Use -
    The process makes use of the resource, such as printing to a printer or reading from a file.
  3. Release -
    The process relinquishes the resource, allowing it to be used by other processes.

Necessary Conditions :
There are four conditions that must be met in order to achieve deadlock as follows.

  1. Mutual Exclusion -
    At least one resource must be kept in a non-shareable state; if another process requests it, it must wait for it to be released.
  2. Hold and Wait -
    A process must hold at least one resource while also waiting for at least one resource that another process is currently holding.
  3. No preemption -
    Once a process holds a resource (i.e. after its request is granted), that resource cannot be taken away from that process until the process voluntarily releases it.
  4. Circular Wait -
    There must be a set of processes P0, P1, P2,..., PN such that every P[I] is waiting for P[(I + 1) percent (N + 1)]. (It is important to note that this condition implies the hold-and-wait condition, but dealing with the four conditions is easier if they are considered separately).

Methods for Handling Deadlocks :
In general, there are three approaches to dealing with deadlocks as follows.

  1. Preventing or avoiding deadlock by Avoid allowing the system to become stuck in a loop.
  2. Detection and recovery of deadlocks, When deadlocks are detected, abort the process or preempt some resources.
  3. Ignore the problem entirely.
  4. To avoid deadlocks, the system requires more information about all processes. The system, in particular, must understand what resources a process will or may request in the future. ( Depending on the algorithm, this can range from a simple worst-case maximum to a complete resource request and release plan for each process. )
  5. Deadlock detection is relatively simple, but deadlock recovery necessitates either aborting processes or preempting resources, neither of which is an appealing option.
  6. If deadlocks are not avoided or detected, the system will gradually slow down as more processes become stuck waiting for resources that the deadlock has blocked and other waiting processes. Unfortunately, when the computing requirements of a real-time process are high, this slowdown can be confused with a general system slowdown.

Deadlock Prevention :
Deadlocks can be avoided by avoiding at least one of the four necessary conditions: as follows.

Condition-1 :
Mutual Exclusion :

Condition-2 :
Hold and Wait :
To avoid this condition, processes must be prevented from holding one or more resources while also waiting for one or more others. There are a few possibilities here:

Condition-3 :
No Preemption :
When possible, preemption of process resource allocations can help to avoid deadlocks.

Condition-4 :
Circular Wait :

Deadlock Avoidance :

Deadlock Detection :

Recovery From Deadlock :
There are three basic approaches to getting out of a bind:

  1. Inform the system operator and give him/her permission to intervene manually.
  2. Stop one or more of the processes involved in the deadlock.
  3. Prevent the use of resources.

Approach of Recovery From Deadlock :
Here, we will discuss the approach of Recovery From Deadlock as follows.

Approach-1 :
Process Termination :
There are two basic approaches for recovering resources allocated to terminated processes as follows.

  1. Stop all processes that are involved in the deadlock. This does break the deadlock, but at the expense of terminating more processes than are absolutely necessary.
  2. Processes should be terminated one at a time until the deadlock is broken. This method is more conservative, but it necessitates performing deadlock detection after each step.

In the latter case, many factors can influence which processes are terminated next as follows.

  1. Priorities in the process
  2. How long has the process been running and how close it is to completion.
  3. How many and what kind of resources does the process have? (Are they simple to anticipate and restore? )
  4. How many more resources are required for the process to be completed?
  5. How many processes will have to be killed?
  6. Whether the process is batch or interactive.

Approach-2 :
Resource Preemption :
When allocating resources to break the deadlock, three critical issues must be addressed:

  1. Selecting a victim -
    Many of the decision criteria outlined above apply to determine which resources to preempt from which processes.
  2. Rollback -
    A preempted process should ideally be rolled back to a safe state before the point at which that resource was originally assigned to the process. Unfortunately, determining such a safe state can be difficult or impossible, so the only safe rollback is to start from the beginning. (In other words, halt and restart the process.)
  3. Starvation -
    How do you ensure that a process does not go hungry because its resources are constantly being preempted? One option is to use a priority system and raise the priority of a process whenever its resources are preempted. It should eventually gain a high enough priority that it will no longer be preempted.