What exactly is deadlock?

Deadlock is a state in which a task starts waiting for something that will not happen. As a result, it stops progressing.

Operating systems use multi-processing and multi-tasking for process completion, task completion, and to speed up work. When the two processes run concurrently and block each other’s paths, this condition is called deadlock.

In other words, deadlock is a condition when a process cannot be completed because it doesn’t get the resources it requires.

The conditions to declare a deadlock state are as follows:

  • Only one process can use the resource at a time, resulting in exclusive control of the resources.

  • Both processes hold the resources and wait for another process to leave hold of their resource.

  • Until the process completes its execution, it doesn’t give up on resources.

  • Each process in the chain holds a resource requested by another.

Understand deadlock with an example

Let’s understand deadlock with a simple example.

Example 1

Suppose two processes (process A and process B) are running simultaneously.

process A {
     update variable x
     update variable y

process B {
     update variable y
     update variable x

Since both processes are executing, process A will acquire a lock on variable x, and process B will acquire a lock on variable y.

Get hands-on with 1200+ tech skills courses.