-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
247 lines (184 loc) · 6.13 KB
/
main.cpp
File metadata and controls
247 lines (184 loc) · 6.13 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <vector>
#define endl '\n'
using namespace std;
void read_m_vector(vector<int> &vector, string name, int m){
cout << "Enter the " << name <<" Vector"<<endl;
char out = 'A';
cout << "";
for(int i = 0 ; i< m ;i++){
cout << out << " ";
out = out +1;
}
cout << endl;
for(int i = 0 ; i< m ;i++)
cin >> vector[i];
}
void read_nm_matrix(vector<vector<int>> &twoDvector, string name, int n, int m){
cout << "Enter the " << name <<" Matrix"<<endl;
char out = 'A';
cout << " ";
for(int i = 0 ; i< m ;i++){
cout << out << " ";
out = out +1;
}
cout << endl;
for(int i = 0 ; i< n ;i++){
cout << "P" << i << ":";
for(int j = 0 ; j < m ; j++){
cin >> twoDvector[i][j];
}
cout << endl;
}
}
void readReq(vector<int> &pro,int &p,int &m){
cout << endl;
cout << "Enter the number of the process to inquiry about immediate requests" << endl;
cin >> p;
cout << "Enter the request vector" << endl;
for(int i = 0 ; i < m ; i++)
cin >> pro[i];
}
void calc_need(vector<vector<int>> &Need, vector<vector<int>> &Max, vector<vector<int>> &Allocation, int n, int m){
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
Need[i][j] = Max[i][j] - Allocation[i][j];
char out = 'A';
cout << "The Need Matrix:"<< endl << endl;
cout << " ";
for(int i = 0 ; i< m ;i++){
cout << out << " ";
out = out +1;
}
cout << endl;
for(int i = 0 ; i< n ;i++){
cout << "P" << i << ":";
for(int j = 0 ; j < m ; j++){
cout << Need[i][j] << " ";
}
cout << " " << endl;
}
cout << endl;
}
bool safeState(vector<int> &sequence, vector<vector<int>> &Need, vector<int> &Available, vector<vector<int>> &Allocation,int n, int m){
// Safety Algorithm
//declare the finish vector and Initialize it with false for all elements
vector<bool>finish (n);
for (int k = 0; k < n; k++)
finish[k] = false;
// sequence vector to hold the safe sequence
int index = 0;
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
if (finish[i] == 0) {
int flag = 0;
for (int j = 0; j < m; j++) {
if (Need[i][j] > Available[j]){
flag = 1;
break;
}
}
if (flag == 0) {
sequence[index++] = i;
for (int y = 0; y < m; y++)
Available[y] += Allocation[i][y];
finish[i] = true;
}
}
}
}
//if any element of the finish vector is false then the system is in a unsafe state
for (int k = 0; k < n; k++){
if (!finish[k]){
return false;
}
}
//if all the elements of the finish vector is true then the system is in a safe state
return true;
}
bool validReq(vector<int> &pro, vector<vector<int>> &Need, vector<int> &Available, vector<vector<int>> &Allocation,int m,int p){
//Resource-Request Algorithm
for (int i = 0; i < m; i++){
if(pro[i] <= Need[p][i]){
if(pro[i] <= Available[i]){
Available[i] -= pro[i];
Allocation[p][i] += pro[i];
Need[p][i] -= pro[i];
}
else
return false;
}
else
return false;
}
return true;
}
int main() {
// Process Format P0, P1, P2, P3....
// Resources Types Format A, B, C, ....
// n > number of process, m > number or Resources types
int n, m;
char out = 'A';
cout << "Enter the Number of Process"<<endl;
cin >> n;
cout << "Enter the Number of Resources"<<endl;
cin>> m;
vector<vector<int>> Allocation (n, vector<int> (m));
vector<vector<int>> Max (n, vector<int> (m));
vector<vector<int>> Need (n, vector<int> (m));
vector<int> Available (m);
//taking the input matrices from the user
read_nm_matrix(Allocation,"Allocation",n , m);
read_nm_matrix(Max, "Max", n, m);
read_m_vector(Available, "Available", m);
cout << endl;
// calculate the need matrix & output it
calc_need(Need, Max, Allocation, n, m);
//handling the inquiry of the user
cout << "Enter 0 to know if the system is in a safe state or not " << endl <<
"Enter 1 to determine a certain immediate request by one of the processes can be granted or not" <<endl;
int input;
cin >> input;
if (input == 0){
vector<int>sequence (n);
// checking if the system is in a safe state or not
bool isSafeState = safeState(sequence,Need, Available, Allocation,n,m);
if(isSafeState){
cout << "Yes , Safe state <";
for (int i = 0; i < n - 1; i++)
cout << "P" << sequence[i] << ",";
cout << "P" << sequence[n - 1] << ">" << endl << endl;
} else{
cout << "No, unsafe state" << endl;
return 0;
}
} else if(input == 1){
int p;
vector<int> pro (m);
readReq(pro,p,m);
bool isValid = validReq(pro,Need,Available,Allocation,m,p);
if(isValid){
vector<int>sequence (n);
// checking if the system is in a safe state or not
bool isSafeState = safeState(sequence,Need, Available, Allocation,n,m);
if(isSafeState){
cout << "Yes request can be granted with safe state , Safe state <P" << p << "req,";
for (int i = 0; i < n - 1; i++)
cout << "P" << sequence[i] << ",";
cout << "P" << sequence[n - 1] << ">" << endl << endl;
} else{
cout << "No request can't be granted" << endl;
return 0;
}
} else{
cout << "No the request can't be granted" << endl;
return 0;
}
}
else{
cout << "Wrong input!"<< endl << "Please Try again";
return 0;
}
system("pause");
return 0;
}