-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.java
More file actions
356 lines (292 loc) · 11.5 KB
/
main.java
File metadata and controls
356 lines (292 loc) · 11.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
import java.util.Scanner;
import java.io.*;
import java.util.*;
class invalid extends InputMismatchException{
invalid(){} }
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sadd,eadd,numOfpartitions=0;
boolean e1=true,e2=true,e3=true,e4=true;
//Number of Parition validation(1)
while(e1){
try{
System.out.println("Please enter the number of partitions : ");
numOfpartitions=input.nextInt();
if(numOfpartitions<=0)
throw new invalid();
e1=false;
}catch(invalid e){
String str=input.next();
System.out.println("invalid number input, Please try again");
}
catch(InputMismatchException e){
String str=input.next();
System.out.println("invalid input, Please enter numbers only.");}
}//end while1
//start Memory array creation
Partition[] memory = new Partition[numOfpartitions];
int partitionSize=0;
for(int i=0;i<numOfpartitions;i++){
e2=true;
//PartionSize validation(2)
while(e2){
try {
System.out.println("Please enter the size of partition "+(i+1)+" in KB :");
partitionSize=input.nextInt();
if (partitionSize<=0)
throw new invalid();
e2=false;
if(i==0){ //if it is the 1st parition set start to 0
sadd=0;
}
else{
sadd=memory[i-1].getEadd(); //if it's not the first process then it will be stored after the ending address
}
eadd=sadd + partitionSize;
memory[i]=new Partition(partitionSize,sadd,eadd);
}catch(invalid e){
System.out.println("invalid size input, Please try again");
}
catch(InputMismatchException e){
input.next();
System.out.println("invalid size type input, Please enter numbers only.");
}
} //end while2
}//end loop
String approach="";
//ApproachInput Validation(3)
while(e3){
try{
System.out.println("Please enter the allocation approach:[ First-fit (F), Best-fit(B), or Worst-fit (W) ]");
approach=input.next();
approach=approach.toUpperCase();
if(!(approach.equals("F")||approach.equals("B")||approach.equals("W")))
throw new invalid();
e3=false;
}catch(invalid e){
System.out.println("invalid selection input, Please try again.");
}
catch(InputMismatchException e){
input.next();
System.out.println("invalid type input, Please enter characters only.");
}
}//end while3
int selection=0;
String pname;
int pSize;
do{
try{
System.out.println("Please Select an Option \n[1] Allocate a block of memory\n[2] De-allocate a block of memory \n[3] Report details\n[4] Exit from program");
selection=input.nextInt();
switch(selection){
//allocate a block of memory
case 1:
boolean enter=true;
while(enter){
try{
System.out.println("Please enter a process ID in \"PN\" format:");
pname=input.next();
if (pname.length()<=1 )
throw new invalid();
else{
if ( !(pname.substring(0,1).equalsIgnoreCase("P") ) )
throw new invalid();
for ( int i=1 ; i<pname.length() ;i++)
if(!(Character.isDigit(pname.charAt(i)) ) )
throw new invalid();
pname = pname.toUpperCase();//unify format with P (uppercase)
System.out.println("Please enter a process size");
pSize=input.nextInt();
enter=false;
allocate(memory,pname,pSize,numOfpartitions,approach);
} //end else
}catch(invalid e){
System.out.println("invalid processID (PN) format input, Please try again.");
}catch(InputMismatchException e){
input.next();
System.out.println("invalid selection input, Please try again.");
}}
break;
//De-allocate a block memory
case 2:
boolean enter1=true;
while(enter1){
try{
System.out.println("Please enter a process ID");
pname=input.next();
if (pname.length()<=1 )
throw new invalid();
else{
if ( !(pname.substring(0,1).equalsIgnoreCase("P") ) )
throw new invalid();
for ( int i=1 ; i<pname.length() ;i++)
if(!(Character.isDigit(pname.charAt(i)) ) )
throw new invalid();
pname = pname.toUpperCase();//unify format with P (uppercase)
deallocate(pname,memory);
enter1=false;}
}catch(invalid e){
System.out.println("invalid processID (PN) format input, Please try again.");
}catch(InputMismatchException e){
input.next();
System.out.println("invalid selection input, Please try again."); }}
break;
//display Report details
case 3:
report(memory);
break;
case 4:
System.out.print("Thank you");
break;
default:
System.out.println("please select a valid input");
}//end switch
}catch(invalid e){
System.out.println("invalid processID (PN) format input, Please try again.");
}catch(InputMismatchException e){
input.next();
System.out.println("invalid selection input, Please try again."); }
} while(selection!=4);
} //end main
public static void printing(Partition[] p){
String pname="[";
for(int j=0;j<p.length;j++){
if (p[j].getStatus().equalsIgnoreCase("allocated"))
pname=pname+p[j].getPid()+"|";
else
pname=pname+"H|";
}
pname = pname.substring(0,pname.length()-1);
pname = pname + "]";
System.out.println(pname);
} //end printing method
public static void allocate(Partition[] p,String processId,int processSize,int numOfpartitions,String approach){
boolean full= true;
boolean notExist =true;
int partitionIndex=-1;
for(int i=0;i<numOfpartitions;i++)
if(p[i].getStatus().equals("Free")){
full=false;
}
if(full)
System.out.println("The memory is full");
else{
for(int j=0;j<numOfpartitions;j++)
if(p[j].getPid().equals(processId)){
System.out.println("This process is already exist");
notExist=false;
}
if(notExist){
switch(approach){
case "F":
partitionIndex=firstFit(p,processId, processSize);
break;
case "B":
partitionIndex=bestFit(p,processId,processSize);
break;
case "W":
partitionIndex=worstFit(p,processId, processSize);
break;
}
if(partitionIndex!=-1){
p[partitionIndex].setPid(processId);
p[partitionIndex].setStatus("Allocated");
p[partitionIndex].setProcessSize(processSize);
p[partitionIndex].setInternalFrag(p[partitionIndex].getSize()-p[partitionIndex].getProcessSize());
}
else{
System.out.println("the process size is larger than any free partition in the memory");
}
printing(p);
}
}
}
public static int worstFit(Partition[] p,String processId,int processSize){
int k =0;
int large =0;
boolean found=false;
while(p[large].getStatus().equalsIgnoreCase("allocated")){
large++;
}
for( k=large+1;k<p.length;k++)
{
if(p[k].getStatus().equalsIgnoreCase("Free"))
if(p[large].getSize()<p[k].getSize()&& p[k].getSize()>=processSize){
large=k;
found=true;}
}
if((p[large].getSize()>=processSize&&p[large].getStatus().equalsIgnoreCase("Free"))||found){
return large;
}
else {
return -1;
}
}
public static int firstFit(Partition[] p,String processId,int processSize){
for(int i=0; i<p.length;i++){
Partition pp=p[i];
if(p[i].getStatus().equals("Free")&&pp.getSize()>=processSize){
return i;}
}//end loop
return -1;
}//end method first
public static int bestFit(Partition[] p,String processId,int processSize){
int best=-1;
int smallest=10000000; //or Integer.max();
for(int i=0; i<p.length;i++){
Partition pp=p[i];//p pp=p[i];//
if(pp.getStatus().equals("Free")&&pp.getSize()>=processSize){
int frag=pp.getSize()-processSize;
if(frag<=smallest){
best=i;
smallest=frag;}
}
}//end loop
return best;}//end method Best
public static void deallocate(String pid, Partition[] p){
boolean found=false;
for(int i=0;i<p.length;i++){
if(p[i].getPid().equals(pid)){
p[i].setPid("Null");
p[i].setStatus("Free");
p[i].setInternalFrag(-1);
found=true;
}
}
if(!found)
System.out.println("Process not found!");
printing(p);
} //end method deallocate
public static void report(Partition[] p) {
try {
BufferedWriter reportFile = new BufferedWriter(new FileWriter("report.txt") );
System.out.println("Memory partitions details : ");
reportFile.write("Memory partitions details : " + "\n");
System.out.println("-------------------------------");
reportFile.write("-------------------------------" );
for (int i=0 ; i<p.length ; i++) { //Loop to display each partition details
System.out.println("(Partion " + (i+1) + ")");
reportFile.write("\n(Partion " + (i+1) + ")\n");
System.out.println("Status : " + p[i].getStatus() );
reportFile.write("Status : " + p[i].getStatus() + "\n" );
System.out.println("Size : " + p[i].getSize() );
reportFile.write("Size : " + p[i].getSize() + "\n" );
System.out.println("Starting Address : " + p[i].getSadd() );
reportFile.write("Starting Address : " + p[i].getSadd() + "\n" );
System.out.println("Ending Address : " + p[i].getEadd() );
reportFile.write("Ending Address : " + p[i].getEadd() + "\n" );
System.out.println("Process ID : " + p[i].getPid() );
reportFile.write("Process ID : " + p[i].getPid() + "\n" );
System.out.println("Internal fragmentation size : " + p[i].getInternalFrag() );
reportFile.write("Internal fragmentation size : " + p[i].getInternalFrag() + "\n" );
System.out.println("-------------------------------");
reportFile.write("-------------------------------");
}//end for
reportFile.flush();
reportFile.close();
}catch( IOException e ) {
e.printStackTrace();
}//end catch
}//end method report
}//end class