-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstFit.java
More file actions
51 lines (42 loc) · 1.63 KB
/
FirstFit.java
File metadata and controls
51 lines (42 loc) · 1.63 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
import java.util.ArrayList;
public class FirstFit extends MemoryAllocationAlgorithm {
public FirstFit(int[] availableBlockSizes) {
super(availableBlockSizes);
}
/**
* This method is used to load a process into a memory slot using the First 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.
*/
public int fitProcess(Process p, ArrayList<MemorySlot> currentlyUsedMemorySlots) {
boolean fit = false;
int address = -1;
int freeSpace;
int requirementMemory = p.getMemoryRequirements();
int i=0;
while (i<availableBlockSizes.length && !fit)
{
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;
}
}
else
{
address = i;
fit = true;
}
}
i++;
}
return address;
}
}