-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankersAlgorithm.C
83 lines (76 loc) · 2.2 KB
/
BankersAlgorithm.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
int main()
{
int processes, resources;
printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);
int max[processes][resources];
int allocated[processes][resources];
int need[processes][resources];
int available[resources];
int work[resources];
int finish[processes];
// Input the maximum resource allocation for each process
for (int i = 0; i < processes; i++)
{
printf("Enter the maximum resource allocation for Process %d: ", i);
for (int j = 0; j < resources; j++)
{
scanf("%d", &max[i][j]);
}
finish[i] = 0;
}
// Input the allocated resources for each process
for (int i = 0; i < processes; i++)
{
printf("Enter the allocated resource for Process %d: ", i);
for (int j = 0; j < resources; j++)
{
scanf("%d", &allocated[i][j]);
need[i][j] = max[i][j] - allocated[i][j];
}
}
// Input the available resources
printf("Enter the available resources: ");
for (int i = 0; i < resources; i++)
{
scanf("%d", &available[i]);
work[i] = available[i];
}
int safe = 0;
while (safe < processes)
{
int found = 0;
for (int i = 0; i < processes; i++)
{
if (finish[i] == 0)
{
int j;
for (j = 0; j < resources; j++)
{
if (need[i][j] > work[j])
break;
}
if (j == resources)
{
for (int k = 0; k < resources; k++)
{
work[k] += allocated[i][k];
}
finish[i] = 1;
printf("Process %d is safe.\n", i);
safe++;
found = 1;
}
}
}
if (found == 0)
{
printf("The system is in an unsafe state.\n");
break;
}
}
return 0;
}