Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 7: Banker's algorithm for deadlock avoidance

Theory link: 2.6 Deadlocks

Experiment 7 of 15 Module 2 2 hours

Video walkthrough

Lab 7 walkthrough — Banker's algorithm for deadlock avoidance

Video coming soon

Lab 7 walkthrough — Banker's algorithm for deadlock avoidance

Recorded by Dr. Krithika L.B. Slot lab07-walkthrough

Aim

Aim. To implement the Banker's safety algorithm and the resource-request algorithm, deciding whether the system is in a safe state and whether a request can be granted.

Explanation

Inputs: Allocation and Max (n × m) and Available (m). Compute Need = Max − Allocation.

Safety: Work = Available; repeatedly find an unfinished process with Need ≤ Work, add its allocation to Work and mark it finished. Safe if all finish.

Request: reject if Request > Need; wait if Request > Available; otherwise pretend to allocate, run the safety check, and grant only if safe (else roll back).

Code

banker.c
/* banker.c - Banker's algorithm: safety check and resource-request granting (BACSE106 Lab 7) */
#include <stdio.h>

int n, m;
int alloc[10][10], max[10][10], need[10][10], avail[10];

int is_safe(void)                     /* returns 1 if safe and prints the safe sequence */
{
    int work[10], finish[10] = {0}, seq[10], count = 0, i, j, found;
    for (j = 0; j < m; j++) work[j] = avail[j];
    do {
        found = 0;
        for (i = 0; i < n; i++) {
            if (finish[i]) continue;
            for (j = 0; j < m; j++)
                if (need[i][j] > work[j]) break;
            if (j == m) {                         /* Need[i] <= Work */
                for (j = 0; j < m; j++) work[j] += alloc[i][j];
                finish[i] = 1;
                seq[count++] = i;
                found = 1;
            }
        }
    } while (found);
    if (count < n) { printf("System is NOT in a safe state.\n"); return 0; }
    printf("System is in a SAFE state.\nSafe sequence: ");
    for (i = 0; i < n; i++) printf("P%d%s", seq[i], i < n - 1 ? " -> " : "\n");
    return 1;
}

int main(void)
{
    int i, j, p, req[10];

    printf("Number of processes and resource types: ");
    scanf("%d %d", &n, &m);
    printf("Allocation matrix:\n");
    for (i = 0; i < n; i++) for (j = 0; j < m; j++) scanf("%d", &alloc[i][j]);
    printf("Max matrix:\n");
    for (i = 0; i < n; i++) for (j = 0; j < m; j++) scanf("%d", &max[i][j]);
    printf("Available: ");
    for (j = 0; j < m; j++) scanf("%d", &avail[j]);

    printf("\nNeed matrix:\n");
    for (i = 0; i < n; i++) {
        printf("P%d: ", i);
        for (j = 0; j < m; j++) { need[i][j] = max[i][j] - alloc[i][j]; printf("%d ", need[i][j]); }
        printf("\n");
    }
    is_safe();

    printf("\nRequesting process (-1 to skip): ");
    scanf("%d", &p);
    if (p < 0) return 0;
    printf("Request vector: ");
    for (j = 0; j < m; j++) scanf("%d", &req[j]);

    for (j = 0; j < m; j++)
        if (req[j] > need[p][j]) { printf("Error: P%d exceeded its maximum claim.\n", p); return 0; }
    for (j = 0; j < m; j++)
        if (req[j] > avail[j]) { printf("Resources not available: P%d must wait.\n", p); return 0; }

    for (j = 0; j < m; j++) {               /* pretend to allocate */
        avail[j] -= req[j]; alloc[p][j] += req[j]; need[p][j] -= req[j];
    }
    if (is_safe())
        printf("Request GRANTED to P%d.\n", p);
    else {
        for (j = 0; j < m; j++) {           /* roll back */
            avail[j] += req[j]; alloc[p][j] -= req[j]; need[p][j] += req[j];
        }
        printf("Request DENIED: P%d must wait.\n", p);
    }
    return 0;
}

How to run

  1. gcc banker.c -o banker
  2. ./banker
  3. Enter n and m, the Allocation rows, the Max rows, Available, then a request (or −1).

Sample input & output

Safe state, request granted

Input
5 3 · Allocation 0 1 0 / 2 0 0 / 3 0 2 / 2 1 1 / 0 0 2 · Max 7 5 3 / 3 2 2 / 9 0 2 / 2 2 2 / 4 3 3 · Available 3 3 2 · request P1: 1 0 2
Output
Output
Need matrix:
P0: 7 4 3
P1: 1 2 2
P2: 6 0 0
P3: 0 1 1
P4: 4 3 1
System is in a SAFE state.
Safe sequence: P1 -> P3 -> P4 -> P0 -> P2

Requesting process (-1 to skip): 1
Request vector: 1 0 2
System is in a SAFE state.
Safe sequence: P1 -> P3 -> P4 -> P0 -> P2
Request GRANTED to P1.

Request leads to unsafe state

Input
same matrices · request P4: 3 3 0
Output
Output
Requesting process (-1 to skip): 4
Request vector: 3 3 0
System is NOT in a safe state.
Request DENIED: P4 must wait.

Try it yourself

  1. Print the Work vector after each process finishes.
  2. Allow several requests in a loop until the user enters −1.
  3. Implement the deadlock-detection algorithm (use a Request matrix instead of Need).

Viva questions

Prepare these for the lab evaluation.

Revealed 0
Why is it called the Banker's algorithm?

Like a banker, it never allocates cash so that it could not satisfy every customer's maximum need in some order.

Is an unsafe state always deadlocked?

No, but deadlock may occur; the algorithm avoids unsafe states entirely.

What information must processes declare in advance?

Their maximum need for each resource type.

Time complexity of the safety algorithm?

O(m × n²).