-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShortestJobFirst.html
273 lines (249 loc) · 10 KB
/
ShortestJobFirst.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
<html>
<head>
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="menu.css" />
<link rel="stylesheet" href="util.css" />
<title>Shortest Job First</title>
<script src="js/jquery-3.1.0.min.js"></script>
<script>
var ready_queue = [];
var FIRST_PROCESS = [];
var GLOBAL_startTime = null;
var GLOBAL_endTime = null;
var my_console = $('#cust_console');
var my_gantt_chart = $('#gantt_chart');
var my_colors = [
'#fa2d3b',
'#5d62f5',
'#48b08f',
'#611a12'
];
var num = 4
function loadValues(){
$('input').each(function(){
$(this).val(Math.floor(Math.random() * 10) + 1);
});
$('#INIT_COMPUTE').click(function(){
if(checkValues()){
my_gantt_chart.empty();
for(var i=0; i <= GET_BURSTTIME_TOTAL(); i++){
$('.arrival_time').each(function(index){
var curr_arrival_time = Math.round(parseFloat($(this).val()));
if(curr_arrival_time == parseFloat(i)){
var process_number = index+1;
var curr_bursttime = parseFloat($('[data-process="'+(process_number)+'"][class="burst_time"]').val());
if(FIRST_PROCESS.length == 0){
FIRST_PROCESS.push(process_number+'?'+curr_bursttime);
}else{
ready_queue.push(process_number+'?'+curr_bursttime);
}
}
});
}
ready_queue.sort(function(a,b){
return a.split('?')[1] - b.split('?')[1]
});
ready_queue = FIRST_PROCESS.concat(ready_queue);
var loop_length = parseFloat(ready_queue.length);
my_gantt_chart.empty();
for(var o=0; o < loop_length; o++){
var tmp_process = ready_queue[0].split('?')[0];
var tmp_burst = parseFloat(ready_queue[0].split('?')[1]);
ready_queue.shift();
var tmp_at = parseFloat($('[class="arrival_time"][data-process="'+tmp_process+'"]').val());
if(GLOBAL_startTime == null){
GLOBAL_startTime = tmp_at;
}
if(GLOBAL_startTime < tmp_at){
$('#gantt_chart').append('<div class="gantt_block bubble" style="background-color: white; width: 10%; color: black;">BUBBLE<br/>'+GLOBAL_startTime+' - '+tmp_at+'</div>');
GLOBAL_startTime = tmp_at;
}
GLOBAL_endTime = GLOBAL_startTime + tmp_burst;
var wt = GLOBAL_startTime - tmp_at;
var tat = GLOBAL_endTime - tmp_at;
$('#P'+tmp_process+'_WT').empty().append(wt);
$('#P'+tmp_process+'_TAT').empty().append(tat);
var curr_width = ((tmp_burst / GET_BURSTTIME_TOTAL()) * 80);
$('#gantt_chart').append('<div class="gantt_block" style="background-color: '+my_colors[o%4]+'; width: '+curr_width+'%;">P'+tmp_process+'<br/>'+GLOBAL_startTime+' - '+GLOBAL_endTime+'</div>');
GLOBAL_startTime = GLOBAL_endTime;
}
var total_tat = 0;
$('.TAT').each(function(index, value){
total_tat += parseFloat($(this).text());
});
$('#AVG_TAT').empty().append((parseFloat(total_tat)/parseFloat(loop_length)));
var total_wt = 0;
$('.WT').each(function(index, value){
total_wt += parseFloat($(this).text());
});
$('#AVG_WT').empty().append((parseFloat(total_wt)/parseFloat(loop_length)));
}
});
$('#methods').change(function(){
location.href = $(this).val();
})
};
$(document).ready(loadValues);
function GET_BURSTTIME_HIGHEST(){
var x = 0;
$('.burst_time').each(function(index){
var bt = parseFloat($(this).val());
if(bt > 0){
x = bt;
}
});
return parseFloat(x);
}
function GET_BURSTTIME_TOTAL(){
var total = 0.0;
$('.burst_time').each(function(index){
total += parseFloat($(this).val());
});
if(parseFloat(total) < GET_ARRIVALTIME_HIGHEST()){
total = GET_ARRIVALTIME_HIGHEST();
}
return parseFloat(total);
}
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;
$('#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){
// check if burst_time is filled out
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){
// check if burst_time is filled out
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 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><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>Shortest Job First</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>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><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><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><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><span class="TAT" id="P4_TAT"></span></td>
<td><span class="WT" id="P4_WT"></span></td>
</tr>
<tr>
<td colspan="3">Average</td>
<td><span id="AVG_TAT"></span></td>
<td><span id="AVG_WT"></span></td>
</tr>
</tbody>
</table>
<br/>
<br/><br/>
<button type="button" class="addBtn" onclick="addRow();">Add Row</button>
<br/><br/>
<div style="width: 80%">
<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>