-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProjectMain.java
190 lines (151 loc) · 4.11 KB
/
ProjectMain.java
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
import java.util.Scanner;
public class ProjectMain {
public static void main(String [] args) {
Scanner read = new Scanner( System.in );
int M;
char alo;
Partition array[];
System.out.println("Enter number of partitions: ");
M = read.nextInt();
array=new Partition[M];
int start=0; //50
for( int i = 0 ; i < M ; i++) {
System.out.println("Enter the size of partition " + (i+1) + " in KB: ");
int s = read.nextInt(); //50
int end= start+s-1; //49 74
//set size of partition here
array[i]=new Partition(s,start,end);
start=end+1;
}
System.out.println("Enter allocation strategy ( F:First-fit , B:Best-fit, W;Worst-fit ): ");
alo = read.next().charAt(0);
//create memory first
int x;
do {
System.out.println("How do you want to manage your memory?");
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 regions of free and allocated memory blocks.");
System.out.println("(4) Exit.");
x = read.nextInt();
switch(x) {
case 1:
//allocating method
System.out.println("enter process size in KB");
int processSize=read.nextInt();
System.out.println("enter process ID");
String pID=read.next();
switch(alo) {
case 'F':
boolean done = firstFit(processSize,pID);
if(done){
System.out.println("allocating Partition is completed")
displayState();
}
else
System.out.println("allocating Partition is not completed")
break;
case 'B':
bestFit(PartitionSize,pID);
break;
case 'W':
worstFit(PartitionSize,pID);
break;
}//switch
break;
case 2:
//de-allocating method
System.out.println("enter process ID");
String pID=read.next();
boolean check=false;
for(int i=0;i<array.length; i++){
if(array[i].ID.equals(pID)){
array[i].deallocate();
check=true;
System.out.println("Process deallocated successfully");
}
}
if(check)
displayState();
else
System.out.println("error , can't deallocate process");
break;
case 3:
//reporting method
break;
}//switch
}while(x!=4);
}//main
public static boolean firstFit(int size,pId){
boolean check=false;
for(int i=0;i<array.length; i++){
if(array[i].pStatus.equals("free") && array[i].pSize>= size){
array[i].allocate(size,pId);
check=true;
return check;
}
}
return check;
}
public static boolean bestFit(int size,pId){
boolean check=false;
int indexMin =-1;
int minFrag=0;
for(int i=0;i<array.length; i++){
if(array[i].pStatus.equals("free") && array[i].pSize>= size){
int frag=array[i].pSize-size;
if(frag<minFrag){
minFrag=frag;
indexMin=i;
check=true;
}
}
}
if(check){
array[indexMin].allocate(size,pId);
System.out.println("allocating Partition is completed");
displayState();
}
else
System.out.println("allocating Partition is not completed");
}
public static worstFit(int size,pId){
boolean check=false;
int indexMax =-1;
int maxFrag=0;
for(int i=0;i<array.length; i++){
if(array[i].pStatus.equals("free") && array[i].pSize>= size){
int frag=array[i].pSize-size;
if(frag>maxFrag){
maxFrag=frag;
indexMax=i;
check=true;
}
}
}
if(check){
array[indexMax].allocate(size,pId);
System.out.println("allocating Partition is completed");
displayState();
}
else
System.out.println("allocating Partition is not completed");
}
public static displayState(){
String state="[";
for(int i=0;i<array.length; i++){
if(!array[i].ID.equals("Null"))
state=state+array[i].ID +" ";
else
state=state+"H ";
if(i+1 >=array.length)
state+="]"
else
state+="| "
}
System.out.println("Memory State:\n" + state);
}
}
//class
//test commit
//testttt