Bankers Algorithm Project
Bankers Algorithm Project
Introduction
This project simulates the Banker's Algorithm for Deadlock Avoidance and Prevention. It allows dynamic management of
processes and resources, checking the system's safe state, and handling resource requests and releases.
Features
- Dynamic Process Addition and Deletion
- Resource Request and Release Simulation
- Safe State Checking
- Request Handling Statistics
- Menu-Driven Interface
Project Structure
This project is implemented in a single C file and is structured with the following major components:
- Utility Functions (Printing Matrices, Checking Safe State)
- Core Functions (Add Process, Request Resources, Release Resources)
- Main Menu for User Interaction
How to Run
1. Compile the C file using any standard compiler, e.g., GCC.
2. Run the executable and interact with the menu.
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_P 20
#define MAX_R 10
// Global Variables
int Allocation[MAX_P][MAX_R];
int Max[MAX_P][MAX_R];
int Need[MAX_P][MAX_R];
int Available[MAX_R];
int totalResources[MAX_R];
void printState() {
printf("\nProcess\tAllocation\tMax\t\tNeed\n");
printf("P%d\t", i);
printf("\t");
printf("\t");
printf("\n");
printf("\nAvailable: ");
printf("\n");
bool isSafe() {
int work[MAX_R];
work[i] = Available[i];
int count = 0;
if (!finish[i]) {
possible = false;
break;
if (possible) {
work[k] += Allocation[i][k];
finish[i] = true;
found = true;
count++;
if (!found)
return false;
return true;
void addProcess() {
if (n >= MAX_P) {
return;
scanf("%d", &Allocation[n][i]);
Available[i] -= Allocation[n][i];
if (!isSafe()) {
Available[i] += Allocation[n][i];
} else {
n++;
// Request resources
void requestResources() {
int pid;
int req[MAX_R];
scanf("%d", &pid);
if (pid >= n) {
return;
return;
return;
// Pretend to allocate
Available[i] -= req[i];
Allocation[pid][i] += req[i];
Need[pid][i] -= req[i];
if (isSafe()) {
requestsHandled++;
} else {
Available[i] += req[i];
Allocation[pid][i] -= req[i];
Need[pid][i] += req[i];
}
}
// Release resources
void releaseResources() {
int pid;
scanf("%d", &pid);
if (pid >= n) {
return;
Available[i] += Allocation[pid][i];
Allocation[i][j] = Allocation[i+1][j];
Max[i][j] = Max[i+1][j];
Need[i][j] = Need[i+1][j];
n--;
int main() {
int choice;
scanf("%d", &r);
Available[i] = totalResources[i];
while (1) {
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 5:
if (isSafe())
else
break;
case 6:
break;
case 7:
printf("\nExiting... Goodbye!\n");
exit(0);
default:
} }
return 0;