Skip to main content

Deadlock Prevention, Detection, Recovery and Avoidance

Deadlock! Think about computer process deadlock being like the traffic deadlock.

Deadlock! Think about computer process deadlock being like the traffic deadlock.

The red cars are causing a deadlock!

The red cars are causing a deadlock!

Deadlock prevention and avoidance in a computer process are the optimal goals but when deadlock occur, detection and recovery can be difficult to initiate.

How many times do you hear “…my computer has hanged up…” or “…my work is taking ages to come out of the printer” or see this response “Program xyz is not responding” Well, one causes of these could be a Deadlock.

This Hub will explain what is a Deadlock, what causes Deadlock, how to prevent a Deadlock, how to detect and recover from a Deadlock and how to avoid Deadlock.

Definition of Deadlock

A situation may arise in shared resources whereby two or more processes (also called threads) cannot continue because the resources which each process requires are held by another process.

This scenario in software systems process management is referred to as Deadlock and in its simplest form it will occur if process 1 is allocated resource A and later it requires resource B and process 2 is allocated resource B and later it requires resource A such that the processes results into a circular wait. The diagram below show a circular waiting caused by three processes.

How deadlock occur and what causes deadlock

How deadlock occur and what causes deadlock

How deadlock occur and what causes deadlock

What causes deadlock?

There are four conditions that allow Deadlock to occur in processes:-

1. The resources involved in processes cannot be shared

2. Processes hold on the resources they have already been allocated while waiting for new ones

3. Resources cannot be pre-emptied (de-allocated) while in use

4. A circular chain of processes exists such that the resource that is currently being requested is in the chain.

Process Deadlock can be solved by adapting one of the following strategies:-

1. Prevent Deadlock by ensuring that at all times at least one of the conditions mentioned above does not hold.

2. Detect Deadlock when it occurs and then employ mechanisms to try and recover it

3. Avoid Deadlock by suitable inspection actions


Deadlock prevention

To prevent Deadlock at least one of the four conditions mentioned above must be denied.

Condition 1 is difficult to deny since some resources, for example printers, by nature cannot be shared between processes. However, the use of spooling can remove Deadlock potential of non-shared peripherals.

Scroll to Continue

Condition 2 can be denied by stipulating that processes request all the resources at once and that they cannot proceed until all the requests are granted. The disadvantage of this action is that resources which are used for a short time are allocated and therefore inaccessible for a long period.

Condition 3 is easily denied by imposing the rule that if a process is denied a request then it must release all the resources that it currently hold and if necessary request for them later together with additional resources. This strategy can be inconvenient in practice since pre-empting a resource like a printer can results into inter-leaving of outputs from several jobs. Further, even if a resource is conveniently pre-emptied the overhead of storing and restoring its states at the time of pre-emption can be quite high.

Condition 4 can be denied by imposing an order on resource types so that if a process has been allocated a resource of type A then it may only request resources of types that fall in that order. This ensures that the circular wait condition does not arise. The disadvantage of this strategy is the constraints imposed on the natural order of resources although this can be reduced by placing commonly used resources early in the order.

Deadlock detection and recovery

This strategy allows the possibility of Deadlock but rely on detection when it occurs and being able to stage recovery. The value of this approach depends on the frequency on which deadlock occurs and the kind of recovery that can be made. Detection algorithms work by detecting the circular wait seen in condition 4. The state of the system at any time can be represented by a state graph.

How deadlock occur and what causes deadlock

How deadlock occur and what causes deadlock

How to detect deadlock and recover from deadlock

A circular wait is represented as a circular closed loop A, B and D (see above). The Deadlock detection algorithm maintains a representation of state graph and inspects it at intervals for existence of a circular loop.

The inspection may occur at every resource allocation. Since the cost of doing the inspection may be very high it may occur at fixed intervals of time and not at each allocation. Detection of deadlock is only useful if an acceptable level of recovery can be made.

The definition of acceptable can be stretched according to circumstances to include the following techniques listed according to the order of sophistication:-

1. Abort all deadlocked processes – this is the method adopted in most general purpose systems.

2. Re-start all deadlocked processes, however, this method may lead straight back to the original Deadlock.

3. Successfully (one at a time) abort deadlocked processes until Deadlock no longer exist. The order in which this is done should reduce resources already used.

4. Successfully (one at a time) pre-empty resources from deadlocked processes until Deadlock no longer exist. The order of pre-empting should be such that to minimize the cost.

Deadlock avoidance

Deadlock avoidance strategy uses an algorithm that anticipates that a Deadlock is likely to occur and therefore deny a resource request which would otherwise be granted.

Before granting a resource request, the state graph of the system is tentatively changed to what it will be if the resource was to be granted and then deadlock detection is applied. If the detection algorithm is clear then the request is granted otherwise the request is denied and the state graph is returned to its original form.

This technique does not work always since it relies on the premise that if the allocation is going to result into a deadlock then it will do so immediately. This is FALSE since Deadlock can occur anytime during the life of a process.

It is important to note that detection and recovery of Deadlock is sometimes left to the computer user rather than being performed by the system itself. An observant computer user will note that certain processes are stuck and realize that Deadlock has occurred. The traditional recovery action is to abort and re-start the deadlocked processes if possible.

Modern Operating Systems provide users with an option to kill a process or processes (for example ‘End Task’ command in ‘Windows Task Manager’) without necessarily shutting down the Operating System.

Ok, now you know what could be the problem with your computer, go ahead and take the necessary action.

  • What is cloud computing? A beginner's approach
    Cloud computing is simply a set of pooled computing resources and services (“cloud”) delivered over the internet. A cloud can be deployed in different structures for example, a Public cloud trade services to anyone on the Internet .
  • Data Security Issues In Cloud Computing
    Cloud computing is the key driving force in many small, medium and large sized companies and as many cloud users seeks the services of cloud computing, the one question that remain their major concern is the security of their data in the cloud.
  • Cloud Computing: Deciding Between Public Cloud And Private Cloud
    The best way then to decide whether you need a private cloud or a public cloud or even a hybrid cloud for your organization is to outline the goals and policies of your organization then implement the suitable cloud option.
  • Microsoft X-Box 360 SmartGlass overview and changes in the Living room
    SmartGlass works as your second screen with a truly unique viewing experience, that means you can be able to view real-time shows and game stats on your iPhone or even display trivia and maps on your tablet.

Want to make money online?

Related Articles