-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
245 lines (224 loc) · 8.44 KB
/
index.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
var chart, total, max, min, queue, processes, quant = 2;
//Process class to store data of a process
class Process{
constructor(idx, arrival, burst){
this.id = idx;
this.arrivalTime = arrival;
this.burstTime = burst;
this.consumedTime = 0;
}
}
//Process Queue to store all the processes
//that are available at a given time for scheduling
class Queue{
constructor(){
this.items = [];
}
isEmpty(){
return this.items.length == 0;
}
enqueue(element){
this.items.push(element);
}
dequeue(){
if(this.isEmpty())
return -1;
return this.items.shift();
}
front(){
if(this.isEmpty())
return -1;
return this.items[0];
}
}
//Random Processes generator and Chart Initializer
function generateProcesses() {
document.getElementById("timer").innerHTML = "Current Time - " + 0 + " seconds";
[min, max] = [15, 20];
total = Math.floor(Math.random() * (max - min + 1)) + min;
chart = new CanvasJS.Chart("chartContainer", {
axisX:{
title: "---Arrival Time-->"
},
axisY:{
title: "---Burst Time-->",
minimum: 0,
maximum: 20,
interval: 1
},
data: [
{
type: "stackedColumn",
color: "blue",
legendText: "ToBeProcessed",
showInLegend: "true",
dataPoints: []
},
{
type: "stackedColumn",
color: "#33ff00",
legendText: "Processed",
showInLegend: "true",
dataPoints: []
}
]
});
chart.render();
[min, processes] = [1, []];
for(var i=0; i<total; i++){
var label_ = String.fromCharCode(65+i);
var burst = Math.floor(Math.random() * (20 - 5 + 1)) + 5;
var arrival = Math.floor(Math.random() * 3) + min - 1;
var process = new Process(i, arrival, burst);
min = arrival+1;
processes.push(process);
chart.data[1].dataPoints.push({label: label_+"(" + arrival + ")", y: 0});
chart.data[0].dataPoints.push({label: label_+"(" + arrival + ")", y: burst});
}
chart.render();
}
//A helper function for FCFS and round-robin alortihm
function fcfsHelper(){
var front = queue.front();
if(front.burstTime > front.consumedTime){
wait = Math.ceil(1000/quant);
front.consumedTime += quant;
front.consumedTime = (front.consumedTime > front.burstTime ? front.burstTime :front.consumedTime);
chart.data[0].dataPoints[front.id].y = front.burstTime-front.consumedTime;
chart.data[1].dataPoints[front.id].y = front.consumedTime;
chart.render();
}
queue.front().consumedTime = front.consumedTime;
return queue.front();
}
//Implementation of First Come First Serve Algortihm
function firstComeFirstServe(){
quant = 2;
queue = new Queue();
queue.enqueue(processes[0]);
var [i, time] = [1, processes[0].arrivalTime];
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
(function traverse() {
if(!queue.isEmpty()){
var wait = Math.ceil(1000/quant);
setTimeout(function() {
var front = queue.front();
time += Math.min(front.burstTime-front.consumedTime, quant);
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
while(i<processes.length && processes[i].arrivalTime<=time)
queue.enqueue(processes[i++]);
front = fcfsHelper();
if(front.burstTime == front.consumedTime){
wait = 0;
queue.dequeue();
}
traverse();
}, wait);
}
})();
}
//Implementation of Shortest Job First Algortihm
function shortestJobFirst(){
quant = 2;
var time = 0;
var processed = new Set();
queue = new Queue;
while(processed.size<processes.length){
var i = 0;
var min = -1;
while(i<processes.length && processes[i].arrivalTime<=time){
if((min==-1 || min.burstTime>processes[i].burstTime) && !processed.has(processes[i].id))
min = processes[i];
i++;
}
if(min==-1)
time+=quant;
else{
time = Math.max(time, min.arrivalTime) + min.burstTime;
processed.add(min.id);
queue.enqueue(min);
}
}
time = queue.front().arrivalTime;
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
(function traverse() {
if(!queue.isEmpty()){
var wait = Math.ceil(1000/quant);
setTimeout(function(){
time += Math.min(queue.front().burstTime-queue.front().consumedTime, quant);
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
var front = fcfsHelper();
if(front.burstTime == front.consumedTime){
wait = 0;
queue.dequeue();
}
traverse();
}, wait);
}
})();
}
//Implementation of Shortest Remaining Time First Algortihm
function shortestRemainingTimeFirst(){
var [ctr, time] = [0, 0];
quant = 1;
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
(function traverse() {
if(ctr<processes.length){
var wait = Math.ceil(250/quant);
setTimeout(function() {
var i = 0;
var min = -1;
while(i<processes.length && processes[i].arrivalTime<=time){
if((min==-1 || (min.burstTime-min.consumedTime)>(processes[i].burstTime-processes[i].consumedTime)) && processes[i].burstTime>processes[i].consumedTime)
min = processes[i];
i++;
}
if(min!=-1){
processes[min.id].consumedTime += quant;
processes[min.id].consumedTime = (processes[min.id].consumedTime > processes[min.id].burstTime ? processes[min.id].burstTime : processes[min.id].consumedTime);
chart.data[0].dataPoints[min.id].y = processes[min.id].burstTime-processes[min.id].consumedTime;
chart.data[1].dataPoints[min.id].y = processes[min.id].consumedTime;
chart.render();
if(processes[min.id].burstTime == processes[min.id].consumedTime)
ctr++;
}
time += quant;
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
traverse();
}, wait);
}
})();
}
//Implementation of Round Robin Algortihm
function roundRobin(){
[quant, queue] = [2, new Queue()];
queue.enqueue(processes[0]);
var [i, time] = [1, processes[0].arrivalTime];
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
(function traverse() {
if(!queue.isEmpty()){
var wait = Math.ceil(1000/quant);
setTimeout(function() {
var front = queue.front();
time += Math.min(front.burstTime-front.consumedTime, quant);
document.getElementById("timer").innerHTML = "Current Time - " + time + " seconds";
while(i<processes.length && processes[i].arrivalTime<=time)
queue.enqueue(processes[i++]);
fcfsHelper();
front = queue.dequeue();
if(front.burstTime > front.consumedTime){
wait = 0;
queue.enqueue(front);
}
traverse();
}, wait);
}
})();
}
//Calling of above Functions
window.onload = generateProcesses();
document.getElementById("regenerate").onclick = function() {generateProcesses();};
document.getElementById("fcfs").onclick = function() {firstComeFirstServe();};
document.getElementById("srjf").onclick = function() {shortestJobFirst();};
document.getElementById("srtf").onclick = function() {shortestRemainingTimeFirst();};
document.getElementById("round-robin").onclick = function() {roundRobin();};