How to Prevent Deadlocks

Deadlock is a situation or condition in which two or more threads are blocked forever. It can also be said that deadlock is a situation in which two or more computer programs sharing the same resource prevent each other from accessing the available resources. Deadlocks can be prevented by adapting proper resource sharing methods and proper programming. Here are some suggestions that will help you know how to prevent deadlocks.

This problem is mainly due to implementation of multithreading process. Earlier computer system is used to run a single program at single time. In multithreading, multiple programs are run by the operating system at a single time. This made the operating system capable of multitasking, which made it more efficient to use, but this resulted in the problem of deadlock.

The operating systems earlier were also allowed to specify the resources required to run a specific program to avoid the conflict of resource sharing. Now, resources are allocated dynamically by the operating system. This also results into deadlock.

When does a deadlock occur?

A simple example below will illustrate the condition in which deadlock occurs.

Suppose there are two programs, program A and program B. Both these programs are running simultaneously.

  • Now, program A requests for resource A1 and it gets this resource as it is free and executes its task.
  • Program B also requests for another resource say b1. It also gets this resource as no other program is using this resource.
  • After completing its task, program A wants the resources available with program B. Program B is still running, this resource is not free for program A, and is queued and waits until the release of resource from B.
  • After completing the task, program B also wants resource a1 available with program A. As this resource is with program A, program B cannot get it and it is also queued until program A releases its resource.
  • Hence, neither program A nor program B can proceed now as both are waiting for resources. In this case, deadlock occurs and both the programs are resumed for an indefinite time.

When deadlocks occur frequently in a machine, the efficiency of the machine reduces subsequently. There are methods, which you can adapt while designing the operating system to prevent deadlocks and increase machine efficiency.

How to prevent deadlocks:

Non-blocking synchronization algorithm:

  • We can use non-blocking synchronization algorithms to prevent deadlocks in the multithreaded programs.
  • If we use these non-blocking synchronization algorithms, the programs or threads that are competing for the same resources are not resumed for an indefinite time.
  • These algorithms when used while designing a program remove the condition of mutual exclusion. This means no process has access to the resource exclusively.
  • Such algorithms can be used only for process that can be spooled and mutual exclusion can be avoided only for them. Spooling is a method in which process are placed in some temporary location until they get the resources required by them and start their work.
  • It is not possible to prevent deadlocks for process, which cannot be spooled using this method of mutual exclusion.

Removing “hold and wait” condition:

  • “hold-and-wait” condition is the condition in which the process which is running and has already occupied some resource, may request for some other resource held by another process.
  • To remove this “hold-and-wait” condition, we need to program the process in such a manner that the process requests for all the required resources while they begin. However, this is an inefficient use of resources, which will help you in preventing deadlock.
  • We can also program the process in such a way that they release all their resources before they gain hold of another resource.

Avoiding preemption condition:

  • Preemption is the condition in which no process can forcefully take a resource from another process, which is currently running and is using this resource.
  • By avoiding this condition using certain algorithms, we can avoid deadlocks.
  • Avoiding this condition can result in some critical problems, as a process has to retain a resource with it until a certain time limit. This time can be until the process has completed its task and no more requires this resource.

Avoiding circular wait condition:

  • In this condition, two or more process waits for another process to complete its execution and release the resource available with it.
  • This major condition results into a deadlock.
  • We can prevent this kind of condition from occurring by using algorithms such as disable interrupts during critical section and by using a hierarchy to determine partial ordering of resources.

In some network operating systems, we also use deep buffers to avoid deadlocks. These multiprocessors using deep buffers increase the efficiency of the systems in which long wires are used.


Related Content:

  1. How to Prevent Lag
  2. How to Prevent Laptop Overheating
  3. How to Prevent Hacking
  4. How to Prevent Procrastination
  5. How to Prevent Employee Theft
. Tags: .

Leave a Reply