========================================================================
EXPERIMENT NO: 07
AIM:
Write C programs to demonstrate the Banker’s Algorithm and recovery processes.
Author: Ajatshatru Arun Kaushik
Date: 10-03-2025
Source: bankersAlgo.c
========================================================================
Problem Statement:
Consider a process snapshot comprising 5+ processes and accessing 3+ resources. Each
resource can have multiple instances.
[1] Apply Banker’s Algorithm to check whether the process snapshot is in Safe State (not
deadlocked).
[2] Consider a new request from the queued process and determine whether the new request
can be granted immediately.
Use appropriate AVAILABLE[], MAX[][], and ALLOCATION[][] data.
========================================================================
SOURCE CODE
========================================================================
[1] Filename: bankersAlgo.c
#include <stdio.h>
#include <stdbool.h>
#define P 5
#define R 4
bool isSafe(int processes[], int avail[], int max[][R], int allot[][R]) {
int need[P][R], work[R], finish[P] = {0};
int safeSeq[P], count = 0;
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}
for (int i = 0; i < R; i++) {
work[i] = avail[i];
}
while (count < P) {
bool found = false;
for (int p = 0; p < P; p++) {
if (!finish[p]) {
bool canAllocate = true;
for (int j = 0; j < R; j++) {
if (need[p][j] > work[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int k = 0; k < R; k++) {
work[k] += allot[p][k];
}
safeSeq[count++] = p;
finish[p] = 1;
found = true;
}
}
}
if (!found) {
printf("System is not in a safe state\n");
return false;
}
}
printf("System is in a safe state.\nSafe sequence is: ");
for (int i = 0; i < P; i++) {
printf("P%d ", safeSeq[i]);
}
printf("\n");
return true;
}
bool requestGrant(int processes[], int avail[], int max[][R], int allot[][R], int processID, int request[]) {
int need[P][R];
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}
for (int i = 0; i < R; i++) {
if (request[i] > need[processID][i]) {
printf("Error: Process has exceeded its maximum claim.\n");
return false;
}
}
for (int i = 0; i < R; i++) {
if (request[i] > avail[i]) {
printf("Resources are not available for the request.\n");
return false;
}
}
for (int i = 0; i < R; i++) {
avail[i] -= request[i];
allot[processID][i] += request[i];
}
if (isSafe(processes, avail, max, allot)) {
printf("Request can be granted.\n");
return true;
} else {
for (int i = 0; i < R; i++) {
avail[i] += request[i];
allot[processID][i] -= request[i];
}
printf("Request cannot be granted (system would enter an unsafe state).\n");
return false;
}
}
int main() {
int processes[] = {0, 1, 2, 3, 4};
int max[][R] = {
{5, 1, 1, 7},
{3, 2, 1, 1},
{3, 3, 2, 1},
{4, 6, 1, 2},
{6, 3, 2, 5}
};
int allot[][R] = {
{3, 0, 1, 4},
{2, 2, 1, 0},
{3, 1, 2, 1},
{0, 5, 1, 0},
{4, 2, 1, 2}
};
int avail1[] = {0, 3, 0, 1};
int avail2[] = {1, 0, 0, 2};
printf("Checking for Available = {0, 3, 0, 1}\n");
isSafe(processes, avail1, max, allot);
printf("\nChecking for Available = {1, 0, 0, 2}\n");
isSafe(processes, avail2, max, allot);
int newRequest[] = {1, 0, 2, 0};
printf("\nChecking request from Process 1: {1, 0, 2, 0}\n");
requestGrant(processes, avail1, max, allot, 1, newRequest);
return 0;
}
========================================================================
OUTPUT
========================================================================
[1] Filename: bankersAlgo.c
ajatshatru-kaushik@Linux:~/Desktop/CS4A09/OS Lab Experiments/Pract_7$ gcc bankersAlgo.c -o
bankersAlgo
ajatshatru-kaushik@Linux:~/Desktop/CS4A09/OS Lab Experiments/Pract_5$ ./bankersAlgo
Checking for Available = {0, 3, 0, 1}
System is not in a safe state
Checking for Available = {1, 0, 0, 2}
System is in a safe state.
Safe sequence is: P1 P2 P3 P4 P0
Checking request from Process 1: {1, 0, 2, 0}
Error: Process has exceeded its maximum claim.
========================================================================
INFERENCE
========================================================================
The problem involves implementing the Banker's Algorithm to determine if a system of processes is in a
safe state, using the AVAILABLE[], MAX[][], and ALLOCATION[][] matrices. Additionally, it requires
handling new resource requests from processes and checking if these requests can be granted
immediately without causing a deadlock. The goal is to ensure the system remains in a safe state after
any new resource allocation.