Deadlocks can be stopped before they happen, either by designing the system so that one condition can never hold, or by checking each request in advance.

Deadlock Prevention

Prevention removes one of the four conditions. Mutual exclusion cannot usually be removed, since some resources, such as printers, are not shareable by nature, though spooling helps. Hold and wait: require a process to request all its resources at once before it starts, or to release everything before requesting more. This lowers utilisation and may cause starvation. No preemption: if a process holding resources requests one that cannot be granted, it releases all that it holds, and they are preempted. This works for resources whose state can be saved, such as CPU registers and memory. Circular wait: number all resource types and require processes to request them in increasing order. This is simple and widely used, as in ordering locks.

Definition: Safe state. A state in which the system can allocate resources to every process, up to its maximum need, in some order (a safe sequence) and still avoid deadlock.

Deadlock Avoidance

Avoidance requires each process to declare in advance the maximum number of each resource that it may need. Before granting any request, the OS checks whether the resulting state would be safe. A state is safe if there is a safe sequence of all the processes, an order in which each process can obtain its remaining needs from the available resources plus those released by the processes before it. A safe state cannot lead to deadlock. An unsafe state may. Requests that would leave the system unsafe are made to wait.

The Banker's Algorithm

The Banker's algorithm, by Dijkstra, applies avoidance to resources with several instances. It uses these data structures for n processes and m resource types: Available[m], the free instances; Max[n][m], the maximum demand; Allocation[n][m], the instances held; and Need[n][m] = Max − Allocation. Safety algorithm: set Work = Available and Finish[i] = false for all i. Find an i with Finish[i] false and Need[i] ≤ Work. If found, set Work = Work + Allocation[i], Finish[i] = true, and repeat. If all Finish[i] become true, the state is safe. Resource-request algorithm: when Pi requests Request[i], check that Request ≤ Need and Request ≤ Available, pretend to grant it, and run the safety algorithm. Grant it only if the new state is safe.

Table 3.6: Banker's example: Available = (3, 3, 2)

Process Allocation (A B C) Max (A B C) Need (A B C)
P0 0 1 0 7 5 3 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

Example 3.6: Finding a safe sequence

Problem: Use the safety algorithm on the data in the table.

Solution:

  1. Work = (3, 3, 2). P1 needs (1, 2, 2) ≤ Work. Run P1: Work = (5, 3, 2).
  2. P3 needs (0, 1, 1) ≤ Work. Work = (7, 4, 3).
  3. P4 needs (4, 3, 1) ≤ Work. Work = (7, 4, 5).
  4. P0 needs (7, 4, 3) ≤ Work. Work = (7, 5, 5).
  5. P2 needs (6, 0, 0) ≤ Work. Work = (10, 5, 7).

Result: The safe sequence is P1, P3, P4, P0, P2, so the state is safe.

#include <stdio.h>
#define P 5
#define R 3
int main(void) {
    int alloc[P][R] = {{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
    int max[P][R] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
    int work[R] = {3, 3, 2}, fin[P] = {0}, seq[P], k = 0;
    while (k < P) {
        int found = 0;
        for (int i = 0; i < P; i++) {
            if (fin[i]) continue;
            int ok = 1;
            for (int j = 0; j < R; j++)
                if (max[i][j] - alloc[i][j] > work[j]) ok = 0;
            if (ok) {
                for (int j = 0; j < R; j++) work[j] += alloc[i][j];
                fin[i] = 1; seq[k++] = i; found = 1;
            }
        }
        if (!found) { printf("Unsafe state\n"); return 0; }
    }
    printf("Safe sequence: ");
    for (int i = 0; i < P; i++) printf("P%d ", seq[i]);
    printf("\n");
    return 0;
}

Output

Safe sequence: P1 P3 P4 P0 P2

Common mistakes

  • Thinking that an unsafe state is always a deadlock. It only may lead to one.
  • Forgetting to compute Need = Max − Allocation.
  • Granting a request without first checking that the new state is safe.

Key takeaways

  • Prevention breaks one of the four conditions, often circular wait by ordering resources.
  • Avoidance keeps the system in a safe state using declared maximum needs.
  • A safe sequence lets every process finish in turn.
  • The Banker's algorithm uses Available, Max, Allocation and Need.

Practice questions

  1. What is a safe state?
  2. How is Need computed in the Banker's algorithm?
  3. How can circular wait be prevented?

Answers to the practice questions

  1. A state in which there is a sequence in which all processes can obtain their maximum needs and finish.
  2. Need = Max − Allocation.
  3. By numbering resource types and requiring them to be requested in increasing order.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit III