-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankersAlgorithm.cpp
76 lines (66 loc) · 1.89 KB
/
BankersAlgorithm.cpp
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
#include <cstring>
#include <iostream>
using namespace std;
bool findSafeSequence(int max[][10], int allocated[][10], int available[], int m, int n, int safeSeq[])
{
bool finish[n];
for (int i = 0; i < n; i++)
finish[i] = false;
int count = 0;
while (count < n)
{
bool found = false;
for (int i = 0; i < n; i++)
if (!finish[i])
{
int j;
for (j = 0; j < m; j++)
if (max[i][j] - allocated[i][j] > available[j])
break;
if (j == m)
{
for (j = 0; j < m; j++)
available[j] += allocated[i][j];
finish[i] = true;
safeSeq[count++] = i;
found = true;
}
}
if (!found)
return false;
}
return true;
}
int main()
{
int n, m;
cout << "Enter the number of processes: ";
cin >> n;
cout << "Enter the number of resources: ";
cin >> m;
int max[10][10];
int allocated[10][10];
int available[10];
int safeSeq[10];
cout << "Enter the maximum resource requirements for each process: " << endl;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> max[i][j];
cout << "Enter the allocated resource for each process: " << endl;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> allocated[i][j];
cout << "Enter the available resources: ";
for (int i = 0; i < m; i++)
cin >> available[i];
if (findSafeSequence(max, allocated, available, m, n, safeSeq))
{
cout << "Safe sequence: ";
for (int i = 0; i < n; i++)
cout << "P" << safeSeq[i] << " ";
cout << endl;
}
else
cout << "No safe sequence exists." << endl;
return 0;
}