What is DeadLock? What Situations Cause Deadlock?

DeadLock Definition:
DeadLock is a situation where two or more processes wait for the other one to finish, thus neither every does. If there are two processes A and B, if there is a deadlock, A would be waiting on B to complete while B would be waiting on A to complete.

What are the necessary conditions for the deadlock to occur?
For a deadlock to occur, all of the following four conditions, known as Coffman conditions, must hold.

  1. Mutual Exclusion:
    Only on process can access a resource at a time.
  2. Hold and Wait:
    A process is currently holding a resource and is requesting another resource hold by another process.
  3. No Pre-emption:
    The holding process does not voluntarily release the resource, and one process cannot force another process to release the resource.
  4. Circular Wait:
    Two or more processes form a circular chain, waiting on the other resource on the chain.

How to prevent deadlocks?
This can be done by avoiding one of the conditions above. The most common method is to avoid circular wait.