-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbanker.cpp
More file actions
90 lines (76 loc) · 1.79 KB
/
banker.cpp
File metadata and controls
90 lines (76 loc) · 1.79 KB
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
84
85
86
87
88
89
90
#include <iostream>
using namespace std;
int main()
{
int n, m, i, j, k;
int alloc[100][100];
int max[100][100], avail[100];
cout << "Enter number of processes: ";
cin >> n;
cout << "Enter number of resource types: ";
cin >> m;
cout << "Enter \"allocation\" matrix: " << endl;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> alloc[i][j];
}
}
cout << "Enter \"maximum\" demand matrix: " << endl;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> max[i][j];
}
}
cout << "Enter \"available\" matrix: " << endl;
for (int i = 0; i < m; ++i)
{
cin >> avail[i];
}
int f[100], ans[100], ind = 0;
for (int k = 0; k < n; k++)
{
f[k] = 0;
}
int need[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
if (f[i] == 0)
{
int flag = 0;
for (int j = 0; j < m; j++)
{
if (need[i][j] > avail[j])
{
flag = 1;
break;
}
}
if (flag == 0)
{
ans[ind++] = i;
for (int j = 0; j < m; j++)
avail[j] += alloc[i][j];
f[i] = 1;
}
}
}
}
cout << "Safe sequence is as follows : <P" << ans[0];
for (int i = 1; i < n; i++)
{
cout << ", P" << ans[i];
}
cout << ">" << endl;
return 0;
}