Necessary condition to occur deadlock, mutual exclusion, no preemption in operating systems

What is the deadlock?

A process  P1 holing some resources and waiting for some another resource that is held by some another Process P2. Similarly, P2 has some resources and waiting for some resources held by P1.  In this condition, both processes are deadlocked. None of them can complete their execution.

deadlock in operating systems
Figure 1: deadlock in operating systems

What are four necessary conditions to occur deadlock?

If these four conditions occur simultaneously, then it is the deadlock.

Mutual exclusion

If a resource is shareable and can be accessed by more than one process at the same time. Then it leads to deadlock.

Hold and wait

If one process holding a resource and waiting for another resource that is held by another process. Then it leads to deadlock.

Circular wait

If processes are waiting for resources in a circle.  For example, P1 is holding Resource R1 and waiting for resource R2. Similarly, P2 holding resource R2 and waiting for resource R1 as represented in figure 1.Then it leads to deadlock.

No preemption

If we have set no priority for all processes, then every process demands that it will execute first and utilize the resources. Then it leads to deadlock.

necessary conditions to occur a deadlock
necessary conditions to occur a deadlock

Examples of deadlock