Deadlock in OS

If two or more processes are waiting for some event that never happens, then we say these processes are involved in deadlock, and the state will be the deadlock state in OS.

If all of the following four conditions fulfill the current situation, then deadlock will occur. If any one condition is false, then there will be no deadlock. 

1. Mutual Exclusion (Never Share Resource)

Only one process can use any resource at a time. If there are two processes, P1 and P2, where a resource R1 is allocated to P1 and P2, a request for R1 is made at the same time. Then P1 never shares the resource R1 with process P2 because the condition of mutual exclusion is that only one process can use one resource at a time.

Cooperative processes cannot use the critical section at the same time.

2. No Preemptive

If a process P1 holds a resource, it will never release it for upcoming process P2, which also demands the same resource already allocated to P1.

3. Hold And Wait

If a process P1 holds a resource and an upcoming process P2 also demands the same resource that has already been allocated to P1, Then P2 has to wait until P1 releases the resource.

4. Circular Wait

deadlock - eliminate circular wait

There exists a set of processes (P1, P2, P3……PN). Where P1 is waiting for P2 P2 is waiting for P3 and so on until PN. And PN is waiting for P1 in the last.

Deadlock Vs. Starvation in OS

Deadlock Starvation
1. In the Deadlock situation, no further process block and no process execution. 1. In Starvation, all the low-priority processes are blocked, and all high-priority processes are executed.
2. Deadlock is infinite time waiting. 2. Starvation is a long wait time but not infinite.
3. Every Deadlock is always a starvation. 3. Every starvation may not be a deadlock.
4. The requested resource is blocked by the other process. 4. The requested resource is continuously used by the higher-priority processes.
5. Deadlock occurs when the following four conditions are fulfilled at the same time.
  • Mutual exclusion preemption hold and wait for circular wait
5. It may occur due to resource management and having no control over the priority of processes, etc.

Deadlock in OS can be handled through the following Methods

1. Deadlock Ignorance 
2. Deadlock Prevention
3. Deadlock Avoidance
4. Deadlock Reduction
5. Deadlock Recovery