-
Notifications
You must be signed in to change notification settings - Fork 3
/
PriorityNonPreemptive.html
289 lines (267 loc) · 12.3 KB
/
PriorityNonPreemptive.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
<html>
<head>
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="menu.css" />
<link rel="stylesheet" href="util.css" />
<title>Priority Non-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 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 i = null;
var pr_done = 0;
var num = 4
function loadValues(){
$('#methods').change(function(){
location.href = $(this).val();
});
$('input').each(function(){
$(this).val(Math.floor(Math.random() * 10) + 1);
});
$('#INIT_COMPUTE').click(function () {
if(checkValues()){
i = GET_ARRIVALTIME_LOWEST();
do{
PROCESS_ARRIVALS(i);
if(cpu_process != null){
if(i == GLOBAL_endTime){
var curr_width = (((GLOBAL_endTime - GLOBAL_startTime) / 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>');
pr_done++;
var curr_arrivaltime = parseFloat($('[data-process="'+cpu_process+'"][class="arrival_time"]').val());
$('#P'+cpu_process+'_TAT').append(GLOBAL_endTime - curr_arrivaltime);
$('#P'+cpu_process+'_WT').append(GLOBAL_startTime - curr_arrivaltime);
cpu_process = null;
cpu_bursttime = null;
}
}
if(cpu_process == null){
if(ready_queue.length > 0){
if(GLOBAL_bubbleStart != null){
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;
}
cpu_process = ready_queue[0].split('?')[0];
cpu_bursttime = parseFloat(ready_queue[0].split('?')[1]);
ready_queue.shift();
GLOBAL_startTime = i;
GLOBAL_endTime = GLOBAL_startTime + cpu_bursttime;
}else{
if(GLOBAL_bubbleStart == null){
GLOBAL_bubbleStart = i;
}
}
}
i++;
}while(pr_done < 4);
var total_TAT = 0;
$('.TAT').each(function (index) {
total_TAT += parseFloat($(this).text());
});
$('#AVG_TAT').empty().append((total_TAT/4));
var total_WT = 0;
$('.WT').each(function (index) {
total_WT += parseFloat($(this).text());
});
$('#AVG_WT').empty().append((total_WT/4));
}
})
};
$(document).ready(loadValues);
function GET_BURSTTIME_SUM(){
var total = 0.0;
$('.burst_time').each(function(index){
total += parseFloat($(this).val());
});
return (total + GET_ARRIVALTIME_LOWEST());
}
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_ARRIVALTIME_LOWEST(){
var lowest = GET_ARRIVALTIME_HIGHEST();
$('.arrival_time').each(function(){
if(parseFloat($(this).val()) < lowest){
lowest = parseFloat($(this).val());
}
});
return lowest;
}
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 checkValues(){
var flag = true;
$('.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 SORT_READY_QUEUE(){
ready_queue.sort(function(a,b){ // sort queue by lowest priority 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 Non-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>