forked from phoenix-aditya/processscheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanker's algorithm STL.cpp
More file actions
101 lines (100 loc) · 2.53 KB
/
banker's algorithm STL.cpp
File metadata and controls
101 lines (100 loc) · 2.53 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
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
using namespace std;
#include <string>
#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define vc vector<char>
#define vvc vector<vc>
#define pb push_back
#define pf push_front
#define test \
ll t; \
cin >> t; \
while (t--)
#define mod 1000000007
#define fo ios_base::sync_with_stdio(false);
#define fi cin.tie(NULL);
int main()
{
fo;
fi;
int numberOfProcesses, numberOfResources, i, j, k;
vvi allocated(5, vi(3));
vvi max(5, vi(3));
vvi need(5, vi(3));
vvi available(1, vi(3));
// input for allocated matrix
allocated[0] = {0, 1, 0};
allocated[1] = {2, 0, 0};
allocated[2] = {3, 0, 2};
allocated[3] = {2, 1, 1};
allocated[4] = {0, 0, 2};
// input for max matrix
max[0] = {7, 5, 3};
max[1] = {3, 2, 2};
max[2] = {9, 0, 2};
max[3] = {2, 2, 2};
max[4] = {4, 3, 3};
// input for available matrix
available[0] = {3, 3, 2};
// input for need matrix
for (i = 0; i < 5; i++)
{
for (j = 0; j < 3; j++)
{
need[i][j] = max[i][j] - allocated[i][j];
}
}
int f[5] = {0};
int ans[5];
int ind = 0;
int y = 0;
for (k = 0; k < numberOfProcesses; k++)
{
for (i = 0; i < numberOfProcesses; i++)
{
if (f[i] == 0)
{
int flag = 0;
for (int j = 0; j < numberOfResources; j++)
{
// if need matrix is greater than available matrix
if (need[i][j] > available[0][j])
{
flag = 1;
break;
}
}
if (flag == 0)
{
ans[ind++] = i;
for (y = 0; y < numberOfResources; y++)
available[0][y] += allocated[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
// to check if sequence is safe or not
for (int i = 0; i < numberOfProcesses; i++)
{
if (f[i] == 0)
{
flag = 0;
cout << "NOT SAFE!";
break;
}
}
if (flag == 1)
{
cout << "SAFE!";
for (int i = 0; i < numberOfProcesses; i++)
{
cout << " P" << ans[i] << " ->";
cout << " P" << ans[numberOfProcesses - 1] << endl;
}
}
return 0;
}