Deadlock in Operating System
Detailed & Easy Notes
What is Deadlock?
Deadlock is a situation in which two or more processes are unable to proceed because each
process is waiting for a resource that is held by another process. As a result, none of the processes
can continue execution, and the system becomes stuck.
Why Deadlock Occurs
Deadlock occurs in systems where multiple processes run concurrently and compete for limited
resources such as CPU, memory, printers, files, or locks. It mostly happens with non-preemptable
resources.
Types of Resources
Preemptable resources are those that can be taken away without harming the process, such as
CPU. Non-preemptable resources cannot be taken forcibly, such as printers and mutex locks.
Deadlock Example
Consider two processes P1 and P2 and two resources R1 and R2. P1 holds R1 and requests R2,
while P2 holds R2 and requests R1. Both processes wait forever, causing a deadlock.
Necessary Conditions for Deadlock
Deadlock occurs only when four conditions are satisfied simultaneously: Mutual Exclusion, Hold
and Wait, No Preemption, and Circular Wait.
Mutual Exclusion
Mutual exclusion means that a resource can be used by only one process at a time. This condition
is necessary for deadlock to occur.
Hold and Wait
In hold and wait, a process holds one resource and waits for another resource held by a different
process.
No Preemption
No preemption means that resources cannot be forcibly taken away from a process; they must be
released voluntarily by the process.
Circular Wait
Circular wait occurs when a set of processes wait for resources in a circular chain, where each
process waits for a resource held by the next process.
Methods to Handle Deadlock
Deadlock can be handled using three approaches: Deadlock Prevention or Avoidance, Deadlock
Detection and Recovery, and Deadlock Ignorance.
Deadlock Prevention
Deadlock prevention aims to ensure that at least one of the necessary conditions for deadlock
never holds true by controlling resource allocation.
Deadlock Avoidance
Deadlock avoidance uses safe resource allocation strategies such as the Banker's Algorithm to
avoid unsafe states and possible deadlocks.
Deadlock Detection and Recovery
If deadlock prevention or avoidance is not used, the system detects deadlocks and recovers by
terminating processes or preempting resources.
Deadlock Ignorance
In deadlock ignorance, the system assumes deadlock is rare and ignores it. If deadlock occurs, the
system is rebooted. This approach is used by Windows and UNIX systems.
Safe State and Unsafe State
A system is in a safe state if all processes can complete in some order. An unsafe state means
deadlock may occur, but it does not guarantee deadlock.
Conclusion
Deadlock is a critical issue in operating systems that can halt system progress. Understanding its
conditions and handling methods helps in designing reliable systems.