Operating System Interview Questions · Question 16

How can an operating system handle deadlocks?

Interview preparation resource from Gate Smashers.

Interview-ready answer

A system can prevent deadlock, avoid unsafe allocations, detect deadlock after it occurs, or ignore it when prevention costs more than occasional recovery.

Understand it clearly

Prevention structurally breaks a Coffman condition, for example by enforcing a global lock order. Avoidance examines each request and grants it only if the system remains in a safe state; Banker's algorithm is the textbook example but requires advance knowledge of maximum demands.

Detection allows requests normally and periodically searches wait-for or resource-allocation graphs for deadlock. Recovery can terminate one or more processes, roll back work, or preempt resources. General-purpose systems often combine practical prevention rules with timeouts and application-level recovery.

A practical system may combine strategies: impose lock ordering to prevent common cycles, use timeouts for selected operations and run detection for resources that cannot be strictly ordered. Recovery may then terminate a task, roll it back or preempt a resource where safe.