-
Notifications
You must be signed in to change notification settings - Fork 3
/
PriorityPreemptive.html
380 lines (344 loc) · 14.7 KB
/
PriorityPreemptive.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
<html>
<head>
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="menu.css" />
<link rel="stylesheet" href="util.css" />
<title>Priority Preemptive</title>
<script src="js/jquery-3.1.0.min.js"></script>
<script>
var ready_queue = [];
var cpu_process = null;
var cpu_bursttime = null;
var cpu_priority = null;
var GLOBAL_startTime = null;
var GLOBAL_endTime = null;
var GLOBAL_bubbleStart = null;
var GLOBAL_bubbleEnd = null;
var my_console = $('#cust_console');
var my_gantt_chart = $('#gantt_chart');
var my_colors = [
'#fa2d3b',
'#5d62f5',
'#48b08f',
'#611a12'
];
var pr_done = 0;
var num = 4
function loadValues(){
$('input').each(function(){
$(this).val(Math.floor(Math.random() * 15) + 1);
});
$('#INIT_COMPUTE').click(function(){
if(checkValues()){
var i = GET_ARRIVALTIME_LOWEST();
do{
PROCESS_ARRIVALS(i);
if(cpu_process != null){
cpu_bursttime--;
console.log(i+'\t: DCRMNT BT : P'+cpu_process+'/'+cpu_bursttime);
if(cpu_bursttime == 0){
GLOBAL_endTime = i;
var bt_of_process = parseFloat(GLOBAL_endTime - GLOBAL_startTime);
var curr_width = ((bt_of_process / GET_BURSTTIME_SUM()) * 80);
$('#gantt_chart').append('<div data-process="'+cpu_process+'" data-start="'+GLOBAL_startTime+'" data-end="'+GLOBAL_endTime+'" class="gantt_block" style="background-color: '+my_colors[(cpu_process-1)%4]+'; width: '+curr_width+'%;">P'+cpu_process+'<br/>'+GLOBAL_startTime+' - '+GLOBAL_endTime+'</div>');
console.log(i+'\t: '+' ADD_GANTT_1 = '+cpu_process+'/'+cpu_bursttime);
cpu_process = null;
cpu_bursttime = null;
pr_done++;
}
}
if(cpu_process == null){
if(ready_queue.length > 0){SORT_READY_QUEUE();
cpu_process = ready_queue[0].split('?')[0];
cpu_bursttime = ready_queue[0].split('?')[1];
cpu_priority = parseFloat(ready_queue[0].split('?')[2]);
ready_queue.shift();
GLOBAL_startTime = i;
if(GLOBAL_bubbleStart != null){
console.log(i+'\t: BUBBLE END');
var bubble_width = ((i - GLOBAL_bubbleStart) / GET_BURSTTIME_SUM()) * 80;
$('#gantt_chart').append('<div class="bubble" style="background-color: white; width: '+bubble_width+'%; color: black;">BUBBLE<br/>'+GLOBAL_bubbleStart+' - '+i+'</div>');
GLOBAL_bubbleStart = null;
}
console.log(i+'\t: ADD PR to CPU : P'+cpu_process+'/'+cpu_bursttime);
}else{
if(GLOBAL_bubbleStart == null){
GLOBAL_bubbleStart = i;
}
console.log(i+'\t: BUBBLE INC - Start = '+GLOBAL_bubbleStart);
}
}else{
if(ready_queue.length > 0){
SORT_READY_QUEUE();
var og_temp_pr = ready_queue[0].split('?')[0];
var og_temp_bt = ready_queue[0].split('?')[1];
var og_temp_prio = parseFloat(ready_queue[0].split('?')[2]);
console.log(i+'\t:'+og_temp_prio+' < '+cpu_priority+' = '+(og_temp_prio <= cpu_priority));
if(og_temp_prio < cpu_priority){
GLOBAL_endTime = i; // change end time to loop value `i`
var bt_of_process = parseFloat(GLOBAL_endTime - GLOBAL_startTime);
var curr_width = ((bt_of_process / GET_BURSTTIME_SUM()) * 80);
$('#gantt_chart').append('<div data-process="'+cpu_process+'" data-start="'+GLOBAL_startTime+'" data-end="'+GLOBAL_endTime+'" class="gantt_block" style="background-color: '+my_colors[(cpu_process-1)]+'; width: '+curr_width+'%;">P'+cpu_process+'<br/>'+GLOBAL_startTime+' - '+GLOBAL_endTime+'</div>');
console.log(i+'\t: '+' ADD_GANTT_2 = '+cpu_process+'/'+cpu_bursttime);
console.log(i+'\t: CHNG PR : P'+cpu_process+'/'+cpu_bursttime+' to '+og_temp_pr+'/'+og_temp_bt);
ready_queue.push(cpu_process+'?'+cpu_bursttime+'?'+cpu_priority);
cpu_process = og_temp_pr;
cpu_bursttime = og_temp_bt;
cpu_priority = og_temp_prio;
ready_queue.shift();
SORT_READY_QUEUE();
GLOBAL_startTime = i;
}
}
}
i++;
}while(pr_done < num);
var et_array_p = [];
var et_array_e = [];
$('.gantt_block').each(function (index) {
var tmp_process = parseFloat($(this).data('process'));
var tmp_start = parseFloat($(this).data('start'));
var tmp_end = parseFloat($(this).data('end'));
var tmp_arrival = parseFloat($('[data-process="'+(tmp_process)+'"][class="arrival_time"]').val());
var slctr_tat = $('#P'+tmp_process+'_TAT');
var slctr_wt = $('#P'+tmp_process+'_WT');
var inArray = $.inArray(tmp_process, et_array_p);
slctr_tat.empty().append(tmp_end - tmp_arrival);
var curr_wt = slctr_wt.text();
if(inArray > -1){
slctr_wt.empty().append(parseFloat(curr_wt) + (tmp_start - et_array_e[inArray]));
et_array_e[inArray] = tmp_end;
}else{
slctr_wt.append(tmp_start - tmp_arrival);
et_array_p.push(tmp_process);
et_array_e.push(tmp_end);
}
});
var total_tat = 0;
$('.TAT').each(function (index) {
total_tat += parseFloat($(this).text());
});
$('#AVG_TAT').empty().append((parseFloat(total_tat)/$('.TAT').length));
var total_wt = 0;
$('.WT').each(function (index) {
total_wt += parseFloat($(this).text());
});
$('#AVG_WT').empty().append((parseFloat(total_wt)/$('.WT').length));
}
});
$('#methods').change(function(){
location.href = $(this).val();
})
};
$(document).ready(loadValues);
function checkValues(){
var flag = true;
$('#cust_console').empty();
$('.arrival_time').each(function(index){
if($(this).val() == '' || !$.isNumeric($(this).val())){
$('#cust_console').append('Please input a number for Arrival Time for Process P'+(index+1)+'<br/>');
flag = false;
}
})
$('.burst_time').each(function(index){
if($(this).val() == '' || !$.isNumeric($(this).val())){
$('#cust_console').append('Please input a number for Burst Time for Process P'+(index+1)+'<br/>');
flag = false;
}
})
$('.priority').each(function(index){
if($(this).val() == '' || !$.isNumeric($(this).val())){
$('#cust_console').append('Please input a number for Priority for Process P'+(index+1)+'<br/>');
flag = false;
}
})
return flag;
}
function GET_BT_OF_PROCESS(cpu){
return parseFloat(Math.round($('[data-process="'+(cpu)+'"][class="burst_time"]').val()));
}
function GET_ARRIVALTIME_LOWEST(){
var lowest = GET_ARRIVALTIME_HIGHEST();
$('.arrival_time').each(function(){
if(parseFloat($(this).val()) < lowest){
lowest = parseFloat($(this).val());
}
});
return lowest;
}
function GET_PROCESSTIME(){
}
function GET_ARRIVALTIME_HIGHEST(){
var highest = 0;
$('.arrival_time').each(function(){
if(highest == 0){
highest = parseFloat($(this).val());
}
if(parseFloat($(this).val()) > highest){
highest = parseFloat($(this).val());
}
});
return parseFloat(highest);
}
function GET_BURSTTIME_SUM(){
var total = 0.0;
$('.burst_time').each(function(index){
total += parseFloat($(this).val());
});
return (total + GET_ARRIVALTIME_LOWEST());
}
function GET_PR_WITH_HIGHEST_AT_AND_BT(){
var procAndBT = null;
$('.arrival_time').each(function(index){
var curr_arrival_time = Math.round(parseFloat($(this).val()));
var highest = 0;
if(curr_arrival_time > highest){
highest = curr_arrival_time;
procAndBT = [$(this).data('process'), parseFloat(highest)];
}
});
return procAndBT;
}
function PROCESS_ARRIVALS(time){
var arrival_flag = false;
$('.arrival_time').each(function(index){
var curr_arrival_time = Math.round(parseFloat($(this).val()));
if(curr_arrival_time == parseFloat(time)){
var process_number = index+1;
var curr_bursttime = parseFloat($('[data-process="'+(process_number)+'"][class="burst_time"]').val());
var curr_prio = parseFloat($('[data-process="'+(process_number)+'"][class="priority"]').val());
ready_queue.push(process_number+'?'+curr_bursttime+'?'+curr_prio);
console.log(time+'\t: PR ARRVD : '+process_number+'/'+curr_bursttime+' | '+ready_queue);
SORT_READY_QUEUE();
arrival_flag = true;
}
});
return arrival_flag;
}
function GET_BURSTTIME_TOTAL(){
var total = 0.0;
$('.burst_time').each(function(index){
total += parseFloat($(this).val());
});
if(GET_ARRIVALTIME_HIGHEST() > total){
total = GET_ARRIVALTIME_HIGHEST();
}
return parseFloat(total);
}
function SORT_READY_QUEUE(){
ready_queue.sort(function(a,b){ // sort queue by lowest bt first
return a.split('?')[2] - b.split('?')[2]
});
}
function addRow()
{
var lastRow = $('#table tr:last');
var table = document.getElementById('table')
let row = '<tr><td>P'
+ (num + 1)
+ '</td><td><input data-process='
+ (num+1)
+ ' type="text" class="arrival_time" /></td><td><input data-process='
+ (num+1)
+ ' type="text" class="burst_time" /></td><td><input data-process='
+ (num+1)
+ ' type="text" class="priority" /></td><td><span class="TAT" id="P'
+ (num+1)
+ '_TAT"></span></td><td><span class="WT" id="P'
+ (num+1)
+ '_WT"></span></td></tr>';
lastRow.before(row)
num+=1
loadValues()
}
</script>
</head>
<body>
<div id="preloader">
<div id="status"> </div>
</div>
<script>
$(window).on('load', function() {
$('#status').fadeOut();
$('#preloader').delay(350).fadeOut('slow');
$('body').delay(350).css({'overflow':'visible'});
})
</script>
<header id="header">
<nav class="links" style="--items: 5;">
<a href="FirstComeFirstServe.html">First Come, First Serve (FCFS)</a>
<a href="ShortestJobFirst.html">Shortest Job First</a>
<a href="RoundRobin.html">Round Robin</a>
<a href="PriorityNonPreemptive.html">Priority Non-Preemptive</a>
<a href="PriorityPreemptive.html">Priority Preemptive</a>
<span class="line"></span>
</nav>
</header>
<br><br><br><br>
<h2>Priority Preemptive</h2>
<br><br>
<center>
<table border="1" id="table" class="table">
<thead>
<tr>
<td>Process Name</td>
<td>Arrival Time</td>
<td>Burst Time</td>
<td>Priority</td>
<td>Turn-Around Time</td>
<td>Waiting Time</td>
</tr>
</thead>
<tbody>
<tr>
<td>P1</td>
<td><input data-process="1" type="text" class="arrival_time" /></td>
<td><input data-process="1" type="text" class="burst_time" /></td>
<td><input data-process="1" type="text" class="priority" /></td>
<td><span class="TAT" id="P1_TAT"></span></td>
<td><span class="WT" id="P1_WT"></span></td>
</tr>
<tr>
<td>P2</td>
<td><input data-process="2" type="text" class="arrival_time" /></td>
<td><input data-process="2" type="text" class="burst_time" /></td>
<td><input data-process="2" type="text" class="priority" /></td>
<td><span class="TAT" id="P2_TAT"></span></td>
<td><span class="WT" id="P2_WT"></span></td>
</tr>
<tr>
<td>P3</td>
<td><input data-process="3" type="text" class="arrival_time" /></td>
<td><input data-process="3" type="text" class="burst_time" /></td>
<td><input data-process="3" type="text" class="priority" /></td>
<td><span class="TAT" id="P3_TAT"></span></td>
<td><span class="WT" id="P3_WT"></span></td>
</tr>
<tr>
<td>P4</td>
<td><input data-process="4" type="text" class="arrival_time" /></td>
<td><input data-process="4" type="text" class="burst_time" /></td>
<td><input data-process="4" type="text" class="priority" /></td>
<td><span class="TAT" id="P4_TAT"></span></td>
<td><span class="WT" id="P4_WT"></span></td>
</tr>
<tr>
<td colspan="4">Average</td>
<td><span id="AVG_TAT"></span></td>
<td><span id="AVG_WT"></span></td>
</tr>
</tbody>
</table>
<br/><br/>
<button type="button" class="addBtn" onclick="addRow();">Add Row</button>
<br/><br/>
<div style="width: 90%">
<div id="gantt_chart">
</div>
</div>
<div style="clear: both;"></div>
<p id="cust_console" style="color:red;"></p>
<button id="INIT_COMPUTE">Compute</button>
</center>
</body>
</html>