Deadlocks and resource safety
A blocked task can hold exactly the resource another task needs. Deadlock handling differs from starvation and livelock because the cycle is a resource-allocation fact. We will identify the four conditions, compare handling strategies, compute Banker’s safe sequence and separate lack of progress from lack of fairness.
#Section 17Classic synchronization problems
The bounded-buffer producer-consumer problem has a finite number of slots. Producers need an empty slot, consumers need a full slot, and both must change the queue safely. A mutex protects the queue structure while counting semaphores track empty and full permits. Readers-writers adds a policy question: allowing many readers can starve a writer, while writer preference can delay readers.
Dining philosophers exposes a circular wait. If each philosopher holds one fork and waits for the next, five participants can wait forever. Resource ordering breaks the cycle by requiring forks to be acquired in a global order. The sleeping-barber variant adds a finite waiting room and tests whether arriving work is admitted or turned away.
The lesson is to name the resource and the invariant before choosing a primitive. A semaphore that counts queue slots does not by itself protect the queue links. A readers-writer lock that admits readers concurrently still needs a policy for writers.
#Section 18Deadlock and Coffman conditions
Finch can deadlock if process P1 holds resource A and waits for B while P2 holds B and waits for A. Deadlock means every task in the cycle waits for an event only another task in the cycle can cause. The classical cycle needs mutual exclusion, hold and wait, no preemption and circular wait.
Mutual exclusion makes at least one resource non-shareable. Hold and wait lets a task retain one resource while requesting another. No preemption means the system cannot safely take a held resource away. Circular wait closes the loop. Remove any one condition and this particular deadlock cannot form, though another failure such as starvation may remain.
Lock ordering is a practical prevention rule. If every path acquires locks in ascending order, no task can wait for an earlier lock while retaining a later lock. The rule must include every lock and callback path, or the missing edge remains a production failure.
#Section 19Handling deadlocks
An operating system can ignore deadlocks, prevent them by breaking a condition, avoid them by admitting only safe allocations, or detect and recover after a cycle appears. Ignoring can be reasonable when recovery is cheap and the failure is rare. Prevention adds ordering, preemption or one-shot allocation constraints that may reduce flexibility.
Avoidance requires knowledge of maximum future claims, which is why it fits a teaching model better than many general-purpose services. Detection can build a wait-for graph, choose a victim, roll it back or terminate it. Recovery must release resources and preserve system invariants, not merely kill an arbitrary process.
Starvation can appear in a prevention scheme if one class always loses access. A good design states both safety and fairness, then measures the cost of the chosen guarantee.
Consider the lock-order rule for Finch’s connection table and output queue. Taking the connection lock first on every path prevents a worker from holding the output lock while requesting the connection lock. A callback that silently takes them in the opposite order invalidates the proof. Prevention is therefore a rule about all acquisition paths, not just the code visible in the first example.
Detection works differently. The system observes waiting relationships and searches for a cycle under its resource model. Breaking the cycle requires an operation that actually releases a needed resource. A transaction can sometimes roll back safely, while killing a thread that owns a user mutex may leave corrupted state behind. Select recovery according to the resource’s ownership and rollback contract.
#Section 20Banker’s algorithm
Banker’s algorithm asks whether a hypothetical allocation leaves some order in which every process can finish. Need equals maximum claim minus current allocation. With available work equal to 1, process B needs 1, so B can finish and release its allocation. Work becomes 2, then A’s need 2 can finish and work becomes 3, then C’s need 2 can finish and work becomes 4.
The resulting safe sequence is B, A, C. “Safe” does not mean no process is waiting now. It means the declared maximum claims admit at least one completion order. An unsafe state cannot guarantee avoiding deadlock under every future request, although it may not contain a cycle at this instant.
The algorithm is a proof about an input model, not a promise that real applications reveal maximum claims accurately. If the claims are false, the safety result is false. If the request is smaller than the declared maximum, the system can evaluate the request and then rerun the safety test.
#Section 21Starvation and livelock
Starvation means a particular task waits indefinitely while the system continues serving others. Priority scheduling without aging can starve a low-priority task. Livelock means tasks remain active, repeatedly react and still make no useful progress, as when two polite workers step aside for one another forever. Deadlock means the tasks are stuck waiting on a cycle.
Fair locks, aging, bounded retries and backoff address different failures. A retry loop can convert a lock collision into livelock if every worker retries at the same instant. Randomized backoff changes timing but does not prove fairness. A queue with explicit ownership can provide a stronger bound.
Diagnosis needs observations: who holds the resource, who waits, how long, and whether useful state changes. Calling every long wait a deadlock produces the wrong fix.