-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryAllocater.java
More file actions
197 lines (158 loc) · 5.35 KB
/
Copy pathMemoryAllocater.java
File metadata and controls
197 lines (158 loc) · 5.35 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
import java.lang.*;
import java.io.*;
import java.util.*;
public class MemoryAllocater {
public static void main(String[] args) {
String fileName = "Minput.txt";
int size = readS("Minput.txt");
fileName = "Pinput.txt";
size = readS("Pinput.txt");
MemorySlot mem[] = new MemorySlot[size];
mem = readM(size, fileName);
Process p[] = new Process[size];
p = readP(size, fileName);
MemorySlot fin[] = new MemorySlot[size];
for(int i = 0; i < size; i++){
fin[i] = new MemorySlot();
}
int c = FF(size, p, mem, fin);
}
// read file from Minput
public static int readS(String fileName){
int size = 0;
try {
Reader inp = new FileReader(fileName);
StreamTokenizer tstream = new StreamTokenizer(inp);
tstream.parseNumbers();
int token = tstream.nextToken();
size = (int)tstream.nval;
}
catch(IOException e){
System.out.println(e);
}
return size;
}
public static MemorySlot[] readM(int size, String fileName){
MemorySlot[] temp = new MemorySlot[size];
for(int i = 0; i < size; i++){
temp[i] = new MemorySlot();
}
try {
Reader inp = new FileReader(fileName);
StreamTokenizer tstream = new StreamTokenizer(inp);
tstream.parseNumbers();
int token = tstream.nextToken();
for(int i = 0; i < size; i++){
if(token == StreamTokenizer.TT_EOF)
break;
else{
token = tstream.nextToken();
temp[i].setSTime((int)tstream.nval);
token = tstream.nextToken();
temp[i].setETime((int)tstream.nval);
temp[i].setSize();
temp[i].setUsed(false);
}
}
}
catch(IOException e){
System.out.println(e);
}
return temp;
}
public static Process[] readP(int size, String fileName){
Process[] temp = new Process[size];
for(int i = 0; i < size; i++){
temp[i] = new Process();
}
try {
Reader inp = new FileReader(fileName);
StreamTokenizer tstream = new StreamTokenizer(inp);
tstream.parseNumbers();
int token = tstream.nextToken();
for(int i = 0; i < size; i++){
if(token == StreamTokenizer.TT_EOF)
break;
else{
token = tstream.nextToken();
temp[i].setPid((int)tstream.nval);
token = tstream.nextToken();
temp[i].setSize((int)tstream.nval);
temp[i].setUsed(false);
}
}
}
catch(IOException e){
System.out.println(e);
}
return temp;
}
// First Fit Method
public static int FF(int size, Process ps[], MemorySlot mem[], MemorySlot ffMem[]){
int c = 0; // create the varible that will return the negative
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(ps[i].getSize() <= mem[j].getSize()){ // if the size of the process is less than the size of the memory slot
if( !mem[j].getUsed()){ // and if the memory slot isn't used
if (!ps[i].getUsed()){ // and if the process slot hasn't been used
// Add the process to the memory slot and calculate the correct end time
ffMem[j].setSTime(mem[j].getSTime());
ffMem[j].setETime(mem[j].getETime() - (mem[j].getSize() - ps[i].getSize()));
ffMem[j].setESize(ps[i].getPid());
ffMem[j].setUsed(true); // the memory slot is used
ps[i].setUsed(true); // the process is used
mem[j].setUsed(true); // the memory slot is used
}
}
}
}
// if the process hasn't been used add it to c and make it a negative
if (!ps[i].getUsed())
c = -ps[i].getPid();
}
return c;
}
public static int BF(int size, Process ps[], MemorySlot mem[], MemorySlot bfMem[]){
MemorySlot[] temp = new MemorySlot[size];
temp = mem;
int c = 0;
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(temp[j].getESize() >= ps[i].getSize()){
bfMem[j].setSTime(mem[j].getSTime());
bfMem[j].setETime(mem[j].getETime() - (mem[j].getSize() - ps[i].getSize()));
bfMem[j].setESize(ps[i].getPid());
}
else if(temp[i].getESize() >= ps[j].getSize()){
bfMem[i].setSTime(mem[i].getSTime());
bfMem[i].setETime(mem[i].getETime() - (mem[i].getSize() - ps[j].getSize()));
bfMem[i].setESize(ps[j].getPid());
}
else if(temp[i].getESize() >= ps[i].getSize()){
bfMem[i].setSTime(mem[i].getSTime());
bfMem[i].setETime(mem[i].getETime() - (mem[i].getSize() - ps[i].getSize()));
temp[i].setESize(mem[j].getSize() - ps[i].getSize());
bfMem[i].setESize(ps[i].getPid());
}
}
}
return c;
}
public void WF(int size, Process ps[], MemorySlot mem[], MemorySlot wfMem[]){
}
public static void write(int size, MemorySlot mem[], String outName, int c){
try {
FileWriter output = new FileWriter(outName);
for(int i = 0; i < size; i++){
output.write(String.valueOf(mem[i].getSTime() + " "));
output.write(String.valueOf(mem[i].getETime() + " "));
output.write(String.valueOf(mem[i].getSize() + " " + "\n"));
}
output.write(String.valueOf(c));
output.close();
}
catch(IOException e){
System.out.println("ERROR" + e);
}
}
}