Lab 7 walkthrough — Banker's algorithm for deadlock avoidance
Lab 7 walkthrough — Banker's algorithm for deadlock avoidance
Recorded by Dr. Krithika L.B. Slot lab07-walkthrough
Theory link: 2.6 Deadlocks
Lab 7 walkthrough — Banker's algorithm for deadlock avoidance
Recorded by Dr. Krithika L.B. Slot lab07-walkthrough
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.
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).
/* 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;
}gcc banker.c -o banker./bankerNeed 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.Requesting process (-1 to skip): 4
Request vector: 3 3 0
System is NOT in a safe state.
Request DENIED: P4 must wait.Prepare these for the lab evaluation.
Revealed 0Like a banker, it never allocates cash so that it could not satisfy every customer's maximum need in some order.
No, but deadlock may occur; the algorithm avoids unsafe states entirely.
Their maximum need for each resource type.
O(m × n²).