-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathMemoryAllocations.java
340 lines (257 loc) · 9.33 KB
/
MemoryAllocations.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
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
// Best Fit , First Fit , Worst Fit
// Simulate first-fit, best-fit and worst-fit memory allocations
/*
FIRST FIT (FF)
*A resource allocation scheme (usually for memory).
*First Fit fits data into memory by scanning from the beginning of available memory to the end,
until the first free space which is at least big enough to accept the data is found.
*This space is then allocated to the data.
*Any left over becomes a smaller, separate free space.
*If the data to be allocated is bigger than the biggest free space,
the request cannot be met, and an error will be generated
BEST FIT (BF)
*The best fit deals with allocating the smallest free partition which meets the requirement of the requesting process.
*This algorithm first searches the entire list of free partitions and considers the smallest hole that is adequate.
*It then tries to find a hole which is close to actual process size needed.
WORST FIT (WF)
*The algorithm searches for free-space in memory in which it can store the desired information.
*The algorithm selects the largest possible free space that the information can be stored on
(i.e. that is bigger than the information needing to be stored) and stores it there.
*This is directly opposed to the best fit algorithm which searches the memory in much the same way as before.
*/
import java.util.*;
public class MemoryAllocations {
// Array for storing the size of differnt memory blocks
static int block[] = new int[10];
// Array for storing the size of memory
// required by each process
static int process[] = new int[10];
static int i;
static int j;
static int b;
static int p;
public static void initialize() {
// Creating Scanner Object to read input from user
Scanner t = new Scanner(System.in);
// Fill the number of blocks
System.out.println("Enter the number of memory blocks");
b = t.nextInt();
// Fill the size of the blocks
System.out.println("Enter the size of each memory block");
for (i = 0; i < b; i++) {
block[i] = t.nextInt();
}
// Fill the number of processes
System.out.println("Enter the number of processes");
p = t.nextInt();
// Fill the size of the processes
System.out.println("Enter the size of each process");
for (i = 0; i < p; i++) {
process[i] = t.nextInt();
}
}
// Function to implement first fit allocation
public static void firstfit() {
int flag[] = new int[10];
int allocated[] = new int[10];
// Function call to initialize memory and process blocks
initialize();
System.out.println("First-Fit Memory Allocation");
// Initially no block is assigned to any process
for (i = 0; i < 10; i++) {
flag[i] = 0;
allocated[i] = -1;
}
/* pick each process and find suitable blocks
according to its size and assign to it */
for (i = 0; i < p; i++) {
for (j = 0; j < b; j++) {
if (flag[j] == 0 && block[j] >= process[i]) {
allocated[j] = i;
flag[j] = 1;
break;
}
}
}
// printing result after memory allocation
System.out.println("Block no.\tsize\t\tprocess no.\t\tsize");
for (i = 0; i < b; i++) {
System.out.printf("\n%d\t\t%d\t\t", i + 1, block[i]);
if (flag[i] == 1)
System.out.printf("%d\t\t\t%d", allocated[i] + 1, process[allocated[i]]);
else
System.out.printf("Not allocated");
}
}
// Function to implement best fit allocation
public static void bestfit() {
final int barray[] = new int[20];
final int parray[] = new int[20];
int fragment[] = new int[20];
int temp;
int lowest = 9999;
// Function call to initialize memory and process blocks
initialize();
System.out.println("Best-Fit Memory Allocation");
/* pick each process and find suitable blocks
according to its size and assign to it
*/
for (i = 0; i < p; i++) {
// Find the best fit block for current process
for (j = 0; j < b; j++) {
if (barray[j] != 1) {
temp = block[j] - process[i];
if (temp >= 0) {
if (lowest > temp) {
parray[i] = j;
lowest = temp;
}
}
}
}
// Remaining Size of that particular block selected after memory allocation
fragment[i] = lowest;
barray[parray[i]] = 1;
lowest = 10000;
}
// printing result after memory allocation
System.out.printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for (i = 0; i < p && parray[i] != 0; i++)
System.out.printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, process[i], parray[i], block[parray[i]], fragment[i]);
}
// Function to implement worst fit allocation
public static void worstfit() {
int temp;
int highest = 0;
int fragments[] = new int[10];
final int blockArr[] = new int[10];
final int fileArr[] = new int[10];
// Function call to initialize memory and process blocks
initialize();
System.out.printf("\n Worst-Fit Memory Allocation");
for (i = 0; i < p; i++) {
// Find the best fit block for current process
for (j = 0; j < b; j++) {
if (blockArr[j] != 1) {
temp = block[j] - process[i];
if (temp >= 0) {
if (highest < temp) {
fileArr[i] = j;
highest = temp;
}
}
}
// Remaining Size of that particular block selected after memory allocation
fragments[i] = highest;
blockArr[fileArr[i]] = 1;
highest = 0;
}
}
// printing result after memory allocation
System.out.printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
for (i = 0; i < p; i++) {
System.out.printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, process[i], fileArr[i], block[fileArr[i]],
fragments[i]);
}
System.out.printf("\n");
}
public static void main(String[] args) {
int choice;
boolean flag = true;
System.out.println("Press 1 to simulate first-fit memory allocation");
System.out.println("Press 2 to simulate best-fit memory allocation");
System.out.println("Press 3 to simulate worst-fit memory allocation");
System.out.println("Press 4 to exit");
System.out.println();
while (flag) {
System.out.printf("\nEnter the choice\n");
// Creating Scanner Object to read input from user
Scanner s = new Scanner(System.in);
// Read the choice given by the user
choice = s.nextInt();
switch (choice) {
case 1:
firstfit();
break;
case 2:
bestfit();
break;
case 3:
worstfit();
break;
case 4:
flag = false;
break;
}
}
}
}
/*
* TEST CASES
*
*
* Press 1 to simulate first-fit memory allocation
* Press 2 to simulate best-fit memory allocation
* Press 3 to simulate worst-fit memory allocation
* Press 4 to exit
*
*
* Enter the choice
* 2
* Enter the number of memory blocks
* 5
* Enter the size of each memory block
* 10 15 5 9 3
* Enter the number of processes
* 4
* Enter the size of each process
* 1 4 7 12
*
* Best-Fit Memory Allocation
* Process_no Process_size Block_no Block_size Fragment
* 0 1 4 3 2
* 1 4 2 5 1
* 2 7 3 9 2
* 3 12 1 15 3
*
*
* Enter the choice
* 3
* Enter the number of memory blocks
* 5 Enter the size of each memory block
* 5 4 3 6 7
* Enter the number of processes 4
* Enter the size of each process
* 1 3 5 3
*
* Worst-Fit Memory Allocation
* File Number File Size Block Number Block Size Fragment
* 0 1 4 7 6
* 1 3 0 5 0
* 2 5 0 5 0
* 3 3 0 5 0
*
*
*
* Enter the choice
* 1
* Enter the number of memory blocks
* 3
* Enter the size of each memory block
* 12 7 4
* Enter the number of processes
* 3
* Enter the size of each process
* 7 4 9
*
* First-Fit Memory Allocation
* Block no. size process no. size
*
* 1 12 1 7
* 2 7 2 4
* 3 4 3 Not allocated
*
*
*
*
*/