-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDynamicStorageAllocation.java
94 lines (88 loc) · 3.45 KB
/
DynamicStorageAllocation.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
public class DynamicStorageAllocation{
static int[] mPartition = {100,500,200,300,600}; // memory partitions
static int[] processess = {212,417,112,426}; // memory needed for process
public static void main(String[] args){
firstFit(mPartition, processess);
bestFit(mPartition, processess);
worstFit(mPartition, processess);
}
//first-fit
public static void firstFit(int[] memory, int[] process){
int[] mem = new int[memory.length];
int[] pro = new int[process.length];
for(int i = 0; i < memory.length; i++){ mem[i] = memory[i];}
for(int i = 0; i < process.length; i++){ pro[i] = process[i];}
System.out.println("First-fit dynamic memory allocation : ");
for(int each : pro){
boolean stop = true;
for(int i = 0; i < mem.length; i++){
if(each <= mem[i]){
System.out.println(each+" -> "+mem[i]+" remain : "+(mem[i]-each));
mem[i] -= each;
stop = false;
break;
}
}
if(stop){
System.out.println("cannot be allocated : "+each);
break;
}
}
}
//best-fit
public static void bestFit(int[] memory, int[] process){
int[] mem = new int[memory.length];
int[] pro = new int[process.length];
for(int i = 0; i < memory.length; i++){ mem[i] = memory[i];}
for(int i = 0; i < process.length; i++){ pro[i] = process[i];}
System.out.println("Best-fit dynamic memory allocation : ");
for(int each : pro){
if(doesFit(mem, each)){
int memIndex = 0;
for(int i = 0; i < mem.length; i++){
if(each <= mem[i]){memIndex = i; break;}
}
for(int i = 0; i < mem.length; i++){
if(each <= mem[i] && mem[i] < mem[memIndex]){memIndex = i;}
}
System.out.println(each+" -> "+mem[memIndex]+" remain : "+(mem[memIndex]-each));
mem[memIndex] -= each;
}
else{
System.out.println("cannot be allocated : "+each);
break;
}
}
}
public static boolean doesFit(int[] memory, int size){
for(int each : memory){
if(size <= each) {return true;}
}
return false;
}
//best-fit
public static void worstFit(int[] memory, int[] process){
int[] mem = new int[memory.length];
int[] pro = new int[process.length];
for(int i = 0; i < memory.length; i++){ mem[i] = memory[i];}
for(int i = 0; i < process.length; i++){ pro[i] = process[i];}
System.out.println("worst-fit dynamic memory allocation : ");
for(int each : pro){
if(doesFit(mem, each)){
int mIndex = 0;
for(int i = 0; i < mem.length; i++){
if(each <= mem[i]){mIndex = i; break;}
}
for(int i = 0; i < mem.length; i++){
if(each <= mem[i] && mem[i] > mem[mIndex]){mIndex = i;}
}
System.out.println(each+" -> "+mem[mIndex]+" remain : "+(mem[mIndex]-each));
mem[mIndex] -= each;
}
else{
System.out.println("cannot be allocated : "+each);
break;
}
}
}
}