Bankers Safety Algo
Bankers Safety Algo
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];
void input()
scanf("%d", &processes);
scanf("%d", &resources);
scanf("%d", &available[i]);
scanf("%d", &max[i][j]);
scanf("%d", &allocation[i][j]);
bool is_safe()
int work[MAX_RESOURCES];
work[i] = available[i];
int safe_sequence[MAX_PROCESSES];
int count = 0;
if (!finish[i])
can_allocate = false;
break;
if (can_allocate)
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("\n");
return true;
return false;
available[i] -= request[i];
allocation[process_id][i] += request[i];
need[process_id][i] -= request[i];
}
if (is_safe())
return true;
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];
scanf("%d", &process_id);
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
753
322
902
222
433
010
200
302
211
002
Request granted.