forked from avirajdongare/BooleanAutocrats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_scheduling.html
454 lines (397 loc) · 17.7 KB
/
cpu_scheduling.html
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
fresh th {
background: linear-gradient(WhiteSmoke, lightgray);
text-align: left;
border: 1px solid black;
height: 400 px;
}
fresh td {
border: 1px solid black;
border-bottom: 0;
/*border-right: 0;*/
height: 20px;
text-align: left;
}
div th, div td {
border: 1px solid black;
text-align: center;
}
table {
display: inline;
border-collapse: collapse;
}
input {
text-align: center;
}
#curtain {
background-color: white;
}
</style>
</head>
<body>
<div>
<div>
Algorithm:
<form id="algorithm">
<input type="radio" name="algorithm" value="fcfs" checked /> FCFS<br />
<input type="radio" name="algorithm" value="sjf" /> SJF<br />
<input type="radio" name="algorithm" value="priority" /> Priority scheduling<br />
<input type="radio" name="algorithm" value="robin" /> Round Robin
</form>
<br>
</div>
<table id="inputTable">
<tr>
<th>Process</th>
<th>Arrival Time</th>
<th>Execute Time</th>
<th class="servtime">Service Time</th>
<th class="priority-only">Priority</th>
</tr>
<tr>
<td>P0</td>
<td>0</td>
<td><input class="initial exectime" type="text" value="5" /></td>
<td class="servtime"></td>
<td class="priority-only initial"><input type="text" /></td>
</tr>
<tr>
<td>P1</td>
<td>1</td>
<td><input class="initial exectime" value="3" /></td>
<td class="servtime"></td>
<td class="priority-only"><input type="text" /></td>
</tr>
</table>
<input id="minus" style="display: inline; left: 428px; position: absolute; top: 170px;" type="button" value="-"
onclick="deleteRow();" />
</div>
<input type="button" value="+" onclick="addRow();" />
<div>
</div>
<p id="quantumParagraph" hidden>Quantum:
<input style="width: 50px;" id="quantum" type="text" value="3" />
</p>
<input type="button" value="Go" onclick="draw();" />
<br /><br />
<fresh>
</fresh>
<p>
Timer: <strong id="timer"></strong> sec
</p>
<script>
recalculateServiceTime();
$('.priority-only').hide();
$(document).ready(function () {
$('input[type=radio][name=algorithm]').change(function () {
if (this.value == 'priority') {
$('.priority-only').show();
$('.servtime').show();
$('#minus').css('left', '604px');
}
else {
$('.priority-only').hide();
$('.servtime').show();
$('#minus').css('left', '428px');
}
if (this.value == 'robin') {
$('.servtime').hide();
$('#quantumParagraph').show();
}
else {
$('#quantumParagraph').hide();
$('.servtime').show();
}
recalculateServiceTime();
});
});
function addRow() {
var lastRow = $('#inputTable tr:last');
var lastRowNumebr = parseInt(lastRow.children()[1].innerText);
var newRow = '<tr><td>P'
+ (lastRowNumebr + 1)
+ '</td><td>'
+ (lastRowNumebr + 1)
+ '</td><td><input class="exectime" type="text"/></td><td class="servtime"></td>'
//if ($('input[name=algorithm]:checked', '#algorithm').val() == "priority")
+ '<td class="priority-only"><input type="text"/></td></tr>';
lastRow.after(newRow);
var minus = $('#minus');
minus.show();
minus.css('top', (parseFloat(minus.css('top')) + 24) + 'px');
if ($('input[name=algorithm]:checked', '#algorithm').val() != "priority")
$('.priority-only').hide();
$('#inputTable tr:last input').change(function () {
recalculateServiceTime();
});
}
function deleteRow() {
var lastRow = $('#inputTable tr:last');
lastRow.remove();
var minus = $('#minus');
minus.css('top', (parseFloat(minus.css('top')) - 24) + 'px');
if (parseFloat(minus.css('top')) < 150)
minus.hide();
}
$(".initial").change(function () {
recalculateServiceTime();
});
function recalculateServiceTime() {
var inputTable = $('#inputTable tr');
var totalExectuteTime = 0;
var algorithm = $('input[name=algorithm]:checked', '#algorithm').val();
if (algorithm == "fcfs") {
$.each(inputTable, function (key, value) {
if (key == 0) return true;
$(value.children[3]).text(totalExectuteTime);
var executeTime = parseInt($(value.children[2]).children().first().val());
totalExectuteTime += executeTime;
});
}
else if (algorithm == "sjf") {
var exectuteTimes = [];
$.each(inputTable, function (key, value) {
if (key == 0) return true;
exectuteTimes[key - 1] = parseInt($(value.children[2]).children().first().val());
});
var currentIndex = -1;
for (var i = 0; i < exectuteTimes.length; i++) {
currentIndex = findNextIndex(currentIndex, exectuteTimes);
if (currentIndex == -1) return;
$(inputTable[currentIndex + 1].children[3]).text(totalExectuteTime);
totalExectuteTime += exectuteTimes[currentIndex];
}
}
else if (algorithm == "priority") {
var exectuteTimes = [];
var priorities = [];
$.each(inputTable, function (key, value) {
if (key == 0) return true;
exectuteTimes[key - 1] = parseInt($(value.children[2]).children().first().val());
priorities[key - 1] = parseInt($(value.children[4]).children().first().val());
});
var currentIndex = -1;
for (var i = 0; i < exectuteTimes.length; i++) {
currentIndex = findNextIndexWithPriority(currentIndex, priorities);
if (currentIndex == -1) return;
$(inputTable[currentIndex + 1].children[3]).text(totalExectuteTime);
totalExectuteTime += exectuteTimes[currentIndex];
}
}
else if (algorithm == "robin") {
$('#minus').css('left', '335px');
$.each(inputTable, function (key, value) {
if (key == 0) return true;
$(value.children[3]).text("");
});
}
}
function findNextIndexWithPriority(currentIndex, priorities) {
var currentPriority = 1000000;
if (currentIndex != -1) currentPriority = priorities[currentIndex];
var resultPriority = 0;
var resultIndex = -1;
var samePriority = false;
var areWeThereYet = false;
$.each(priorities, function (key, value) {
var changeInThisIteration = false;
if (key == currentIndex) {
areWeThereYet = true;
return true;
}
if (value <= currentPriority && value >= resultPriority) {
if (value == resultPriority) {
if (currentPriority == value && !samePriority) {
samePriority = true;
changeInThisIteration = true;
resultPriority = value;
resultIndex = key;
}
}
else if (value == currentPriority) {
if (areWeThereYet) {
samePriority = true;
areWeThereYet = false;
changeInThisIteration = true;
resultPriority = value;
resultIndex = key;
}
}
else {
resultPriority = value;
resultIndex = key;
}
if (value > resultPriority && !changeInThisIteration)
samePriority = false;
}
});
return resultIndex;
}
function findNextIndex(currentIndex, array) {
var currentTime = 0;
if (currentIndex != -1) currentTime = array[currentIndex];
var resultTime = 1000000;
var resultIndex = -1;
var sameTime = false;
var areWeThereYet = false;
$.each(array, function (key, value) {
var changeInThisIteration = false;
if (key == currentIndex) {
areWeThereYet = true;
return true;
}
if (value >= currentTime && value <= resultTime) {
if (value == resultTime) {
if (currentTime == value && !sameTime) {
sameTime = true;
changeInThisIteration = true;
resultTime = value;
resultIndex = key;
}
}
else if (value == currentTime) {
if (areWeThereYet) {
sameTime = true;
areWeThereYet = false;
changeInThisIteration = true;
resultTime = value;
resultIndex = key;
}
}
else {
resultTime = value;
resultIndex = key;
}
if (value < resultTime && !changeInThisIteration)
sameTime = false;
}
});
return resultIndex;
}
function animate() {
$('fresh').prepend('<div id="curtain" style="position: absolute; right: 0; width:100%; height:100px;"></div>');
$('#curtain').width($('#resultTable').width());
$('#curtain').css({ left: $('#resultTable').position().left });
var sum = 0;
$('.exectime').each(function () {
sum += Number($(this).val());
});
console.log($('#resultTable').width());
var distance = $("#curtain").css("width");
animationStep(sum, 0);
jQuery('#curtain').animate({ width: '0', marginLeft: distance }, sum * 1000 / 2, 'linear');
}
function animationStep(steps, cur) {
$('#timer').html(cur);
if (cur < steps) {
setTimeout(function () {
animationStep(steps, cur + 1);
}, 500);
}
else {
}
}
function draw() {
$('fresh').html('');
var inputTable = $('#inputTable tr');
var th = '';
var td = '';
var algorithm = $('input[name=algorithm]:checked', '#algorithm').val();
if (algorithm == "fcfs") {
$.each(inputTable, function (key, value) {
if (key == 0) return true;
var executeTime = parseInt($(value.children[2]).children().first().val());
th += '<th style="height: 60px; width: ' + executeTime * 20 + 'px;">P' + (key - 1) + '</th>';
td += '<td>' + executeTime + '</td>';
});
$('fresh').html('<table id="resultTable"><tr>'
+ th
+ '</tr><tr>'
+ td
+ '</tr></table>'
);
}
else if (algorithm == "sjf") {
var executeTimes = [];
$.each(inputTable, function (key, value) {
if (key == 0) return true;
var executeTime = parseInt($(value.children[2]).children().first().val());
executeTimes[key - 1] = { "executeTime": executeTime, "P": key - 1 };
});
executeTimes.sort(function (a, b) {
if (a.executeTime == b.executeTime)
return a.P - b.P;
return a.executeTime - b.executeTime
});
$.each(executeTimes, function (key, value) {
th += '<th style="height: 60px; width: ' + value.executeTime * 20 + 'px;">P' + value.P + '</th>';
td += '<td>' + value.executeTime + '</td>';
});
$('fresh').html('<table id="resultTable"><tr>'
+ th
+ '</tr><tr>'
+ td
+ '</tr></table>'
);
}
else if (algorithm == "priority") {
var executeTimes = [];
$.each(inputTable, function (key, value) {
if (key == 0) return true;
var executeTime = parseInt($(value.children[2]).children().first().val());
var priority = parseInt($(value.children[4]).children().first().val());
executeTimes[key - 1] = { "executeTime": executeTime, "P": key - 1, "priority": priority };
});
executeTimes.sort(function (a, b) {
if (a.priority == b.priority)
return a.P - b.P;
return b.priority - a.priority
});
$.each(executeTimes, function (key, value) {
th += '<th style="height: 60px; width: ' + value.executeTime * 20 + 'px;">P' + value.P + '</th>';
td += '<td>' + value.executeTime + '</td>';
});
$('fresh').html('<table id="resultTable" style="width: 70%"><tr>'
+ th
+ '</tr><tr>'
+ td
+ '</tr></table>'
);
}
else if (algorithm == "robin") {
var quantum = $('#quantum').val();
var executeTimes = [];
$.each(inputTable, function (key, value) {
if (key == 0) return true;
var executeTime = parseInt($(value.children[2]).children().first().val());
executeTimes[key - 1] = { "executeTime": executeTime, "P": key - 1 };
});
var areWeThereYet = false;
while (!areWeThereYet) {
areWeThereYet = true;
$.each(executeTimes, function (key, value) {
if (value.executeTime > 0) {
th += '<th style="height: 60px; width: ' + (value.executeTime > quantum ? quantum : value.executeTime) * 20 + 'px;">P' + value.P + '</th>';
td += '<td>' + (value.executeTime > quantum ? quantum : value.executeTime) + '</td>';
value.executeTime -= quantum;
areWeThereYet = false;
}
});
}
$('fresh').html('<table id="resultTable" style="width: 70%"><tr>'
+ th
+ '</tr><tr>'
+ td
+ '</tr></table>'
);
}
animate();
}
</script>