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

Bankers Algorithm Project

The document describes a project that simulates the Banker's Algorithm for deadlock avoidance and prevention, featuring dynamic process management, resource request handling, and safe state checking. It includes a menu-driven interface and is implemented in a single C file with core and utility functions. Instructions for compiling and running the program are provided, along with the source code.

Uploaded by

rahul sharma
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)
19 views

Bankers Algorithm Project

The document describes a project that simulates the Banker's Algorithm for deadlock avoidance and prevention, featuring dynamic process management, resource request handling, and safe state checking. It includes a menu-driven interface and is implemented in a single C file with core and utility functions. Instructions for compiling and running the program are provided, along with the source code.

Uploaded by

rahul sharma
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/ 8

Banker's 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];

int n = 0; // Number of processes

int r = 0; // Number of resource types


int requestsHandled = 0;

// Function to print current matrices

void printState() {

printf("\nProcess\tAllocation\tMax\t\tNeed\n");

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

printf("P%d\t", i);

for (int j = 0; j < r; j++)

printf("%d ", Allocation[i][j]);

printf("\t");

for (int j = 0; j < r; j++)

printf("%d ", Max[i][j]);

printf("\t");

for (int j = 0; j < r; j++)

printf("%d ", Need[i][j]);

printf("\n");

printf("\nAvailable: ");

for (int i = 0; i < r; i++)

printf("%d ", Available[i]);

printf("\n");

// Check system safety

bool isSafe() {

int work[MAX_R];

bool finish[MAX_P] = {false};

for (int i = 0; i < r; i++)

work[i] = Available[i];

int count = 0;

while (count < n) {

bool found = false;


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

if (!finish[i]) {

bool possible = true;

for (int j = 0; j < r; j++) {

if (Need[i][j] > work[j]) {

possible = false;

break;

if (possible) {

for (int k = 0; k < r; k++)

work[k] += Allocation[i][k];

finish[i] = true;

found = true;

count++;

if (!found)

return false;

return true;

// Add new process

void addProcess() {

if (n >= MAX_P) {

printf("\nMax process limit reached!\n");

return;

printf("\nEnter Maximum need for P%d:\n", n);

for (int i = 0; i < r; i++)


scanf("%d", &Max[n][i]);

printf("Enter Allocation for P%d:\n", n);

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

scanf("%d", &Allocation[n][i]);

Need[n][i] = Max[n][i] - Allocation[n][i];

Available[i] -= Allocation[n][i];

if (!isSafe()) {

printf("\nSystem not safe after adding! Rolling back.\n");

for (int i = 0; i < r; i++)

Available[i] += Allocation[n][i];

} else {

n++;

printf("\nProcess P%d added successfully.\n", n-1);

// Request resources

void requestResources() {

int pid;

int req[MAX_R];

printf("\nEnter Process number: ");

scanf("%d", &pid);

if (pid >= n) {

printf("Invalid Process ID!\n");

return;

printf("Enter resources to request:\n");

for (int i = 0; i < r; i++)


scanf("%d", &req[i]);

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

if (req[i] > Need[pid][i]) {

printf("Request exceeds need!\n");

return;

if (req[i] > Available[i]) {

printf("Resources not available!\n");

return;

// Pretend to allocate

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

Available[i] -= req[i];

Allocation[pid][i] += req[i];

Need[pid][i] -= req[i];

if (isSafe()) {

printf("\nResources allocated successfully!\n");

requestsHandled++;

} else {

printf("\nUnsafe after allocation. Rolling back.\n");

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

Available[i] += req[i];

Allocation[pid][i] -= req[i];

Need[pid][i] += req[i];

}
}

// Release resources

void releaseResources() {

int pid;

printf("\nEnter Process number to release all resources: ");

scanf("%d", &pid);

if (pid >= n) {

printf("Invalid Process ID!\n");

return;

for (int i = 0; i < r; i++)

Available[i] += Allocation[pid][i];

for (int i = pid; i < n-1; i++) {

for (int j = 0; j < r; j++) {

Allocation[i][j] = Allocation[i+1][j];

Max[i][j] = Max[i+1][j];

Need[i][j] = Need[i+1][j];

n--;

printf("\nProcess P%d removed and resources released.\n", pid);

int main() {

int choice;

printf("\nEnter number of resource types: ");

scanf("%d", &r);

printf("Enter total available resources:\n");

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


scanf("%d", &totalResources[i]);

Available[i] = totalResources[i];

while (1) {

printf("\n\n========= BANKER'S ALGORITHM SIMULATOR =========\n");

printf("1. View Current State\n");

printf("2. Add New Process\n");

printf("3. Request Resources\n");

printf("4. Release Resources\n");

printf("5. View System Safety\n");

printf("6. View Statistics\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: printState(); break;

case 2: addProcess(); break;

case 3: requestResources(); break;

case 4: releaseResources(); break;

case 5:

if (isSafe())

printf("\nSystem is currently SAFE.\n");

else

printf("\nSystem is currently UNSAFE!\n");

break;

case 6:

printf("\nTotal Requests Handled: %d\n", requestsHandled);

break;

case 7:

printf("\nExiting... Goodbye!\n");

exit(0);
default:

printf("\nInvalid choice! Try again.\n");

} }

return 0;

Done By: Jayant Sharma


23WJ1A6731
CSD-1

You might also like