forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManagementAlgorithms.java
More file actions
282 lines (265 loc) · 11.8 KB
/
MemoryManagementAlgorithms.java
File metadata and controls
282 lines (265 loc) · 11.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
package com.thealgorithms.others;
import java.util.ArrayList;
/**
* @author Alexandros Lemonaris
*/
public abstract class MemoryManagementAlgorithms {
/**
* Method to allocate memory to blocks according to CPU algorithms.
* Use of inheritance to avoid repeated code.
* Abstract method since it is implemented different for each algorithm.
* It should return an ArrayList of Integers, where the index is the process
* ID (zero-indexed) and the value is the block number (also zero-indexed).
* @param sizeOfBlocks an int array that contains the sizes of the memory
* blocks available.
* @param sizeOfProcesses: an int array that contains the sizes of the
* processes we need memory blocks for.
* @return the ArrayList filled with Integers repressenting the memory
* allocation that took place.
*/
public abstract ArrayList<Integer> fitProcess(int[] sizeOfBlocks, int[] sizeOfProcesses);
/**
* A constant value used to indicate that an allocation has not been made.
* This value is used as a sentinel value to represent that no allocation has been made
* when allocating space in an array or other data structure.
* The value is -255 and is marked as protected and final to ensure that it cannot be modified
* from outside the class and that its value remains consistent throughout the program
* execution.
*
* @author: Ishan Makadia (github.com/intrepid-ishan)
* @version: April 06, 2023
*/
protected static final int NO_ALLOCATION = -255;
}
/**
* @author Dekas Dimitrios
*/
class BestFitCPU extends MemoryManagementAlgorithms {
/**
* Method to find the maximum valued element of an array filled with
* positive integers.
*
* @param array: an array filled with positive integers.
* @return the maximum valued element of the array.
*/
private static int findMaxElement(int[] array) {
int max = -1;
for (int value : array) {
if (value > max) {
max = value;
}
}
return max;
}
/**
* Method to find the index of the memory block that is going to fit the
* given process based on the best fit algorithm.
*
* @param blocks: the array with the available memory blocks.
* @param process: the size of the process.
* @return the index of the block that fits, or -255 if no such block
* exists.
*/
private static int findBestFit(int[] blockSizes, int processSize) {
// Initialize minDiff with an unreachable value by a difference between a blockSize and the
// processSize.
int minDiff = findMaxElement(blockSizes);
int index = NO_ALLOCATION; // If there is no block that can fit the process, return
// NO_ALLOCATION as the
// result.
for (int i = 0; i < blockSizes.length; i++) { // Find the most fitting memory block for the given process.
if (blockSizes[i] - processSize < minDiff && blockSizes[i] - processSize >= 0) {
minDiff = blockSizes[i] - processSize;
index = i;
}
}
return index;
}
/**
* Method to allocate memory to blocks according to the best fit algorithm.
* It should return an ArrayList of Integers, where the index is the process
* ID (zero-indexed) and the value is the block number (also zero-indexed).
*
* @param sizeOfBlocks: an int array that contains the sizes of the memory
* blocks available.
* @param sizeOfProcesses: an int array that contains the sizes of the
* processes we need memory blocks for.
* @return the ArrayList filled with Integers repressenting the memory
* allocation that took place.
*/
public ArrayList<Integer> fitProcess(int[] sizeOfBlocks, int[] sizeOfProcesses) {
// The array list responsible for saving the memory allocations done by the best-fit
// algorithm
ArrayList<Integer> memAlloc = new ArrayList<>();
// Do this for every process
for (int processSize : sizeOfProcesses) {
int chosenBlockIdx = findBestFit(sizeOfBlocks, processSize); // Find the index of the memory block going to be used
memAlloc.add(chosenBlockIdx); // Store the chosen block index in the memAlloc array list
if (chosenBlockIdx != NO_ALLOCATION) { // Only if a block was chosen to store the process in it,
sizeOfBlocks[chosenBlockIdx] -= processSize; // resize the block based on the process size
}
}
return memAlloc;
}
}
/**
* @author Dekas Dimitrios
*/
class WorstFitCPU extends MemoryManagementAlgorithms {
/**
* Method to find the index of the memory block that is going to fit the
* given process based on the worst fit algorithm.
*
* @param blocks: the array with the available memory blocks.
* @param process: the size of the process.
* @return the index of the block that fits, or -255 if no such block
* exists.
*/
private static int findWorstFit(int[] blockSizes, int processSize) {
int max = -1;
int index = -1;
for (int i = 0; i < blockSizes.length; i++) { // Find the index of the biggest memory block available.
if (blockSizes[i] > max) {
max = blockSizes[i];
index = i;
}
}
// If the biggest memory block cannot fit the process, return -255 as the result
if (processSize > blockSizes[index]) {
return NO_ALLOCATION;
}
return index;
}
/**
* Method to allocate memory to blocks according to the worst fit algorithm.
* It should return an ArrayList of Integers, where the index is the process
* ID (zero-indexed) and the value is the block number (also zero-indexed).
*
* @param sizeOfBlocks: an int array that contains the sizes of the memory
* blocks available.
* @param sizeOfProcesses: an int array that contains the sizes of the
* processes we need memory blocks for.
* @return the ArrayList filled with Integers repressenting the memory
* allocation that took place.
*/
public ArrayList<Integer> fitProcess(int[] sizeOfBlocks, int[] sizeOfProcesses) {
// The array list responsible for saving the memory allocations done by the worst-fit
// algorithm
ArrayList<Integer> memAlloc = new ArrayList<>();
// Do this for every process
for (int processSize : sizeOfProcesses) {
int chosenBlockIdx = findWorstFit(sizeOfBlocks, processSize); // Find the index of the memory block going to be used
memAlloc.add(chosenBlockIdx); // Store the chosen block index in the memAlloc array list
if (chosenBlockIdx != NO_ALLOCATION) { // Only if a block was chosen to store the process in it,
sizeOfBlocks[chosenBlockIdx] -= processSize; // resize the block based on the process size
}
}
return memAlloc;
}
}
/**
* @author Dekas Dimitrios
*/
class FirstFitCPU extends MemoryManagementAlgorithms {
/**
* Method to find the index of the memory block that is going to fit the
* given process based on the first fit algorithm.
*
* @param blocks: the array with the available memory blocks.
* @param process: the size of the process.
* @return the index of the block that fits, or -255 if no such block
* exists.
*/
private static int findFirstFit(int[] blockSizes, int processSize) {
for (int i = 0; i < blockSizes.length; i++) {
if (blockSizes[i] >= processSize) {
return i;
}
}
// If there is not a block that can fit the process, return -255 as the result
return NO_ALLOCATION;
}
/**
* Method to allocate memory to blocks according to the first fit algorithm.
* It should return an ArrayList of Integers, where the index is the process
* ID (zero-indexed) and the value is the block number (also zero-indexed).
*
* @param sizeOfBlocks: an int array that contains the sizes of the memory
* blocks available.
* @param sizeOfProcesses: an int array that contains the sizes of the
* processes we need memory blocks for.
* @return the ArrayList filled with Integers repressenting the memory
* allocation that took place.
*/
public ArrayList<Integer> fitProcess(int[] sizeOfBlocks, int[] sizeOfProcesses) {
// The array list responsible for saving the memory allocations done by the first-fit
// algorithm
ArrayList<Integer> memAlloc = new ArrayList<>();
// Do this for every process
for (int processSize : sizeOfProcesses) {
int chosenBlockIdx = findFirstFit(sizeOfBlocks, processSize); // Find the index of the memory block going to be used
memAlloc.add(chosenBlockIdx); // Store the chosen block index in the memAlloc array list
if (chosenBlockIdx != NO_ALLOCATION) { // Only if a block was chosen to store the process in it,
sizeOfBlocks[chosenBlockIdx] -= processSize; // resize the block based on the process size
}
}
return memAlloc;
}
}
/**
* @author Alexandros Lemonaris
*/
class NextFit extends MemoryManagementAlgorithms {
private int counter = 0; // variable that keeps the position of the last registration into the memory
/**
* Method to find the index of the memory block that is going to fit the
* given process based on the next fit algorithm. In the case of next fit,
* if the search is interrupted in between, the new search is carried out from the last
* location.
*
* @param blocks: the array with the available memory blocks.
* @param process: the size of the process.
* @return the index of the block that fits, or -255 if no such block
* exists.
*/
private int findNextFit(int[] blockSizes, int processSize) {
for (int i = 0; i < blockSizes.length; i++) {
if (counter + i >= blockSizes.length) {
counter = -i; // starts from the start of the array
}
if (blockSizes[i + counter] >= processSize) {
counter += i;
return counter;
}
}
// If there is not a block that can fit the process, return -255 as the result
counter += blockSizes.length; // counter keeps its last value
return NO_ALLOCATION;
}
/**
* Method to allocate memory to blocks according to the first fit algorithm.
* It should return an ArrayList of Integers, where the index is the process
* ID (zero-indexed) and the value is the block number (also zero-indexed).
*
* @param sizeOfBlocks: an int array that contains the sizes of the memory
* blocks available.
* @param sizeOfProcesses: an int array that contains the sizes of the
* processes we need memory blocks for.
* @return the ArrayList filled with Integers repressenting the memory
* allocation that took place.
*/
public ArrayList<Integer> fitProcess(int[] sizeOfBlocks, int[] sizeOfProcesses) {
// The array list responsible for saving the memory allocations done by the first-fit
// algorithm
ArrayList<Integer> memAlloc = new ArrayList<>();
// Do this for every process
for (int processSize : sizeOfProcesses) {
int chosenBlockIdx = findNextFit(sizeOfBlocks, processSize); // Find the index of the memory block going to be used
memAlloc.add(chosenBlockIdx); // Store the chosen block index in the memAlloc array list
if (chosenBlockIdx != NO_ALLOCATION) { // Only if a block was chosen to store the process in it,
sizeOfBlocks[chosenBlockIdx] -= processSize; // resize the block based on the process size
}
}
return memAlloc;
}
}