What are deadlocks, and how can they occur

Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.

Deadlock can arise if the following four conditions hold simultaneously:

  • Mutual Exclusion: One or more than one resource are non-shareable (Only one process can use at a time)
  • Hold and Wait: A process is holding at least one resource and waiting for resources.
  • No Preemption: A resource cannot be taken from a process unless the process releases the resource.
  • Circular Wait: A set of processes are waiting for each other in circular form.

Deadlocks can happen with Lock interface NonReentrant locks, when a non-reentrant locks tries to gain a new lock.