Sub topic 2
What a Deadlock Is, and What It Is Not
Assume you know basic operating-system concepts, including processes, threads, critical sections, locks, and semaphores. You should also be comfortable reading directed graphs and comparing resource counts.
Definition of deadlock
A deadlock is a state in which a group of processes or threads is permanently blocked because every member of the group is waiting for an event that another member of the same group must cause.
For example, suppose:
- Process P1 holds lock A and waits for lock B.
- Process P2 holds lock B and waits for lock A.
Neither process can continue. P1 cannot obtain B until P2 releases it, and P2 cannot obtain A until P1 releases it. If neither process releases its current lock, the system has a deadlock.
The important part is the cycle of dependencies. Each blocked process depends on another blocked process in the same cycle.
A process may wait for a resource, a message, an input event, or another process. Deadlock is not limited to locks. In operating-system problems, the resources are often printers, memory units, files, semaphores, or I/O devices.
Deadlock compared with related conditions
Several conditions look similar but have different causes and solutions.
| Condition | What happens | Can useful system progress continue? |
|---|---|---|
| Deadlock | Each process waits for another blocked process in a cycle | No progress for the processes in the cycle |
| Starvation | One process waits indefinitely while other processes continue | Yes, other processes can progress |
| Livelock | Processes keep changing state but accomplish no useful work | Usually no useful progress |
| Ordinary blocking | A process waits temporarily for an expected event | Yes, after the event occurs |
Starvation, also called indefinite postponement, occurs when a process is repeatedly denied access to a resource or the processor. For example, a scheduler might always choose newer or higher-priority processes. The waiting process does not necessarily hold a resource that another waiting process needs. Other processes may continue running normally.
Livelock occurs when processes are active rather than blocked, but their actions prevent useful progress. Consider two processes that detect a conflict and both repeatedly back off, retry, and collide again. Their states change, but the intended operation never completes.
Ordinary blocking is expected synchronization. A thread that waits for a lock held by a short-running critical section is blocked, but it is not deadlocked if the lock holder can finish and release the lock.
The dependency cycle
A useful working model represents each process as a node. Draw an edge from process P1 to process P2 when P1 waits for P2 to release a resource or perform an event. A cycle means that the processes depend on one another in a circular way.
For a single instance of each resource, such a cycle directly identifies deadlock. With multiple instances of a resource, a cycle may only indicate a possibility. Another process might still obtain an available instance and release resources, allowing the cycle to disappear. Later chapters formalize this distinction with resource-allocation graphs and matrix algorithms.
Practical test
To decide whether a situation is a deadlock, ask:
- Which process holds each needed resource?
- Which process is waiting for each resource or event?
- Can any waiting process proceed without help from another waiting process?
- Do the dependencies form a closed cycle?
- Is the waiting indefinite, or can normal execution eventually release the resource?
Exercise
P1 waits for a lock held by P2. P2 is running and will release the lock after completing its critical section. Is this a deadlock?
Show answer
No. P1 is temporarily blocked, but P2 can proceed and release the lock. The dependency is not a permanent cycle of blocked processes.