1. Gyanastra
  2. Operating Systems
  3. Deadlock Avoidance and Banker's Algorithm
  4. Deadlock Avoidance and Safe vs Unsafe States
Free lesson, no account needed 5 min read 12 practice questions after it
Asked in past papers23 times
  • GATE CSE202620212018201720142009200820072005199619921988
  • ISRO201720132011
  • UGC NET20192018201620152014201320112005

Operating Systems · Deadlock Avoidance and Banker's Algorithm

Deadlock Avoidance and Safe vs Unsafe States

Overview

Deadlock avoidance accepts that deadlock is possible but tries to avoid it at every step. The system classifies its state as safe or unsafe and only grants resource allocations that keep it in a safe state.

Key concepts

  • Deadlock avoidance: a strategy in which the system accepts that there is a chance of getting into a deadlock, but at every step it tries to avoid it.
    Whenever resources are allocated to a process, the system is sure that this allocation will not lead to deadlock.
  • Safe state: the system is in a safe state when all the processes could somehow run to completion with the resources available to them — there exists a way of allocating the resources so that every process runs to completion.
  • Unsafe state: there is no safety — for the processes present in the system there is no way the resources could be allocated so that they will run to completion.
  • State classification: the tool used for avoidance is distinguishing the states of the system into the two categories, safe and unsafe.

How it works

  1. Start from the idea that deadlock is not ruled out completely — the system could still reach a deadlock — so instead of preventing deadlock structurally, the system continuously avoids it.
  2. Classify the state of the system as either safe or unsafe. The decision procedure is: find out whether the system is in the safe state or the unsafe state.
  3. Apply the guiding rule: the system will never be allowed to go out of the safe state. Only those allocations that keep the system in a safe state are granted.
  4. Whenever a request for resources is made by a process, check that granting this allocation will not lead to deadlock; if it would, do not grant it at that step.
Figure. Safe, unsafe and deadlocked states. A safe state has a safe sequence, so no deadlock can occur. An unsafe state has no safe sequence and may lead to a deadlock, though not always. Every deadlocked state is unsafe, but not every unsafe state is deadlocked.

Worked examples

The decision procedure for a resource request:

  1. A process requests resources.
  2. Determine whether granting the allocation keeps the system in a safe state (all processes can still somehow run to completion).
  3. If the resulting state is safe, grant the allocation.
  4. If the resulting state would be unsafe, do not grant the allocation at that step.

Comparison

AspectSafe stateUnsafe state
MeaningAll processes can somehow run to completion using the available resourcesNo way of allocating the resources so that the processes will run to completion
SafetySystem is safeThere is no safety
What the system doesAllocations that keep the system here are permittedThe system must never be allowed to enter this state
Relation to deadlockNo deadlockDeadlock is possible (the state is not guaranteed deadlock-free)

Revision summary

  • Deadlock avoidance = there is a chance of deadlock, but at every step it is avoided.
  • The system must always be kept inside the safe state — it is never allowed to leave the safe state.
  • Safe state = every process can complete with the available resources;
    unsafe state = no allocation order lets them all complete.
  • Before every resource allocation, check that the allocation will not lead to deadlock; this check is what makes the scheme an avoidance scheme.
  • An unsafe state is not guaranteed deadlock — deadlock is only possible in it.

Previous-year exam insights

  • Banker's algorithm is a deadlock-avoidance algorithm (not prevention); it requires each process to declare its maximum resource need in advance and grants a request only if the resulting state is safe.
    Asked inGATE 2026ISRO 2017GATE 2008
  • Safety algorithm: Work = Available;
    Finish[i] = false for all i.
    Repeatedly find a process i with Finish[i] = false and Need[i] ≤ Work;
    set Work = Work + Allocation[i] and Finish[i] = true.
    If all Finish[i] become true, the system is safe; otherwise it is unsafe.
    Asked inGATE 2018GATE 2017GATE 2014UGC NET 2019
  • Resource-request algorithm: if Request[i] ≤ Need[i] and Request[i] ≤ Available, pretend to allocate (Available = Available − Request[i], Allocation[i] += Request[i], Need[i] −= Request[i]) and run the safety algorithm;
    grant only if the resulting state is safe, otherwise roll back and make the process wait.
    Asked inGATE 2014GATE 2007
  • Deadlock prevention (by breaking one of the four Coffman conditions: mutual exclusion, hold and wait, no preemption, circular wait) is more restrictive than deadlock avoidance; in prevention, requests are not granted based on the resulting state being safe.
    Asked inGATE 2026GATE 2008ISRO 2017
  • For n processes each needing at most k instances of a single resource type with m total instances, deadlock is impossible if m ≥ n(k − 1) + 1; equivalently, the maximum n with no deadlock is n ≤ (m − 1)/(k − 1) when k > 1.
    Asked inUGC NET 2018
  • A safe state is never deadlocked, but an unsafe state may or may not be deadlocked; therefore 'unsafe' does not imply 'deadlocked', and a system in an unsafe state can still complete if processes do not request their maximum needs.
    Asked inGATE 2017GATE 2009
Pages 1–2 of 2