-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextFit.java
More file actions
56 lines (45 loc) · 1.71 KB
/
NextFit.java
File metadata and controls
56 lines (45 loc) · 1.71 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
import java.util.ArrayList;
public class NextFit extends MemoryAllocationAlgorithm {
public NextFit(int[] availableBlockSizes) {
super(availableBlockSizes);
}
/**
* This method is used to load a process into a memory slot using the Next Fit algorithm
* and returns the address of the memory slot, if the process fits.
* @param p is a Process object that needs tobe loaded into memory.
* @param currentlyUsedMemorySlots is the Arraylist containing the Memory slots that are occupied by one or more processes.
* @return Returns the address of the memory slot where the process is loaded. Returns -1 if the process doesn't fit anywhere.
*/
static int count = 0;
public int fitProcess(Process p, ArrayList<MemorySlot> currentlyUsedMemorySlots) {
boolean fit = false;
int address = -1;
int freeSpace;
int requirementMemory = p.getMemoryRequirements();
int i = count;
while (i<availableBlockSizes.length)
{
if (requirementMemory <= availableBlockSizes[i])
{
if (currentlyUsedMemorySlots.get(i) != null)
{
freeSpace = currentlyUsedMemorySlots.get(i).getBlockEnd() - currentlyUsedMemorySlots.get(i).getEnd();
if ((requirementMemory < freeSpace))
{
address = i;
fit = true;
count = i;
}
}
else
{
address = i;
fit = true;
count = i;
}
}
i++;
}
return address;
}
}