-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOS2.cpp
More file actions
272 lines (236 loc) · 5.31 KB
/
Copy pathOS2.cpp
File metadata and controls
272 lines (236 loc) · 5.31 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
263
264
265
266
267
268
269
270
271
272
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Result
{
public:
int block;
int process;
Result(int block, int process)
{
this->block = block;
this->process = process;
}
};
void erase(int x, vector<int> &p)
{
vector<int> temp;
for (int i = 0; i < p.size(); i++)
{
if (i != x)
{ //we will ignore the process that was placed in the freeblock vector
temp.push_back(p[i]);
}
}
p.clear();
p = temp;
}
void firstFit(int numOfBlocks, vector<int> &block, int numOfProcesses, vector<int> &process)
{
vector<int> freeblock;
for (int y = 0; y < numOfBlocks; y++)
{
freeblock.push_back(0);
}
for (int i = 0; i < numOfBlocks; i++)
{
int temp = -1;
for (int x = 0; x < process.size(); x++)
{
if (block[i] >= process[x])
{
freeblock[i] = process[x];
temp = x;
break;
}
}
if (temp > -1)
{
erase(temp, process);
temp = -1;
}
}
cout << "First fit: " << endl;
cout << "Blocks "
<< " "
<< "Processes in blocks" << endl;
for (int j = 0; j < freeblock.size(); j++)
{
cout << " " << block[j] << " | ";
cout << freeblock[j];
cout << endl;
}
}
void bestFit(int numOfBlocks, vector<int> &block, int numOfProcesses, vector<int> &process)
{
vector<Result> result;
for (int i = 0; i < numOfBlocks; i++)
{
result.push_back(Result(block[i], 0));
}
int indexOfCurrentBest = -1;
int subValueOfCurrBest = -1;
for (int i = 0; i < numOfProcesses; i++)
{
for (int x = 0; x < result.size(); x++)
{
if (result[x].block >= process[i] && result[x].process == 0)
{
if (result[x].block == process[i])
{
subValueOfCurrBest = 0;
indexOfCurrentBest = x;
break;
}
else
{
int subVal = result[x].block - process[i];
if (subValueOfCurrBest == -1 || subVal < subValueOfCurrBest)
{
subValueOfCurrBest = subVal;
indexOfCurrentBest = x;
}
}
}
}
if (indexOfCurrentBest >= 0)
{
result[indexOfCurrentBest].process = process[i]; //inserting the process in the block
indexOfCurrentBest = -1;
subValueOfCurrBest = -1;
}
}
int counter = 0;
cout << "Best fit: " << endl;
cout << "Blocks"
<< " "
<< "Processes in blocks" << endl;
for (int j = 0; j < result.size(); j++)
{
if (result[j].process == 0)
{
counter++;
}
cout << " " << result[j].block << " | ";
cout << result[j].process;
cout << endl;
}
int count = process.size() - result.size();
if (count > 0)
{
cout << "Number of processes is bigger than available blocks therefore there is " << count + counter << " that weren't allocated in the memory" << endl;
}
if (count >= 0)
{
cout << "There was " << counter << " block of memory that remained empty" << endl;
}
}
void worstFit(int numOfBlocks, vector<int> &block, int numOfProcesses, vector<int> &process)
{
vector<Result> result;
for (int i = 0; i < numOfBlocks; i++)
{
result.push_back(Result(block[i], 0));
}
int indexOfCurrentWorst = -1;
int subValueOfCurrWorst = -1;
for (int i = 0; i < numOfProcesses; i++)
{
for (int x = 0; x < result.size(); x++)
{
if (result[x].block >= process[i] && result[x].process == 0)
{
int subVal = result[x].block - process[i];
if (subValueOfCurrWorst == -1 || subVal > subValueOfCurrWorst)
{
subValueOfCurrWorst = subVal;
indexOfCurrentWorst = x;
}
}
}
if (indexOfCurrentWorst >= 0)
{
result[indexOfCurrentWorst].process = process[i];
indexOfCurrentWorst = -1;
subValueOfCurrWorst = -1;
}
}
int counter = 0;
cout << "Worst fit: " << endl;
cout << "Blocks "
<< " "
<< "Processes in blocks" << endl;
for (int j = 0; j < result.size(); j++)
{
if (result[j].process == 0)
{
counter++;
}
cout << " " << result[j].block << " | ";
cout << result[j].process;
cout << endl;
}
int count = process.size() - result.size();
if (count > 0)
{
cout << "Number of processes is bigger than available blocks therefore there is " << count + counter << " that weren't allocated in the memory" << endl;
}
if (count >= 0)
{
cout << "There was " << counter << " block of memory that remained empty" << endl;
}
}
void choose(int num, int numOfBlocks, vector<int> &block, int numOfProcesses, vector<int> &process)
{
if (num == 1)
{
firstFit(numOfBlocks, block, numOfProcesses, process);
}
else if (num == 2)
{
bestFit(numOfBlocks, block, numOfProcesses, process);
}
else if (num == 3)
{
worstFit(numOfBlocks, block, numOfProcesses, process);
}
}
int main()
{
vector<int> block;
vector<int> process;
int blockSize;
int numberOfBlocks;
int numberOfProcesses;
int processSize;
int allocationMethod;
cout << "Enter number of blocks: ";
cin >> numberOfBlocks;
cout << endl;
for (int i = 0; i < numberOfBlocks; i++)
{
cout << "Enter block size: ";
cin >> blockSize;
block.push_back(blockSize);
cout << endl;
}
cout << endl;
cout << "Enter number of processes: ";
cin >> numberOfProcesses;
cout << endl;
for (int i = 0; i < numberOfProcesses; i++)
{
cout << "Enter process size: ";
cin >> processSize;
process.push_back(processSize);
cout << endl;
}
cout << endl;
cout << "Enter 1 for firstfit" << endl;
cout << "Enter 2 for bestfit" << endl;
cout << "Enter 3 for worstfit" << endl;
cin >> allocationMethod;
cout << endl;
choose(allocationMethod, numberOfBlocks, block, numberOfProcesses, process);
}