-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
286 lines (254 loc) · 11.4 KB
/
Main.java
File metadata and controls
286 lines (254 loc) · 11.4 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
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;
public class Main {
private static int M;
private static int KB;
private static Partition[] memory;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user to enter the number of partitions
System.out.print("Enter the number of partitions: ");
try {
M = scanner.nextInt();
if (M < 1) {
throw new Exception("Number of partitions must be a positive integer between 1 and ");
}
} catch (Exception e) {
System.out.println("Invalid input: " + e.getMessage());
return;
}
// Prompt user to enter the size of each partition
System.out.println("Enter the size of each partition in KB:");
memory = new Partition[M];
int start = 0;
int end = 0;
for (int i = 0; i < M; i++) {
System.out.print("Partition " + (i + 1) + ": ");
try {
KB = scanner.nextInt();
if (KB < 1) {
throw new Exception("Partition size must be a positive integer.");
}
end = start + (KB) - 1;
memory[i] = new Partition(start, end, KB);
start = memory[i].getEndAddress() + 1;
} catch (Exception e) {
System.out.println("Invalid input: " + e.getMessage());
return;
}
}
char strategy = 'N';
boolean flag = true;
// Prompt user to enter the allocation strategy
while (flag) {
System.out.print("Enter the allocation strategy (F, B, or W): ");
try {
strategy = scanner.next().toUpperCase().charAt(0);
if (!(strategy == 'F' || strategy == 'B' || strategy == 'W'))
throw new Exception("Input should be f, b or w");
else
flag = false;
} catch (Exception e) {
System.out.println("Invalid input: " + e.getMessage());
}
}
// Loop until user chooses to exit
int choice = 0;
while (choice != 4) {
// Display menu and prompt user for choice
System.out.println("\nMemory Management Menu:");
System.out.println("1. Allocate a block of memory");
System.out.println("2. De-allocate a block of memory");
System.out.println("3. Report detailed information about memory blocks");
System.out.println("4. Exit");
System.out.print("Enter your choice (1-4): ");
choice = scanner.nextInt();
switch (choice) {
case 1:
// Allocate memory block
try {
boolean flagID = false;
String processId ="";
while(!flagID){
System.out.print("Enter process ID as: ");
processId = "P" + scanner.next();
try {
int num = Integer.parseInt(processId.substring(1));
if (num < 0)
throw new Exception();
else
flagID = true;
} catch (Exception e) {
System.out.println("Process ID must be a positive integer");
}
}
System.out.print("Enter process size: ");
int processSize = scanner.nextInt();
if (processSize < 1) {
throw new Exception("Process size must be a positive integer ");
}
Boolean allocated = false;
// Allocate memory from using the selected allocation strategy
switch (strategy) {
case 'F':
allocated = firstFit(processId ,processSize);
break;
case 'B':
allocated = bestFit(processId, processSize);
break;
case 'W':
allocated = worstFit(processId ,processSize);
break;
}
if (!allocated) {
System.out.println("Error: Not enough memory available to allocate process.");
} else {
System.out.println("Allocation done successfully.");
// Display memory state after allocation
System.out.print("Memory state after allocation: [");
for (int i = 0; i < M; i++) {
if (memory[i].getStatus().equals("allocated")) {
System.out.print(memory[i].getProcessNum());
} else {
System.out.print("H");
}
if (i < M - 1) {
System.out.print(" | ");
}
}
System.out.println("]");
}
} catch (Exception e) {
System.out.println("Invalid input: " + e.getMessage());
}
break;
case 2:
// De-allocate memory block
try {
System.out.print("Enter process ID to release): ");
String processId = "P" + scanner.next();
int validate = Integer.parseInt(processId.substring(1));
if (validate < 1) {
throw new Exception("Process ID must be a positive integer");
}
// Find the partition that has been allocated to the process and de-allocate it
boolean found = false;
for (int i = 0; i < M; i++) {
if (memory[i].getStatus().equals("allocated")
&& memory[i].getProcessNum().equals(processId)) {
memory[i].setStatus("free");
memory[i].setProcessNum("-1");
memory[i].setFragmentSize(-1);
found = true;
System.out.println("De-allocation done successfully.");
// Display memory state after de-allocation
System.out.print("Memory state after de-allocation: [");
for (int j = 0; j < M; j++) {
if (memory[j].getStatus().equals("allocated")) {
System.out.print(memory[j].getProcessNum());
} else {
System.out.print("H");
}
if (j < M - 1) {
System.out.print(" | ");
}
}
System.out.println("]");
break;
}
}
if (!found) {
System.out.println("Error: Process ID not found or not allocated.");
}
} catch (Exception e) {
System.out.println("Invalid input: " + e.getMessage());
}
break;
case 3:
// Print detailed information about memory blocks to console and write to file
System.out.println("Memory block information:");
for (int i = 0; i < M; i++) {
System.out.println(memory[i].toString());
}
try {
File file = new File("Report.txt");
FileWriter writer = new FileWriter(file);
for (int i = 0; i < M; i++) {
writer.write(memory[i].toString() + "\n");
}
writer.close();
System.out.println("Memory block information written to file: Report.txt");
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
break;
case 4:
// Exit loop
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid input: Please enter a number between 1 and 4.");
}
}
}
// findFirstFitPartition
static boolean firstFit(String name, int size) {
boolean allocated = false;
for (int i = 0; i < memory.length; i++) {
int space = memory[i].getPartitionSize();
if ((memory[i].getStatus() != "allocated") && (size <= space)) {
memory[i].setStatus("allocated");
memory[i].setProcessNum(name);
memory[i].setProcessSize(size);
memory[i].calculateInternalFragment();
allocated = true;
break;
}
}
return allocated;
}
// findBestFitPartition
static boolean bestFit(String processID, int processSize) {
int bestFit = -1;
for (int i = 0; i < memory.length; i++) {
if (memory[i].getStatus().equals("free") && memory[i].getPartitionSize() >= processSize) {
if (bestFit == -1)
bestFit = i;
else if (memory[i].getPartitionSize() < memory[bestFit].getPartitionSize())
bestFit = i;
}
}
if (bestFit != -1) {
memory[bestFit].setStatus("allocated");
memory[bestFit].setProcessNum(processID);
memory[bestFit].setProcessSize(processSize);
memory[bestFit].calculateInternalFragment();
return true;
} else
System.out.println("Sorry, there is no free space");
return false;
}
// findWorstFitPartition
static boolean worstFit(String processID, int processSize) {
int WorstFit = -1;
for (int i = 0; i < memory.length; i++) {
if (memory[i].getStatus().equals("free") && memory[i].getPartitionSize() >= processSize) {
if (WorstFit == -1)
WorstFit = i;
else if (memory[i].getPartitionSize() > memory[WorstFit].getPartitionSize())
WorstFit = i;
}
}
if (WorstFit != -1) {
memory[WorstFit].setStatus("allocated");
memory[WorstFit].setProcessNum(processID);
memory[WorstFit].setProcessSize(processSize);
memory[WorstFit].calculateInternalFragment();
return true;
} else
return false;
}
}