-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
262 lines (222 loc) · 8.02 KB
/
Copy pathSource.cpp
File metadata and controls
262 lines (222 loc) · 8.02 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
//// Function to find the system is in safe state or not
bool isSafe(int n,int m, vector<int> &avail, vector< vector<int> > &Max, vector< vector<int> > &Alloc,
vector< vector<int> > &Need, vector<int> &safeSeq)
{
int i, j, k;
bool* finish = new bool[n];
int* work = new int[m];
for (i = 0; i < n; i++) {
finish[i] = false;
}
for (i = 0; i < m; i++) {
work[i] = avail[i];
}
int count = 0;
while (count < n)
{
bool found = false;
for (i = 0; i < n; i++)
{
if (finish[i] == 0)
{
for (j = 0; j < m; j++)
if (Need[i][j] > work[j])
break;
if (j == m)
{
for (k = 0; k < m; k++)
work[k] += Alloc[i][k];
safeSeq[count++] = i;
finish[i] = 1;
found = true;
}
}
}
if (found == false)
{
return false;
}
}
delete[] finish;
delete[] work;
return true;
}
bool Res_req(int n, int m, int pid, vector<int> &avail, vector< vector<int> > &Max, vector< vector<int> > &Alloc,
vector< vector<int> > &Need, vector<int> &Req, vector<int> &safeSeq) {
int i, j, k;
bool grant;
for (i = 0; i < m; i++) {
if (Req[i] > Need[pid][i]) {
cout << "Error occured , The Request Cannot be granted" << endl;
return false;
}
else {
if (Req[i] > avail[i]) {
cout << "The Request Cannot be granted , the process 'P"<<pid<<" will wait the resources" << endl;
return false;
}
}
}
for (i = 0; i < m; i++) {
if (Req[i] <= avail[i]) {
avail[i] = avail[i] - Req[i];
Alloc[pid][i] = Alloc[pid][i] + Req[i];
Need[pid][i] = Need[pid][i] - Req[i];
}
}
grant = isSafe(n, m, avail, Max, Alloc, Need, safeSeq);
if (grant == true) {
cout << "\n Yes , Request can be granted with safe state ,Safe state <";
cout << "P" << pid << "req"<<",";
for (i = 0; i < n; i++) {
cout << "P" << safeSeq[i];
if (i != (n - 1)) {
cout << ",";
}
}
cout << ">" << endl;
for (i = 0; i < m; i++) {
avail[i] = avail[i] + Req[i];
Alloc[pid][i] = Alloc[pid][i] - Req[i];
Need[pid][i] = Need[pid][i] + Req[i];
}
return true;
}
else if (grant == false) {
cout << "No" << endl;
cout << "The Request Cannot be granted , the system with unsafe state " << endl;
for (i = 0; i < m; i++) {
avail[i] = avail[i] + Req[i];
Alloc[pid][i] = Alloc[pid][i] - Req[i];
Need[pid][i] = Need[pid][i] + Req[i];
}
return false;
}
}
int main() {
int n = 0, m = 0; // n = no.of process , m = no.of resources
int i, j, k;
int input = 0;
char c1,c2, again;
do{
input = 0, n = 0, m = 0;
cout << "Please enter number of Process: " << endl;
cin >> n;
cout << "Please enter number of Resources: " << endl;
cin >> m;
vector<vector<int>> Alloc(n, vector<int>(m, 0));
vector<vector<int>> Max(n, vector<int>(m, 0));
vector<vector<int>> Need(n, vector<int>(m, 0));
vector<int> Avail(m, 0);
vector<int> safeSeq(n, 0);// To store safe sequence
for (i = 0; i < n; i++) {
safeSeq[i] = 0;
}
cout << "Please enter Allocation table in this form:"
" 1 2 3 4 5 (Resources numbers should be seperated by spaces) and (press enter after each row at each new process)"
"(like Matrices but without brackets):" << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> input;
Alloc[i][j] = input;
}
}
cout << "Please enter Max table in this form:"
" 1 2 3 4 5 (Resources numbers should be seperated by spaces) and (press enter after each row at each new process)"
"(like Matrices but without brackets):" << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> input;
Max[i][j] = input;
}
}
cout << "Please enter Available row in this form 1 2 3 4 5 (Resources numbers should be seperated by spaces): " << endl;
for (i = 0; i < m; i++) {
cin >> input;
Avail[i] = input;
}
// calculating Need Matrix
cout << "\n Need Matrix: \n" << endl;
cout << " ";
for (i = 0; i < m; i++)
{
cout << "R" << i << " ";
}
cout << endl;
for (i = 0; i < n; i++) {
cout << 'P' << i << " ";
for (j = 0; j < m; j++) {
Need[i][j] = (Max[i][j]) - (Alloc[i][j]);
cout << Need[i][j] << " ";
}
cout << "\n";
}
bool flag1 =false,flag2=false;
int pid;
vector<int> Req(m, 0);
do {
cout << "\nDo you want to know the safe state ? (Y/N) \n";
cin >> c1;
} while (c1 != 'Y' && c1 != 'y' && c1 != 'N' && c1 != 'n');
if (c1 == 'Y' || c1 == 'y') {
flag1 = isSafe(n, m, Avail, Max, Alloc, Need, safeSeq);
if (flag1 == false) {
cout << "No" << endl;
}
else if (flag1 == true) {
cout << "\n Yes , Safe state <";
for (i = 0; i < n; i++) {
cout << "P" << safeSeq[i];
if (i != (n - 1)) {
cout << ",";
}
}
cout << ">" << endl;
}
}
if (flag1 == false && (c1 == 'Y' || c1 == 'y')) {
cout << "there is no immediate requests because the system with unsafe state " << endl;
cout << " \nEnter 0: to restert the program , Q: to end the program " << endl;
cin >> again;
if (again == 'Q' || again == 'q') {
cout << "\nThank You" << endl;
exit(0);
}
}
do {
cout << "\nDo you want immediate requests ? (Y/N) \n";
cin >> c2;
} while (c2 != 'Y' && c2 != 'y' && c2 != 'N' && c2 != 'n');
if (c2 == 'Y' || c2 == 'y') {
do {
do {
cout << "Please enter the pid of the process that demand the request from (0 to n-1) ,where n is no. of process " << endl;
cin >> pid;
} while ((pid >= n) || (pid < 0));
cout << "Please enter Requested row in this form 1 2 3 4 5 (Resources numbers should be seperated by spaces):" << endl;
for (i = 0; i < m; i++) {
cin >> input;
Req[i] = input;
}
flag2 = Res_req(n, m, pid, Avail, Max, Alloc, Need, Req, safeSeq);
cout << " \nEnter 1 : to another request , 0: to restert the program , Q: to end the program " << endl;
cin >> again;
} while (again == '1');
}
else if (c2 == 'N' || c2 == 'n') {
cout << "Thank You" << endl;
cout << " \nEnter 0: to restert the program , Q: to end the program "<<endl;
cin >> again;
}
if (again == 'Q' || again == 'q') {
cout << "\nThank You" << endl;
exit(0);
}
}while (again == '0');
return 0;
}