-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
436 lines (421 loc) · 16.8 KB
/
Main.java
File metadata and controls
436 lines (421 loc) · 16.8 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
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.Queue;
import java.util.LinkedList;
public class Main {
// Static variables to track the next process ID and the waiting queue for processes
public static Short nextId = 1;
public static Queue<E_process> waitingQueue = new LinkedList<>();
/**
* Adds pre-running processes to RAM.
* @param pre_run_process Number of pre-running processes.
* @param p Array of pre-running processes.
* @param array_ram RAM array.
* @param sc Scanner object for input.
*/
public static void add_process(int pre_run_process, Process[] p, int[] array_ram, Scanner sc) {
for (Short i = 0; i < pre_run_process; i++) {
System.out.println("Enter details of process " + (i + 1) + " (size, start_address):");
Short size = checkNegativeInput(sc);
Short startaddress = checkNegativeInput(sc);
Short id = nextId++;
p[i] = new Process(id, size, startaddress);
for (Short j = startaddress; j < startaddress + size; j++) {
if (array_ram[j] != 0) {
System.out.println("This space is already occupied by another process...!");
break;
} else {
array_ram[j] = id;
}
}
}
}
/**
* Prints the current state of RAM.
* @param array_ram RAM array.
*/
public static void print_ram(int[] array_ram) {
System.out.println("--------------------RAM--------------------");
for (Short i = 0; i < array_ram.length; i++) {
System.out.print("[" + array_ram[i] + "]");
}
}
/**
* Allocates memory for processes using the First Fit algorithm.
* @param sc Scanner object for input.
* @param array_ram RAM array.
* @param ep Array of processes to be added.
*/
public static void First_Fit(Scanner sc, int[] array_ram, E_process[] ep) {
System.out.println("Enter number of processes you want to add:");
Short a_p = checkNegativeInput(sc);
for (Short i = 0; i < a_p; i++) {
System.out.print("Enter size of process " + (i + 1) + ":");
Short size = checkNegativeInput(sc);
Short id = nextId++;
ep[i] = new E_process(size, id);
}
for (Short i = 0; i < a_p; i++) {
Short count = 0;
Short startAddress = 0;
boolean allocated = false;
for (Short j = 0; j < array_ram.length; j++) {
if (array_ram[j] == 0) {
if (count == 0) {
startAddress = j;
}
count++;
if (count == ep[i].getsize()) {
for (int k = startAddress; k < startAddress + ep[i].getsize(); k++) {
array_ram[k] = ep[i].getid();
}
allocated = true;
break;
}
} else {
count = 0;
}
}
if (!allocated) {
waitingQueue.add(ep[i]);
System.out.println("Memory allocation failed for process " + ep[i].getid() + ". No suitable block available");
System.out.println("Process " + ep[i].getid() + " added to the waiting queue");
}
}
print_ram(array_ram);
}
/**
* Allocates memory for processes using the Best Fit algorithm.
* @param sc Scanner object for input.
* @param id Process ID.
* @param array_ram RAM array.
* @param ep Array of processes to be added.
*/
public static void Best_Fit(Scanner sc, Short id, int[] array_ram, E_process[] ep) {
System.out.print("Enter number of processes you want to add:");
Short a_p = checkNegativeInput(sc);
for (Short i = 0; i < a_p; i++) {
System.out.print("Enter size of process " + (i + 1) + ":");
Short size = checkNegativeInput(sc);
id = nextId++;
ep[i] = new E_process(size, id);
}
Map<Integer, Integer> available_blocks = new HashMap<>();
for (Short i = 0; i < array_ram.length; i++) {
if (array_ram[i] == 0) {
int blockSize = 0;
int start = i;
while (i < array_ram.length && array_ram[i] == 0) {
blockSize++;
i++;
}
if (blockSize > 0) {
available_blocks.put(start, blockSize);
}
}
}
for (Short i = 0; i < a_p; i++) {
int processSize = ep[i].getsize();
int[] allocationInfo = allocateBestFit(available_blocks, processSize);
int allocateStart = allocationInfo[0];
int blockSize = allocationInfo[1];
if (allocateStart != -1) {
for (int j = allocateStart; j < allocateStart + processSize; j++) {
array_ram[j] = ep[i].getid();
}
if (blockSize > processSize) {
available_blocks.put(allocateStart + processSize, blockSize - processSize);
}
available_blocks.remove(allocateStart);
} else {
waitingQueue.add(ep[i]);
System.out.println("Memory allocation failed for process " + ep[i].getid() + ". No suitable block is available");
System.out.println("Process " + ep[i].getid() + " added to the waiting queue");
}
}
print_ram(array_ram);
}
/**
* Helper function to find the best fit for a process in RAM.
* @param available_blocks Map of available blocks in RAM.
* @param processSize Size of the process to be allocated.
* @return Array containing start index and block size of the best fit block.
*/
private static int[] allocateBestFit(Map<Integer, Integer> available_blocks, int processSize) {
int[] allocationInfo = {-1, Integer.MAX_VALUE}; // {start index, block size}
for (Map.Entry<Integer, Integer> entry : available_blocks.entrySet()) {
int blockStart = entry.getKey();
int blockSize = entry.getValue();
if (blockSize >= processSize && blockSize < allocationInfo[1]) {
allocationInfo[0] = blockStart;
allocationInfo[1] = blockSize;
}
}
return allocationInfo;
}
/**
* Allocates memory for processes using the Worst Fit algorithm.
* @param sc Scanner object for input.
* @param id Process ID.
* @param array_ram RAM array.
* @param ep Array of processes to be added.
*/
public static void Worst_fit(Scanner sc, Short id, int[] array_ram, E_process[] ep) {
System.out.print("Enter number of processes you want to add:");
Short a_p = (short) checkNegativeInput(sc);
for (int i = 0; i < a_p; i++) {
System.out.print("Enter size of process " + (i + 1) + ":");
Short size = checkNegativeInput(sc);
id = nextId++;
ep[i] = new E_process(size, id);
}
for (int i = 0; i < a_p; i++) {
int processSize = ep[i].getsize();
int worstFitStart = allocateWorstFit(array_ram, processSize);
if (worstFitStart != -1) {
// Allocate memory for the process
for (int j = worstFitStart; j < worstFitStart + processSize; j++) {
array_ram[j] = ep[i].getid();
}
} else {
waitingQueue.add(ep[i]);
System.out.println("Memory allocation failed for process " + ep[i].getid() + ". No suitable block available");
System.out.println("Process " + ep[i].getid() + " added to the waiting queue");
}
}
print_ram(array_ram);
}
/**
* Helper function to find the worst fit for a process in RAM.
* @param array_ram RAM array.
* @param processSize Size of the process to be allocated.
* @return Start index of the worst fit block, or -1 if no suitable block is found.
*/
private static int allocateWorstFit(int[] array_ram, int processSize) {
int worstFitStart = -1;
int worstFitSize = -1;
int currentBlockSize = 0;
for (int i = 0; i < array_ram.length; i++) {
if (array_ram[i] == 0) {
if (currentBlockSize == 0) {
worstFitStart = i;
}
currentBlockSize++;
} else {
if (currentBlockSize > worstFitSize) {
worstFitSize = currentBlockSize;
worstFitStart = i - currentBlockSize;
}
currentBlockSize = 0;
}
}
if (currentBlockSize > worstFitSize) {
worstFitSize = currentBlockSize;
worstFitStart = array_ram.length - currentBlockSize;
}
if (worstFitSize >= processSize) {
return worstFitStart;
} else {
return -1;
}
}
/**
* Compacts memory to free up contiguous blocks and reallocate waiting processes.
* @param array_ram RAM array.
* @param waitingQueue Queue of waiting processes.
*/
public static void compact_memory(int[] array_ram, Queue<E_process> waitingQueue) {
Short id = 0;
// Compact the memory by moving processes to the start of the RAM array
for (Short i = 0; i < array_ram.length; i++) {
if (array_ram[i] != 0) {
array_ram[id++] = array_ram[i];
}
}
for (Short i = id; i < array_ram.length; i++) {
array_ram[i] = 0;
}
allocateFromQueue(array_ram, waitingQueue);
}
/**
* Releases memory occupied by a specified process and reallocates waiting processes.
* @param processId ID of the process to be released.
* @param array_ram RAM array.
* @param waitingQueue Queue of waiting processes.
*/
public static void release_memory(int processId, int[] array_ram, Queue<E_process> waitingQueue) {
boolean success = false;
// Release memory occupied by the process
for (Short i = 0; i < array_ram.length; i++) {
if (array_ram[i] == processId) {
array_ram[i] = 0;
success = true;
}
}
if (success) {
System.out.println("Memory released successfully...!");
} else {
System.out.println("Could not find process with id " + processId);
}
allocateFromQueue(array_ram, waitingQueue);
}
private static void allocateFromQueue(int[] array_ram,Queue<E_process> waitingQueue){
while (!waitingQueue.isEmpty()) {
E_process temp = waitingQueue.poll();
boolean allocated = false;
for (Short j = 0; j < array_ram.length; j++) {
if (array_ram[j] == 0) {
Short size = temp.getsize();
boolean fits = true;
for (Short k = j; k < j + size; k++) {
if (k >= array_ram.length || array_ram[k] != 0) {
fits = false;
break;
}
}
if (fits) {
for (Short k = j; k < j + size; k++) {
array_ram[k] = temp.getid();
}
allocated = true;
break;
}
}
}
if (!allocated) {
waitingQueue.add(temp);
break;
}
}
print_ram(array_ram);
}
/**
* Analyzes memory fragmentation by counting free blocks and total free memory.
* @param array_ram RAM array.
*/
public static void analyzeFragmentation(int[] array_ram) {
int freeBlocks = 0;
int totalFreeSize = 0;
int currentBlockSize = 0;
boolean inFreeBlock = false;
for (int i = 0; i < array_ram.length; i++) {
if (array_ram[i] == 0) {
if (!inFreeBlock) {
inFreeBlock = true;
freeBlocks++;
}
currentBlockSize++;
} else {
if (inFreeBlock) {
totalFreeSize += currentBlockSize;
currentBlockSize = 0;
inFreeBlock = false;
}
}
}
if (inFreeBlock) {
totalFreeSize += currentBlockSize;
}
System.out.println("Memory Fragmentation Analysis:");
System.out.println("Number of free blocks: " + freeBlocks);
System.out.println("Total free memory size: " + totalFreeSize);
}
/**
* Checks if the input RAM size is valid (greater than 0).
* @param sc Scanner object for input.
* @return Valid RAM size.
*/
public static Short checkRam(Scanner sc) {
Short temp = sc.nextShort();
while (temp <= 0) {
System.out.print("Ram size must be greater than 0, please try again:");
temp = sc.nextShort();
}
return temp;
}
/**
* Ensures the input value is non-negative.
* @param sc Scanner object for input.
* @return Valid non-negative input.
*/
public static Short checkNegativeInput(Scanner sc) {
Short temp = sc.nextShort();
while (temp < 0) {
System.out.print("Input value is negative, please enter a non-negative value: ");
temp = sc.nextShort();
}
return temp;
}
/**
* Main method to run the memory management simulation.
* @param args Command line arguments.
*/
public static void main(String[] args) {
Process[] p = new Process[100];
Scanner sc = new Scanner(System.in);
boolean exit = false;
while (!exit) {
try {
System.out.print("Enter size of your RAM:");
Short size_of_ram = checkRam(sc);
final int[] array_ram = new int[size_of_ram];
E_process[] ep = new E_process[100];
System.out.print("Enter no. of pre-running processes:");
Short pre_run_process = checkNegativeInput(sc);
add_process(pre_run_process, p, array_ram, sc);
print_ram(array_ram);
while (true) {
System.out.println("\n1) Allocation in First Fit");
System.out.println("2) Allocation in Best Fit");
System.out.println("3) Allocation in Worst Fit");
System.out.println("4) Compact Memory");
System.out.println("5) Release Memory for Process");
System.out.println("6) Memory Fragmentation Analysis");
System.out.println("7) Exit");
System.out.print("Enter your choice:");
Short choice = checkNegativeInput(sc);
switch (choice) {
case 1:
First_Fit(sc, array_ram, ep);
break;
case 2:
Best_Fit(sc, choice, array_ram, ep);
break;
case 3:
Worst_fit(sc, choice, array_ram, ep);
break;
case 4:
compact_memory(array_ram, waitingQueue);
break;
case 5:
System.out.print("Enter process id to release memory:");
Short processId = checkNegativeInput(sc);
release_memory(processId, array_ram, waitingQueue);
break;
case 6:
analyzeFragmentation(array_ram);
break;
case 7:
exit = true;
break;
default:
System.out.println("Invalid choice");
break;
}
if (exit) {
break;
}
}
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
System.out.print("Do you want to retry the operation? (yes/no)");
String retryChoice = sc.next();
if (!retryChoice.equalsIgnoreCase("yes")) {
exit = true;
}
}
}
sc.close();
}
}