-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
286 lines (245 loc) · 7.21 KB
/
utils.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Defines all storage types used within software
// Can be used to emulate basic functioality of Main and Secondary Memory
class BasicMemory {
constructor(MemorySize) {
// Initiallzing memory location
this.memory = [];
for (let i = 0; i < MemorySize; i++) {
this.memory[i] = null;
}
}
// Adds new entry into memory in specifc location
add(content, memNumber, offset) {
// Content should be an array of elements
const memSize = memNumber - offset;
if (content > memSize) return 0;
// Copying content into memory at memNumber
let pointer = memNumber;
content.forEach((chunk) => {
this.memory[pointer] = chunk;
pointer++;
});
return 1;
}
// Used to find a specifc address and contents
find(index) {
return this.memory[index];
}
// Remove entry from specifc memory location
remove(index) {
if (!this.memory[index]) return 0;
const page = this.memory[index];
this.memory[index] = null;
return page;
}
// Get Current state of memory
getMemoryState() {
return this.memory;
}
}
function loadingProgram({
MainMemory,
mainMemorySize,
Swap,
TLB,
pageCache,
eventList,
programList,
pageSize,
}) {
// Loading Programs into random parts of memory
let pageCount = 1;
let newEventList = [...eventList];
programList.forEach((program, index) => {
let pageNo = Math.ceil(program.length / Number(pageSize));
let pages = [];
let start = 0;
for (let i = 0; i < pageNo; i++) {
if (MainMemory.find(mainMemorySize - 1)) {
Swap.add([program.slice(start, start + pageSize)], pageCount);
// Updating Page Table
const PageTable = {
...MainMemory.find(0),
[`P${index}-${i}`]: pageCount,
};
MainMemory.add([PageTable], 0);
pageCount += 1;
start += pageSize;
} else {
MainMemory.add([program.slice(start, start + pageSize)], pageCount);
// Updating Page Table
const PageTable = {
...MainMemory.find(0),
[`P${index}-${i}`]: pageCount,
};
MainMemory.add([PageTable], 0);
start += pageSize;
pageCount += 1;
}
newEventList.push({
mainMemory: MainMemory.getMemoryState().slice(),
tlb: TLB.getMemoryState().slice(),
swap: Swap.getMemoryState().slice(),
pageCache: pageCache.getMemoryState().slice(),
});
}
});
return newEventList;
}
function FIFO(
programList,
executionList,
mainMemorySize,
SwapSize,
TLBSize,
pageCacheSize,
pageSize
) {
if (programList.length < 2) return "Enter Bigger ProgramList";
if (pageSize > mainMemorySize)
return "Main Memory Size should be larger than Page Size";
if (mainMemorySize > SwapSize)
return "Swap Size should be bigger than mainMemory size";
// programList contains list of programs with their executing instructions
const MainMemory = new BasicMemory(mainMemorySize + 1); // RAM with 4Gigs
const Swap = new BasicMemory(SwapSize); // HardDisk swap
const TLB = new BasicMemory(TLBSize); // Translation Lookaside Buffer
const pageCache = new BasicMemory(pageCacheSize + 1);
let pageCachePointer = 1;
let TLBPointer = 0;
let queuePointer = 1;
let eventList = []; // Stores list of events
// Page Table
MainMemory.add([{}], 0);
pageCache.add([[]], 0);
eventList.push({
mainMemory: MainMemory.getMemoryState().slice(),
tlb: TLB.getMemoryState().slice(),
swap: Swap.getMemoryState().slice(),
pageCache: pageCache.getMemoryState().slice(),
});
eventList = loadingProgram({
MainMemory,
mainMemorySize: mainMemorySize + 1,
Swap,
pageCache,
TLB,
eventList,
programList,
pageSize,
});
// Simulating CPU fetching for page
executionList.forEach((execute) => {
// Checking pageCache for given page
if (pageCache.find(0).includes(execute)) {
console.log(`${execute} Present in Page Cache`);
return;
}
const memLocation = MainMemory.find(0)[execute];
// Removing page from Main Memory
let page = MainMemory.find(memLocation);
// Checking swap
if (!page) {
page = Swap.find(memLocation);
if (!page) return;
// Updating Page Table
let PageTable = MainMemory.find(0);
const pageAssoc = Object.keys(PageTable).filter(
(key) => PageTable[key] === queuePointer
)[0];
PageTable = {
...MainMemory.find(0),
[execute]: queuePointer,
[pageAssoc]: memLocation,
};
MainMemory.add([PageTable], 0);
eventList.push({
mainMemory: MainMemory.getMemoryState().slice(),
tlb: TLB.getMemoryState().slice(),
swap: Swap.getMemoryState().slice(),
pageCache: pageCache.getMemoryState().slice(),
});
let replace = MainMemory.remove(queuePointer);
Swap.add([replace], memLocation);
MainMemory.add([page], queuePointer);
eventList.push({
mainMemory: MainMemory.getMemoryState().slice(),
tlb: TLB.getMemoryState().slice(),
swap: Swap.getMemoryState().slice(),
pageCache: pageCache.getMemoryState().slice(),
});
queuePointer = queuePointer >= mainMemorySize ? 1 : queuePointer + 1;
}
eventList.push({
mainMemory: MainMemory.getMemoryState().slice(),
tlb: TLB.getMemoryState().slice(),
swap: Swap.getMemoryState().slice(),
pageCache: pageCache.getMemoryState().slice(),
});
// Adding page to Page Cache
pageCache.add([page], pageCachePointer);
let addresses = [...pageCache.find(0)];
addresses[pageCachePointer - 1] = execute;
pageCache.add([addresses], 0);
pageCachePointer =
pageCachePointer + 1 >= pageCacheSize + 1 ? 1 : pageCachePointer + 1;
eventList.push({
mainMemory: MainMemory.getMemoryState().slice(),
tlb: TLB.getMemoryState().slice(),
swap: Swap.getMemoryState().slice(),
pageCache: pageCache.getMemoryState().slice(),
});
// Adding Page Frame Location to TLB
TLB.add([{ pageNumber: execute, frameNumber: memLocation }], TLBPointer);
eventList.push({
mainMemory: MainMemory.getMemoryState().slice(),
tlb: TLB.getMemoryState().slice(),
swap: Swap.getMemoryState().slice(),
pageCache: pageCache.getMemoryState().slice(),
});
TLBPointer = TLBPointer >= TLBSize - 1 ? 0 : TLBPointer + 1;
console.log(`Getting: ${execute}`);
console.log("Main Memory");
console.log(MainMemory.getMemoryState());
console.log("Page Cache");
console.log(pageCache.getMemoryState());
console.log("TLB");
console.log(TLB.getMemoryState());
});
return eventList;
}
const programTest = [
["s1", "s2", "s3", "s4", "s5"],
["s1", "s2", "s3"],
["s1", "s2"],
["s1", "s2", "s3"],
["s1", "s2", "s3", "s4"],
["s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8"],
["s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9"],
["s1", "s2", "s3"],
];
const executionList = [
"P6-0",
"P6-1",
"P0-1",
"P0-0",
"P7-14",
"P2-0",
"P0-0",
"P2-0",
"P2-0",
"P2-0",
"P6-0",
"P7-0",
"P0-0",
"P0-0",
"P4-0",
"P2-0",
"P4-0",
"P2-0",
"P7-0",
"P5-0",
"P6-0",
];
//console.log(FIFO(programTest, executionList, 5, 20, 5, 3, 3));
FIFO(programTest, executionList, 5, 20, 5, 3, 3);