This repository was archived by the owner on Feb 5, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathContiguousAllocation.java
More file actions
64 lines (56 loc) · 2.32 KB
/
Copy pathContiguousAllocation.java
File metadata and controls
64 lines (56 loc) · 2.32 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
import java.util.Arrays;
public class ContiguousAllocation {
int[] memory; // Representasi memori sebagai array
int[] processSizes; // Ukuran proses yang ingin dialokasikan
public ContiguousAllocation(int totalMemorySize) {
this.memory = new int[totalMemorySize]; // 1 unit = 1 KB
Arrays.fill(memory, -1); // -1 artinya kosong, 0+ adalah ID proses
}
// Method untuk alokasi First Fit
public void firstFit(int[] processSizes) {
for (int processID = 0; processID < processSizes.length; processID++) {
int size = processSizes[processID];
boolean allocated = false;
// Cari hole pertama yang cukup besar
for (int i = 0; i < memory.length; i++) {
if (memory[i] == -1) {
int count = 0;
int j = i;
while (j < memory.length && memory[j] == -1 && count < size) {
count++;
j++;
}
if (count >= size) {
// Alokasikan proses
for (int k = 0; k < size; k++) {
memory[i + k] = processID;
}
System.out.println("Proses " + processID + " (" + size + "KB) dialokasikan dari alamat " + i);
allocated = true;
break;
}
}
}
if (!allocated) {
System.out.println("Proses " + processID + " (" + size + "KB) TIDAK BISA dialokasikan.");
}
}
}
// Menampilkan status memori
public void printMemoryStatus() {
System.out.println("\nStatus Memori:");
for (int i = 0; i < memory.length; i++) {
System.out.print((memory[i] == -1 ? "[ ]" : "[" + memory[i] + "]"));
}
System.out.println();
}
// Main method untuk test
public static void main(String[] args) {
int totalMemory = 20; // Total memori 20KB
int[] processes = {5, 7, 3, 6}; // Ukuran proses dalam KB
ContiguousAllocation allocator = new ContiguousAllocation(totalMemory);
System.out.println("Alokasi Memori dengan Metode First Fit");
allocator.firstFit(processes);
allocator.printMemoryStatus();
}
}