[go: up one dir, main page]

0% found this document useful (0 votes)
4 views8 pages

Bankers Safety Algo

The document contains a C program that implements the Banker's algorithm for deadlock avoidance in resource allocation. It allows users to input the number of processes and resources, their maximum resource requirements, and current allocations, then checks if the system is in a safe state. The program also handles resource requests from processes and determines if the requests can be granted without leading to an unsafe state.

Uploaded by

devyanibotre2004
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)
4 views8 pages

Bankers Safety Algo

The document contains a C program that implements the Banker's algorithm for deadlock avoidance in resource allocation. It allows users to input the number of processes and resources, their maximum resource requirements, and current allocations, then checks if the system is in a safe state. The program also handles resource requests from processes and determines if the requests can be granted without leading to an unsafe state.

Uploaded by

devyanibotre2004
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

#include <stdio.

h>

#include <stdbool.h>

#define MAX_PROCESSES 10

#define MAX_RESOURCES 10

int available[MAX_RESOURCES];

int max[MAX_PROCESSES][MAX_RESOURCES];

int allocation[MAX_PROCESSES][MAX_RESOURCES];

int need[MAX_PROCESSES][MAX_RESOURCES];

int processes, resources;

void input()

printf("Enter number of processes: ");

scanf("%d", &processes);

printf("Enter number of resources: ");

scanf("%d", &resources);

printf("Enter available resources: ");

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

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

printf("Enter maximum resource allocation matrix:");

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


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

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

printf("Enter allocation matrix:");

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

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

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

need[i][j] = max[i][j] - allocation[i][j];

bool is_safe()

bool finish[MAX_PROCESSES] = {false};

int work[MAX_RESOURCES];

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

work[i] = available[i];

int safe_sequence[MAX_PROCESSES];

int count = 0;

while (count < processes)

bool found = false;


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

if (!finish[i])

bool can_allocate = true;

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

if (need[i][j] > work[j])

can_allocate = false;

break;

if (can_allocate)

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

work[j] += allocation[i][j];

safe_sequence[count++] = i;

finish[i] = true;

found = true;

if (!found)

{
printf("System is not in a safe state!\n");

return false;

printf("System is in a safe state. Safe sequence: ");

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

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

printf("\n");

return true;

bool request_resources(int process_id, int request[])

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

if (request[i] > need[process_id][i] || request[i] > available[i])

return false;

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

available[i] -= request[i];

allocation[process_id][i] += request[i];

need[process_id][i] -= request[i];

}
if (is_safe())

return true;

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

available[i] += request[i];

allocation[process_id][i] -= request[i];

need[process_id][i] += request[i];

return false;

int main()

input();

if (!is_safe())

return 0;

int process_id;

int request[MAX_RESOURCES];

printf("Enter process ID making request: ");

scanf("%d", &process_id);

printf("Enter request for resources: ");

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

scanf("%d", &request[i]);
if (request_resources(process_id, request))

printf("Request granted.\n");

else

printf("Request denied.\n");

return 0;

}
Enter number of processes: 5

Enter number of resources: 3

Enter available resources: 3 3 2

Enter maximum resource allocation matrix:

753

322

902

222
433

Enter allocation matrix:

010

200

302

211

002

System is in a safe state. Safe sequence: 1 3 4 0 2

Enter process ID making request: 1

Enter request for resources: 1 0 2

Request granted.

System is in a safe state. Safe sequence: 1 3 4 0 2

You might also like