What is a Deadlock and Why Does it Happen?

by Carson
49 views
deadlock of two processes

Occasionally, multiple processes or apps may stop responding at once because of a deadlock. But what is a deadlock? Why does it make apps (or even the operating system) freeze? What are the conditions needed to make it happen? Let’s get started.

What is a Deadlock?

Suppose two people (represented as A and B) have to conduct experiments for their projects to complete. A’s experiment can help B finish the project, and vice versa. However, as they don’t trust each other, they refuse to lend the resources to each other.

Then, what will occur? A is waiting for equipment from B, while B is waiting for equipment from A. Meanwhile, they are both locking their resources, which don’t allow them to exchange equipment. Therefore, they are stuck in a state where they can’t finish the project unless they find the equipment from other sources.

That’s what’s happening with two processes when they’re in a deadlock. They refuse to release resources, and they are trying to use the other’s resources simultaneously. Moreover, a deadlock can happen with any number of apps, as long as multiple apps are in trouble. But the same thing is happening to the apps: There’s no way forward other than obtaining the resources, so these apps will stop responding.

An illustration of four apps in a deadlock
Image edited by Canva

What Are the Necessary Conditions for a Deadlock to Occur?

There are precisely four conditions that must be satisfied simultaneously for apps to be stuck in a deadlock: These four conditions include:

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

Now, let’s talk about them one by one and explain why all four conditions are essential for a deadlock.

Mutual Exclusion

In this scenario, a resource can be held by only one program at a time, during which the app cannot share it with other processes. This is also a critical policy of operating systems since if two or more processes have read/write access to the resource at the same time, it can cause conflicts, which may lead to incomplete data being saved to the hard drive.

But this is also a necessary condition for deadlocks to occur. If programs could share the same set of resources, they will not have to wait for the other to complete.

Hold and Wait

For deadlocks to happen, the programs involved must be able to hold resources while waiting for another set of resources. If not, resources will be released when the program waits for another resource, which will prevent deadlocks by giving way to other programs to grab resources. However, this is also a bad thing since if a program can’t hold and wait simultaneously, it could cause the temporary data needed for the program to run to be lost.

No Preemption

In this scenario, other programs must not use the resources until a program releases them voluntarily. If this is not satisfied, other programs may be able to forcefully release the resources and take them, which is also an issue that can cause errors from the missing or modified resources that are being held.

Circular Wait

Lastly, the most important reason for deadlocks to exist is the possibility of a circular wait. It means that one process is waiting for the other to complete a task, and so on until it comes back to the first process. This is easily seen in the image that shows an example of a deadlock.

If the circular wait is broken, the issue will be resolved since each program can then get the resources needed for them to continue.

Prevention and Handling

Now, it’s time to discuss how to avoid deadlocks. Since the first three conditions are necessary, preventions of deadlocks should only focus on preventing or breaking the “circular wait”.

Firstly, if a deadlock has occurred, killing one of the processes will help. Since there is no infinite loop on waiting for resources, the other processes will be given what they need, solving the problem.

Secondly, a priority-based precaution can be implemented. That means one app will have higher access levels to resources, and can lock them first even if multiple programs requests the corresponding resources at the same time and causes a circular wait. That way, the processes with a lower priority will be waiting for the resources to be released, which in turn, truly prevents the deadlock from happening in the first place.

Conclusion

Deadlocks are serious problems that may lead to data loss, but it only happens in situations where four conditions are met at the same time and place. Remember how deadlocks work, and you can learn more in the references if you’d like.

References and Credits

  1. (n.d.). Avoiding Deadlock. Retrieved August 19, 2021, from https://docs.oracle.com/cd/E19455-01/806-5257/6je9h0347/index.html
  2. (n.d.). Deadlock. Retrieved August 19, 2021, from https://www.cs.yale.edu/homes/aspnes/pinewiki/Deadlock.html
  3. (2019, November 7). What is Deadlock and what are its four necessary conditions? Retrieved August 19, 2021, from https://afteracademy.com/blog/what-is-deadlock-and-what-are-its-four-necessary-conditions

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.