Introduction of Deadlock in Operating System



A deadlock refers to a situation of no progress that can happen in a multi-process systems, such as an operating system. It occurs when two or more entities (processes in the case of an OS) are each waiting indefinitely for resources held by each other. Read this chapter to understand what is deadlock, how it occurs in an operating system, effects of deadlock, and how to prevent it.

What is Deadlock in an OS?

In an operating system, deadlock is a state in which two or more processes are unable to proceed because each is waiting for a resource that the other holds, resulting in a cycle of dependencies. The processes involved in the deadlock situation are said to be deadlocked because they cannot continue execution without intervention from the operating system.

The image below shows a deadlock situation between two processes P1 and P2 −

DeadLock Example

In this example, Process P1 holds Resource R2 and is waiting for Resource R1 to be ready. Meanwhile, Process P2 holds Resource R1 and is waiting for Resource R2 to be ready. That's the deadlock situation.

Real-life Example of Deadlock

Alice and Bob are siblings. Alice has pencil and Bob has eraser. To complete their homework, each of them needs both pencil and eraser simultaneously. None of them is willing to share their stationery with the other. This situation can be represented as below −

Alice - Has Pencil, Needs Eraser
Bob   - Has Eraser, Needs Pencil

Alice -> Waiting for Bob to complete his homework
Bob   -> Waiting for Alice to complete her homework

Result: None of them completes homework.

This is a classic example of deadlock happening in real life. So deadlock is not something that happens only inside a computer system. There are many real-life situations where deadlock can occur.

Conditions for Deadlock

To occur deadlock in an operating system, four necessary conditions must hold simultaneously. These conditions are often referred to as the Coffman conditions, named after the researchers who defined them −

  • Mutual Exclusion − At least one resource must be held in a non-shareable mode, meaning only one process can use it at a time. Example: A printer can only be used by one process at a time.
  • Hold and Wait − A process holding at least one resource is waiting to acquire additional resources that are currently being held by other processes. Example: Process A holds a printer and waits for a scanner, while Process B holds a scanner and waits for the printer.
  • No Preemption − Resources cannot be forcibly taken away from processes holding them. A process must release the resource voluntarily when it is no longer needed. Example: If Process A is holding a printer and is waiting for a scanner, the system cannot force Process A to give up the printer.
  • Circular Wait − A set of processes exist such that each process in the set is waiting for a resource that another process in the set holds, forming a cycle of dependencies. Example: Process A waits for Process 's resource, Process B waits for Process 's resource, and Process C waits for Process 's resource, forming a circular chain.

Effects of Deadlock

The negative effects of a deadlock are listed below −

  • System Inefficiency − Deadlock leads to the wastage of resources that are held by processes that cannot make progress.
  • Process Starvation − Some processes may never get the resources they need if they are involved in a deadlock.
  • System Unresponsiveness − The system may appear frozen or unresponsive because processes cannot complete their tasks due to deadlock.

How to Prevent Deadlock

One can take the following precautionary measures to prevent a deadlock from taking place −

  • Prevention − The operating system can prevent deadlock by ensuring that at least one of the Coffman conditions is not met. For example, preventing circular wait can be done by requiring processes to request all the resources they will need at once (no hold and wait).
  • Avoidance − Deadlock avoidance algorithms, such as the Banker's Algorithm, dynamically analyze resource requests to ensure the system never enters a state where deadlock could occur.
  • Detection and Recovery − Some systems allow deadlock to occur but periodically check for it. If deadlock is detected, the system takes action to recover, such as terminating a process or forcibly preempting resources.

Conclusion

In this chapter, we provided a brief overview of what a deadlock is, what are its negative effects, and how to prevent a deadlock state. The next few chapters will have more elaborate explanation of deadlocks and how to detect and recover from a deadlock.

Advertisements