![]() |
VOOZH | about |
Deadlock is a state in an operating system where two or more processes are stuck forever because each is waiting for a resource held by another. It happens only when four conditions exist: mutual exclusion, hold and wait, no preemption, and circular wait. For example, P1 holds R1 and needs R2, while P2 holds R2 and needs R1, so both wait forever. Deadlock can be handled using prevention, avoidance (Bankerโs algorithm), or detection and recovery.
A process in an operating system typically uses resources in the following sequence:
Deadlock arises when processes hold some resources while waiting for others.
There are several examples of deadlock. Some of them are mentioned below.
1. The system has 2 tape drives. P0 and P1 each hold one tape drive and each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as follows:
| P0 Action | P1 Action |
|---|---|
| wait(A) | wait(B) |
| wait(B) | wait(A) |
3. Assume the space is available for allocation of 200K bytes, and the following sequence of events occurs.
P0 | P1 |
|---|---|
Request 80KB; | Request 70KB; |
Request 60KB; | Request 80KB; |
Deadlock occurs if both processes progress to their second request.
Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions)