What is Deadlock and what are its four necessary conditions?

In this blog, we will learn one of the most important concepts of Operating System i.e. Deadlock. We will find what a Deadlock is and we will also see the four necessary conditions of Deadlock. So, let's get started.

If you are already familiar with deadlock and its four necessary conditions, then you can skip this blog and read What are Deadlock handling techniques in Operating System?

What is Deadlock?

Deadlock is a situation where two or more processes are waiting for each other. For example, let us assume, we have two processes P1 and P2. Now, process P1 is holding the resource R1 and is waiting for the resource R2. At the same time, the process P2 is having the resource R2 and is waiting for the resource R1. So, the process P1 is waiting for process P2 to release its resource and at the same time, the process P2 is waiting for process P1 to release its resource. And no one is releasing any resource. So, both are waiting for each other to release the resource. This leads to infinite waiting and no work is done here. This is called Deadlock.

If a process is in the waiting state and is unable to change its state because the resources required by the process is held by some other waiting process, then the system is said to be in Deadlock.

Let's take one real-life example to understand the concept of Deadlock in a better way. Suppose, you are studying in a school and you are using the bus service also. So, you have to pay two fees i.e. bus fee and tuition fee. Now, think of a situation, when you go for submitting the bus fee and the accountant says that you have to submit the tuition fee first and then the bus fee. So, you go to submit the tuition fees on the other counter and the accountant there said that you have to first submit the bus fees and then the tuition fees. So, what will you do here? You are in a situation of deadlock here. You don't know what to submit first, bus fees or tuition fees?

Necessary Conditions of Deadlock

There are four different conditions that result in Deadlock. These four conditions are also known as Coffman conditions and these conditions are not mutually exclusive. Let's look at them one by one.

  • Mutual Exclusion: A resource can be held by only one process at a time. In other words, if a process P1 is using some resource R at a particular instant of time, then some other process P2 can't hold or use the same resource R at that particular instant of time. The process P2 can make a request for that resource R but it can't use that resource simultaneously with process P1.
  • Hold and Wait: A process can hold a number of resources at a time and at the same time, it can request for other resources that are being held by some other process. For example, a process P1 can hold two resources R1 and R2 and at the same time, it can request some resource R3 that is currently held by process P2.
  • No preemption: A resource can't be preempted from the process by another process, forcefully. For example, if a process P1 is using some resource R, then some other process P2 can't forcefully take that resource. If it is so, then what's the need for various scheduling algorithm. The process P2 can request for the resource R and can wait for that resource to be freed by the process P1.
  • Circular Wait: Circular wait is a condition when the first process is waiting for the resource held by the second process, the second process is waiting for the resource held by the third process, and so on. At last, the last process is waiting for the resource held by the first process. So, every process is waiting for each other to release the resource and no one is releasing their own resource. Everyone is waiting here for getting the resource. This is called a circular wait.

Deadlock will happen if all the above four conditions happen simultaneously.

Difference between Deadlock and Starvation

There is a difference between a Deadlock and Starvation. You shouldn't get confused between these. In the case of Deadlock, each and every process is waiting for each other to release the resource. But in the case of starvation, the high priority processes keep on executing and the lower priority processes keep on waiting for its execution. So, every deadlock is always starvation, but every starvation is not a deadlock. Deadlock is infinite waiting but starvation is not an infinite waiting. Starvation is long waiting. If the higher priority processes don't come, then the lower priority process will get a chance to be executed in case of starvation. So, in the case of starvation, we have long waiting and not infinite waiting. You can learn more about starvation from here .

Read our blog What are deadlock handling techniques in Operating System? to find ways of handling deadlock if it occurs.

That's it for this blog. Hope you enjoyed this blog.

Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content.

Keep Learning :)

Team AfterAcademy!