
298 views
Bankers Algorithm in C
The Banker’s algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems to ensure that processes request and release resources in a safe and deadlock-free manner. Here’s a simple implementation of the Banker’s algorithm in C:
C
#include <stdio.h>
// Define the maximum number of resources and processes
#define MAX_RESOURCES 5
#define MAX_PROCESSES 5
// Available resources
int available[MAX_RESOURCES];
// Maximum demand matrix
int maximum[MAX_PROCESSES][MAX_RESOURCES];
// Allocated resources matrix
int allocation[MAX_PROCESSES][MAX_RESOURCES];
// Need matrix
int need[MAX_PROCESSES][MAX_RESOURCES];
// Number of resources and processes
int num_resources;
int num_processes;
// Function to check if a request can be granted safely
int isSafe(int process, int request[]) {
int work[MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
// Initialize work and finish arrays
for (int i = 0; i < num_resources; i++) {
work[i] = available[i];
}
// Check if the request can be granted
for (int i = 0; i < num_resources; i++) {
if (request[i] > need[process][i] || request[i] > work[i]) {
return 0; // Request cannot be granted
}
}
// Simulate resource allocation and release
for (int i = 0; i < num_resources; i++) {
work[i] -= request[i];
allocation[process][i] += request[i];
need[process][i] -= request[i];
}
// Check if the system remains in a safe state
int count = 0;
while (count < num_processes) {
int found = 0;
for (int i = 0; i < num_processes; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < num_resources; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == num_resources) {
for (int k = 0; k < num_resources; k++) {
work[k] += allocation[i][k];
}
finish[i] = 1;
found = 1;
count++;
}
}
}
if (!found) {
return 0; // System is not in a safe state
}
}
return 1; // Request can be granted safely
}
// Function to allocate resources to a process
void allocateResources(int process, int request[]) {
if (isSafe(process, request)) {
for (int i = 0; i < num_resources; i++) {
available[i] -= request[i];
allocation[process][i] += request[i];
need[process][i] -= request[i];
}
printf("Request granted to process %d\n", process);
} else {
printf("Request denied to process %d\n", process);
}
}
int main() {
// Initialize the number of resources and processes
num_resources = 3;
num_processes = 5;
// Initialize available resources
available[0] = 3;
available[1] = 3;
available[2] = 2;
// Initialize maximum demand matrix
maximum[0][0] = 7; maximum[0][1] = 5; maximum[0][2] = 3;
maximum[1][0] = 3; maximum[1][1] = 2; maximum[1][2] = 2;
maximum[2][0] = 9; maximum[2][1] = 0; maximum[2][2] = 2;
maximum[3][0] = 2; maximum[3][1] = 2; maximum[3][2] = 2;
maximum[4][0] = 4; maximum[4][1] = 3; maximum[4][2] = 3;
// Initialize allocated resources matrix
allocation[0][0] = 0; allocation[0][1] = 1; allocation[0][2] = 0;
allocation[1][0] = 2; allocation[1][1] = 0; allocation[1][2] = 0;
allocation[2][0] = 3; allocation[2][1] = 0; allocation[2][2] = 2;
allocation[3][0] = 2; allocation[3][1] = 1; allocation[3][2] = 1;
allocation[4][0] = 0; allocation[4][1] = 0; allocation[4][2] = 2;
// Initialize need matrix
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
need[i][j] = maximum[i][j] - allocation[i][j];
}
}
// Request resources for a process (you can change the process and request)
int process = 1;
int request[3] = {1, 0, 2};
allocateResources(process, request);
return 0;
}
In this program:
- The Banker’s algorithm is implemented using the
isSafe
function to check if a resource request can be granted safely without causing a deadlock. TheallocateResources
function is used to allocate resources to a process if the request is safe. - The
available
,maximum
,allocation
, andneed
arrays represent the available resources, maximum demand matrix, allocated resources matrix, and need matrix, respectively. - The program initializes these arrays with sample data and then demonstrates a resource request for a specific process using the
allocateResources
function.
Please note that this is a simplified example for educational purposes. In real operating systems, the Banker’s algorithm is used to manage resource allocation for multiple processes running concurrently.