-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryAllocation.java
More file actions
487 lines (352 loc) · 14.5 KB
/
memoryAllocation.java
File metadata and controls
487 lines (352 loc) · 14.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import java.io.*;
import java.util.*;
public class memoryAllocation{
ArrayList<String> sfmp1 = new ArrayList<>(); // size of free memory.txt partitions
ArrayList<String> process1 = new ArrayList<>(); // size of processes
// output.txt
FileWriter fw;
{
try {
fw = new FileWriter("src/output.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
PrintWriter pw = new PrintWriter(fw);
//get inputs from memory.txt
void getinputs() {
process1.clear(); // restart for each time
sfmp1.clear();
try {
File myObj = new File("src/memory.txt");
Scanner myReader = new Scanner(myObj);
int a = 0;
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] dt = data.split(",");
if (a == 0) {
for (int i = 0; i < dt.length; i++) {
if (dt[i] != "") {
sfmp1.add(dt[i]); // get free inputs
// System.out.println(sfmp1.get(i));
}
}
a++;
} else {
for (int j = 0; j < dt.length; j++) {
if (dt[j] != "") {
process1.add(dt[j]); // get process inputs
// System.out.println(process1.get(j));
}
}
}
}
myReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void firstFit() throws IOException {
ArrayList<String> sfmp = new ArrayList<>(); // size of free memory.txt.txt partitions
ArrayList<String> process = new ArrayList<>(); // size of processes
sfmp.addAll(sfmp1);
process.addAll(process1);
// key : process , value : Arraylist
Map<String, ArrayList<String>> ffmap = new LinkedHashMap<>();
ArrayList<String> dfmp = new ArrayList<>();
dfmp.addAll(sfmp);
ffmap.put("start", dfmp);
for (int i = 0; i < process.size(); i++) { // process loop
for (int j = 0; j < sfmp.size(); j++) { // free loop
String str = sfmp.get(j);
char c = str.charAt(str.length() - 1);
// if process less than
if (!"*".equals(String.valueOf(c)) && Integer.parseInt(process.get(i)) < Integer.parseInt(sfmp.get(j))) {
int d = Integer.parseInt(sfmp.get(j)) - Integer.parseInt(process.get(i));
sfmp.remove(sfmp.get(j));
sfmp.add(j, process.get(i) + "*");
sfmp.add(j + 1, String.valueOf(d));
ArrayList<String> fmp1 = new ArrayList<>();
fmp1.addAll(sfmp);
ffmap.put(process.get(i), fmp1);
break;
// if process number equals free memory part number
} else if (!"*".equals(String.valueOf(c)) && Integer.parseInt(process.get(i)) == Integer.parseInt(sfmp.get(j))) {
sfmp.remove(sfmp.get(j));
sfmp.add(j, process.get(i) + "*");
ArrayList<String> fmp2 = new ArrayList<>();
fmp2.addAll(sfmp);
ffmap.put(process.get(i), fmp2);
break;
// if process number greater than all free memoryt part numbers
} else {
for (int m = 0; m < sfmp.size(); m++) { // if there is no element less or equal, it won't allocated
if (!"*".equals(String.valueOf(c))) {
if (Integer.parseInt(process.get(i)) <= Integer.parseInt(sfmp.get(j))) {
continue;
}
}
ArrayList<String> fmp3 = new ArrayList<>();
fmp3.add("not allocated, must wait");
ffmap.put(process.get(i), fmp3);
break;
}
}
}
}
/* int c=0;
ArrayList<String> fmp3 = new ArrayList<>();
fmp3.add("not allocated, must wait");
for(HashMap.Entry<String, ArrayList<String>> entry : ffmap.entrySet()) {
if (!entry.getValue().equals(fmp3)) {
++c; // find place of not allocated number
}else{
break;
}
}
for (int i = c; i < process.size(); i++) {
ffmap.remove(process.get(i));
ffmap.put(process.get(i), fmp3);
}
*/
// print part
String str = "";
myloop:
for (HashMap.Entry<String, ArrayList<String>> entry : ffmap.entrySet()) {
str += entry.getKey() + " => ";
for (int j = 0; j < entry.getValue().size(); j++) {
str += " " + entry.getValue().get(j);
}
str += '\n';
}
System.out.print(str);
System.out.println();
// output.txt part
// FileWriter fw = new FileWriter("src/output.txt");
// PrintWriter pw = null;
try {
// pw = new PrintWriter(fw);
pw.println("************* FIRST - FIT *************** ");
myloop:
for (HashMap.Entry<String, ArrayList<String>> entry : ffmap.entrySet()) {
pw.print(entry.getKey() + " => ");
for (int j = 0; j < entry.getValue().size(); j++) {
pw.print(" " + entry.getValue().get(j));
// if (entry.getValue().get(j).equals("not allocated, must wait")) {
// break;
// }
}
pw.println();
}
pw.println();
pw.flush();
} finally {
try {
// always close the writer
// pw.close();
} catch (Exception e) {
}
}
}
void bestFit() throws IOException {
ArrayList<String> sfmp = new ArrayList<>(); // size of free memory.txt partitions list
ArrayList<String> process = new ArrayList<>(); // size of processes list
sfmp.addAll(sfmp1);
process.addAll(process1);
Map<String, ArrayList<String>> ffmap2 = new LinkedHashMap<>();
ArrayList<String> dfmp = new ArrayList<>();
dfmp.addAll(sfmp);
ffmap2.put("start", dfmp); // first line
for (int i = 0; i < process.size(); i++) { // process
int minFree = smallestfree(process.get(i), sfmp); // call smallestfree for getting smallest greater number
if (minFree != -1 && Integer.parseInt(process.get(i)) != minFree) { // if process and free number not equals
int diff = minFree - Integer.parseInt(process.get(i));
int index = sfmp.indexOf(String.valueOf(minFree));
sfmp.remove(sfmp.get(index));
sfmp.add(index, process.get(i) + "*");
sfmp.add(index + 1, String.valueOf(diff));
ArrayList<String> free1 = new ArrayList<>();
free1.addAll(sfmp);
ffmap2.put(process.get(i), free1);
// if process and free number equals
} else if (minFree != -1 && Integer.parseInt(process.get(i)) == minFree) {
int index = sfmp.indexOf(String.valueOf(minFree));
sfmp.remove(sfmp.get(index));
sfmp.add(index, String.valueOf(process.get(i)) + "*");
ArrayList<String> free2 = new ArrayList<>();
free2.addAll(sfmp);
ffmap2.put(process.get(i), free2);
} // not allocated
if (minFree == -1) {
int index = sfmp.indexOf(String.valueOf(minFree));
ArrayList<String> free3 = new ArrayList<>();
free3.add("not allocated, must wait");
ffmap2.put(process.get(i), free3);
// for (int a = i; a < process.size(); a++) {
// ffmap2.put(process.get(a), free3);
// }
// break;
}
}
// print part
String str2 = "";
myloop:
for (HashMap.Entry<String, ArrayList<String>> entry : ffmap2.entrySet()) {
str2 += entry.getKey() + " => ";
for (int j = 0; j < entry.getValue().size(); j++) {
str2 += " " + entry.getValue().get(j);
}
str2 += '\n';
}
System.out.print(str2);
System.out.println();
//output.txt
// FileWriter fw = new FileWriter("src/output.txt");
// PrintWriter pw = null;
try {
// pw = new PrintWriter(fw);
pw.println("************* BEST - FIT *************** ");
myloop:
for (HashMap.Entry<String, ArrayList<String>> entry : ffmap2.entrySet()) {
pw.print(entry.getKey() + " => ");
for (int j = 0; j < entry.getValue().size(); j++) {
pw.print(" " + entry.getValue().get(j));
}
pw.println();
}
pw.println();
pw.flush();
} finally {
try {
// always close the writer
// pw.close();
} catch (Exception e) {
}
}
}
// to find smallest hole that is the big enough
public static int smallestfree(String process, ArrayList<String> array) {
int min = Integer.MAX_VALUE; // will be smallest greater number
int pro = Integer.parseInt(process); // process number
int minS = Integer.MAX_VALUE;
for (int i = 0; i < array.size(); i++) {
if (isInteger(array.get(i))) {
int freeNum = Integer.parseInt(array.get(i));
int diff = freeNum - pro;
if (freeNum == pro) {
min = freeNum;
break;
} else if (diff < minS && diff >= 0) {
minS = diff;
min = freeNum;
}
}
}
if (min == Integer.MAX_VALUE) {
return -1;
} else {
return min;
}
}
private static boolean isInteger(String s) { // check string is numeric or not
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
void worstFit() throws IOException {
ArrayList<String> sfmp = new ArrayList<>(); // size of free memory.txt t partitions
ArrayList<String> process = new ArrayList<>(); // size of processes
sfmp.addAll(sfmp1);
process.addAll(process1);
Map<String, ArrayList<String>> ffmap3 = new LinkedHashMap<>();
ArrayList<String> dfmp = new ArrayList<>();
dfmp.addAll(sfmp);
ffmap3.put("start", dfmp);
for (int i = 0; i < process.size(); i++) { // process
int maxFree = largestfree(process.get(i), sfmp); // call to find largest one
// if process and free number not equals
if (maxFree != -1 && Integer.parseInt(process.get(i)) != maxFree) {
int diff = maxFree - Integer.parseInt(process.get(i));
int index = sfmp.indexOf(String.valueOf(maxFree));
sfmp.remove(sfmp.get(index));
sfmp.add(index, process.get(i) + "*");
sfmp.add(index + 1, String.valueOf(diff));
ArrayList<String> free1 = new ArrayList<>();
free1.addAll(sfmp);
ffmap3.put(process.get(i), free1);
// if they are equals
} else if (maxFree != -1 && Integer.parseInt(process.get(i)) == maxFree) {
int index = sfmp.indexOf(String.valueOf(maxFree));
sfmp.remove(sfmp.get(index));
sfmp.add(index, String.valueOf(process.get(i)) + "*");
ArrayList<String> free2 = new ArrayList<>();
free2.addAll(sfmp);
ffmap3.put(process.get(i), free2);
}// if therre is no greater than process
if (maxFree == -1) {
int index = sfmp.indexOf(String.valueOf(maxFree));
ArrayList<String> free3 = new ArrayList<>();
free3.add("not allocated, must wait");
ffmap3.put(process.get(i), free3);
// for (int a = i; a < process.size(); a++) {
// ffmap3.put(process.get(a), free3);
// }
// break;
}
}
// print part
String str2 = "";
for (HashMap.Entry<String, ArrayList<String>> entry : ffmap3.entrySet()) {
str2 += entry.getKey() + " => ";
for (int j = 0; j < entry.getValue().size(); j++) {
str2 += " " + entry.getValue().get(j);
}
str2 += '\n';
}
System.out.print(str2);
System.out.println();
// output.txt part
// FileWriter fw = new FileWriter("src/output.txt");
// PrintWriter pw = null;
try {
// pw = new PrintWriter(fw);
pw.println("************* WORST - FIT *************** ");
myloop:
for (HashMap.Entry<String, ArrayList<String>> entry : ffmap3.entrySet()) {
pw.print(entry.getKey() + " => ");
for (int j = 0; j < entry.getValue().size(); j++) {
pw.print(" " + entry.getValue().get(j));
}
pw.println();
}
pw.flush();
} finally {
try {
// always close the writer
pw.close();
} catch (Exception e) {
}
}
}
public static int largestfree(String process, ArrayList<String> array) {
int max = Integer.MIN_VALUE;
int pro = Integer.parseInt(process);
for (int i = 0; i < array.size(); i++) {
if (isInteger(array.get(i))) { // check if numeric or not
int freeNum = Integer.parseInt(array.get(i));
int diff = freeNum - pro;
if (freeNum > max && diff>=0) { // find max number
max = freeNum;
}
}
}
if (max == Integer.MIN_VALUE) {
return -1;
} else {
return max;
}
}
}