0% found this document useful (0 votes)
13 views

22BLC1012-Lab-06

The document describes a lab assignment for simulating the Banker's algorithm to determine if a system is in a safe state. It includes code that allows users to input the number of processes, types of resources, and their allocations, as well as functions to check safety and handle resource requests. The program outputs the need matrix, safe sequence, and handles requests while ensuring the system remains in a safe state.

Uploaded by

dpsvn.gaur12217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

22BLC1012-Lab-06

The document describes a lab assignment for simulating the Banker's algorithm to determine if a system is in a safe state. It includes code that allows users to input the number of processes, types of resources, and their allocations, as well as functions to check safety and handle resource requests. The program outputs the need matrix, safe sequence, and handles requests while ensuring the system remains in a safe state.

Uploaded by

dpsvn.gaur12217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB 6 Simulation of Bankers algorithm

Name: Aditya Gorasia


Reg. No.: 22BLC1012
Course Name: Operating Systems Lab
Slot: L9+L10
Faculty Name: Dr. Revathi M.
Course Code: BCSE303P

1. Simulate the Banker's algorithm to check whether the given system is in safe state
or not. Display the need matrix and safe sequence. Check if a request from process
arrives, can the request be granted immediately? Program should get the number of
process, type of resources and number of instances of each resources from the user.
The user should also provide maximum number of instances of each resource
required by the process and the allocated instances
CODE:

#include <stdio.h>
#include <stdbool.h>

void printMatrix(int n, int m, int matrix[n][m], const char* title) {


printf("\n%s:\n", title);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%4d", matrix[i][j]);
}
printf("\n");
}
}

void printArray(int n, int arr[n], const char* title) {


printf("\n%s: ", title);
for (int i = 0; i < n; i++) {
printf("%4d", arr[i]);
}
printf("\n");
}

bool isSafe(int n, int m, int max[n][m], int alloc[n][m], int avail[m]) {


int need[n][m];
int work[m];
bool finish[n];
int safeSeq[n];
int count = 0;
int index = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
finish[i] = false;
}
for (int i = 0; i < m; i++) {
work[i] = avail[i];
}
while (count < n) {
bool found = false;
for (int i = 0; i < n; i++) {
if (!finish[i]) {
// Check if need[i] can be satisfied with work
bool possible = true;
for (int j = 0; j < m; j++) {
if (need[i][j] > work[j]) {
possible = false;
break;
}
}

if (possible) {
for (int k = 0; k < m; k++) {
work[k] += alloc[i][k];
}

safeSeq[index++] = i;
finish[i] = true;
found = true;
count++;
}
}
}

if (!found) {
printf("\nThe system is NOT in a safe state!\n");
return false;
}
}
printf("\nThe system is in a safe state.\nSafe sequence: ");
for (int i = 0; i < n; i++) {
printf("P%d ", safeSeq[i]);
}
printf("\n");

return true;
}

void processRequest(int n, int m, int max[n][m], int alloc[n][m], int avail[m], int need[n][m]) {
int process, request[m];
printf("\nEnter the process number (0 to %d) that is making a new request: ", n-1);
scanf("%d", &process);

printf("Enter the new request for process P%d:\n", process);


for (int i = 0; i < m; i++) {
scanf("%d", &request[i]);
}
printf("\nRequest from P%d: ", process);
for (int i = 0; i < m; i++) {
printf("%4d", request[i]);
}
printf("\n");

for (int i = 0; i < m; i++) {


if (request[i] > need[process][i]) {
printf("Request cannot be granted as it exceeds the process's need.\n");
return;
}
}
for (int i = 0; i < m; i++) {
if (request[i] > avail[i]) {
printf("Cannot be granted immediately. Need to wait for resources to be free.\n");
return;
}
}
for (int i = 0; i < m; i++) {
avail[i] -= request[i];
alloc[process][i] += request[i];
need[process][i] -= request[i];
}
printf("\nNew Available Resources after granting the request:\n");
printArray(m, avail, "Available");

printf("\nNew Allocation Matrix after granting the request:\n");


printMatrix(n, m, alloc, "Allocation");

printf("\nNew Need Matrix after granting the request:\n");


printMatrix(n, m, need, "Need");
if (!isSafe(n, m, max, alloc, avail)) {
printf("\nThe request cannot be granted as it leads to an unsafe state.\n");
for (int i = 0; i < m; i++) {
avail[i] += request[i];
alloc[process][i] -= request[i];
need[process][i] += request[i];
}
}
else {
printf("\nThe request has been granted, and the system is in a safe state.\n");
}
}
int main() {
int n, m;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resource types: ");
scanf("%d", &m);

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


int originalAvail[m], originalAlloc[n][m], originalNeed[n][m];
printf("Enter the Max Matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the Available Resources:\n");
for (int i = 0; i < m; i++) {
scanf("%d", &avail[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
originalAlloc[i][j] = alloc[i][j];
originalNeed[i][j] = max[i][j] - alloc[i][j];
}
}
for (int i = 0; i < m; i++) {
originalAvail[i] = avail[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printMatrix(n, m, max, "Max Matrix");
printMatrix(n, m, alloc, "Allocation Matrix");
printMatrix(n, m, need, "Need Matrix");
while(true){
processRequest(n, m, max, alloc, avail, need);
}
return 0;
}

OUTPUT:

You might also like