0% found this document useful (0 votes)
13 views4 pages

bankers

The document contains a C program that implements the Banker's algorithm for resource allocation and deadlock avoidance. It checks if the system is in a safe state and processes resource requests from users while ensuring that the system remains safe. The program prompts users to input the number of processes, resources, allocation and maximum matrices, and available resources, followed by a resource request from a specific process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

bankers

The document contains a C program that implements the Banker's algorithm for resource allocation and deadlock avoidance. It checks if the system is in a safe state and processes resource requests from users while ensuring that the system remains safe. The program prompts users to input the number of processes, resources, allocation and maximum matrices, and available resources, followed by a resource request from a specific process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdbool.h>

#define MAX_PROCESSES 5
#define MAX_RESOURCES 3

// Function to check if the system is in a safe state


bool isSafe(int processes, int resources, int allocation[processes][resources], int max[processes]
[resources], int available[resources]) {
int work[resources], finish[processes];
for (int i = 0; i < resources; i++) {
work[i] = available[i];
}

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


finish[i] = 0; // All processes are initially unfinished
}

int count = 0;
while (count < processes) {
bool found = false;
for (int i = 0; i < processes; i++) {
if (!finish[i]) {
int canExecute = 1;
for (int j = 0; j < resources; j++) {
if (max[i][j] - allocation[i][j] > work[j]) {
canExecute = 0;
break;
}
}
if (canExecute) {
for (int k = 0; k < resources; k++) {
work[k] += allocation[i][k]; // Release resources
}
finish[i] = 1;
found = true;
count++;
}
}
}
if (!found) {
return false; // No safe sequence found
}
}
return true; // Safe sequence exists
}

// Function to check if the request can be granted


bool requestResources(int processes, int resources, int allocation[processes][resources], int
max[processes][resources], int available[resources], int request[resources], int processIndex) {
// Check if request is less than or equal to the need
for (int i = 0; i < resources; i++) {
if (request[i] > max[processIndex][i] - allocation[processIndex][i]) {
printf("Error: Process has exceeded maximum claim.\n");
return false;
}
}

// Check if request is less than or equal to available resources


for (int i = 0; i < resources; i++) {
if (request[i] > available[i]) {
printf("Resources are not available.\n");
return false;
}
}

// Pretend to allocate the resources


for (int i = 0; i < resources; i++) {
available[i] -= request[i];
allocation[processIndex][i] += request[i];
}

// Check if the system is in a safe state


if (isSafe(processes, resources, allocation, max, available)) {
printf("Request granted.\n");
return true;
} else {
// Rollback the allocation if not in safe state
for (int i = 0; i < resources; i++) {
available[i] += request[i];
allocation[processIndex][i] -= request[i];
}
printf("Request denied. The system would be in an unsafe state.\n");
return false;
}
}

int main() {
int processes, resources;
printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);

int allocation[processes][resources], max[processes][resources], available[resources];

// Input allocation matrix


printf("Enter the allocation matrix:\n");
for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) {
scanf("%d", &allocation[i][j]);
}
}

// Input max matrix


printf("Enter the max matrix:\n");
for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) {
scanf("%d", &max[i][j]);
}
}

// Input available resources


printf("Enter the available resources:\n");
for (int i = 0; i < resources; i++) {
scanf("%d", &available[i]);
}

// Request resources from the user


int processIndex;
int request[resources];
printf("Enter the process number requesting resources: ");
scanf("%d", &processIndex);
processIndex--; // Adjust for 0-based index

printf("Enter the request for each resource (space-separated): ");


for (int i = 0; i < resources; i++) {
scanf("%d", &request[i]);
}

// Call the requestResources function to process the request


requestResources(processes, resources, allocation, max, available, request, processIndex);
return 0;
}

You might also like