-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
426 lines (401 loc) · 10.1 KB
/
sketch.js
File metadata and controls
426 lines (401 loc) · 10.1 KB
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
/**
* crea un objetode tipo proceso que sea manipulable para las colas
* @param {int} id el id del proceso (Pid)
* @param {int} at en tiempo de entrada (Arraiving time)
* @param {int} ext el tiempo de ejecucion (Execution Time)
* @param {int} prl el nivel de prioridad
* @returns {object} RR process object
*/
function RRprocess(id, at, ext,prl, color) {
this.id = id;
this.at = at;
this.ext = ext;
this.prl = prl;
this.totalduration = ext;
this.realtimeBuffer = ext;
this.color = color;
}
var processesCounter = 1;
var prlis = [];
var realTimeQuewe = [];
var currentProcess;
var burstcounter;
var quantum;
var running = false;
function setup() {
frameRate(12);
burstcounter = 0;
currentProcess = {};
createCanvas(900, 300);
addcontainer();
addTable();
quantum = 0 | document.getElementById("inputQ").value;
}
function draw() {
updateTables();
if (running) {
drawstuff();
} else {
if(realTimeQuewe.length == 0){
background(225);
}
}
}
/**
* dibuja lo eferente al canvas
* @date 2021-03-07
* @returns {any}
*/
function drawstuff() {
drawPNames();
prlis.forEach((element) => {
if (element.at == burstcounter) {
realTimeQuewe.push(element);
console.log("agregado a la RTQ");
}
});
if (realTimeQuewe.length > 0) {
if (
realTimeQuewe[0].realtimeBuffer >
realTimeQuewe[0].totalduration - quantum &&
realTimeQuewe[0].totalduration - quantum >= 0
) {
realTimeQuewe[0].realtimeBuffer--;
} else if (
realTimeQuewe[0].realtimeBuffer == 0 ||
realTimeQuewe[0].realtimeBuffer ==
realTimeQuewe[0].totalduration - quantum
) {
headToBack();
} else if (
realTimeQuewe[0].realtimeBuffer > 0 &&
realTimeQuewe[0].totalduration - quantum < 0
) {
realTimeQuewe[0].realtimeBuffer--;
}
}
drawlines();
burstcounter++;
}
/**
* dibuja las lineas de tiempo
* @date 2021-03-07
* @returns {any}
*/
function drawlines() {
strokeWeight(4);
for (index = 1; index <= prlis.length; index++) {
if (realTimeQuewe.length > 0) {
try {
if (realTimeQuewe[0].id == index) {
drawRedLine(index,realTimeQuewe[0].color);
} else {
drawWhiteLine(index);
}
} catch (error) {
drawWhiteLine(index);
}
} else {
drawWhiteLine(index);
}
}
}
/**
* dibuja unalinea eferente al proceso con su color
* @date 2021-03-07
* @param {any} index
* @param {any} color
* @returns {any}
*/
function drawRedLine(index,color) {
htime = getHTime();
var x = 50 + map(burstcounter, 0, htime, 20, 300);
var y = 29 * index;
color2 = exToRGB(color);
stroke(color2.r,color2.g, color2.b);
line(x, y, x, y + 16);
stroke(255);
}
/**
* convierte exadecimal a rgb
* @date 2021-03-07
* @param {any} exColor
* @returns {any}
*/
function exToRGB(exColor){
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(exColor);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
/**
* dibuja las linear referentes a momentos en los que el proceso no
* se esta ejecutando
* @date 2021-03-07
* @param {any} index
* @returns {any}
*/
function drawWhiteLine(index) {
htime = getHTime();
var x = 50 + map(burstcounter, 0, htime, 20, 300);
var y = 29 * index;
stroke(255, 255, 255);
line(x, y, x, y + 16);
}
/**
* muestra la cola en tiempo real de los procesos
* @date 2021-03-07
* @returns {any}
*/
function headToBack() {
// reiniciendo el buffer
if (realTimeQuewe.length > 0) {
realTimeQuewe[0].totalduration = realTimeQuewe[0].realtimeBuffer;
if (realTimeQuewe[0].totalduration > 0) {
var buffer = realTimeQuewe[0];
realTimeQuewe.splice(0, 1);
realTimeQuewe.push(buffer);
} else {
realTimeQuewe.splice(0, 1);
//console.log("eliminado de la cabeza");
}
}
// colocando el de mayor prioridad en la cola
higestP = Math.max.apply(null,realTimeQuewe.map(o => {return parseInt(o.prl);}));
prbuffer = realTimeQuewe.filter(o => o.prl == higestP)[0];
if(prbuffer){
realTimeQuewe.splice(realTimeQuewe.indexOf(prbuffer), 1);
realTimeQuewe.unshift(prbuffer);
if(prbuffer.prl>1){prbuffer.prl--;}
}
}
/**
*dibuja los nobres P1,P2... en el canvas
* @date 2021-03-07
* @returns {any}
*/
function drawPNames() {
prlis.forEach((process) => {
textSize(32);
text("P" + process.id, 9, 15 + 29 * (prlis.indexOf(process) + 1));
});
}
/**
* obtiene el tiempo maximo de ejecucion
* @date 2021-03-07
* @returns {any}
*/
function getHTime() {
var res = -1;
prlis.forEach((element) => {
var at = parseInt("" + element.at);
var ext = parseInt("" + element.ext);
res += ext;
});
res = res/prlis.length;// tiempo medio de ejecucion
return res+(2.4*prlis.length)*(map(quantum, 1, 1000, 5, 1));
}
/**
* metodo que se ejecuta cuando el proceso se pausa
* @date 2021-03-07
* @returns {any}
*/
function drawPause() {
clear();
background(225);
textFont("Georgia");
textSize(32);
text("Add processes to the list and then", 50, 130);
text("Press start to see the Execution timeline", 50, 170);
fill(0, 102, 153);
}
/**
*metodo que coloca la tabla de procesos
* @date 2021-03-07
* @returns {any}
*/
function addTable() {
tableContainer = createDiv();
tableContainer.position(200, 320);
tableContainer.size(600, 300);
tableContainer.addClass("tables-container");
RRproclist = createDiv(`
<table class="processT">
<thead>
<tr>
<th>Pid</th>
<th>Arraiving time</th>
<th>Execution time</th>
<th>Priority level</th>
</tr>
</thead>
<tbody id="processT">
</tbody>
</table>`);
RRproclist.addClass("processes-table");
tableContainer.child(RRproclist);
quewe = createDiv(`
<table class="processT"">
<thead>
<tr>
<th>Real time quewe</th>
</tr>
</thead>
<tbody id="processQ">
</tbody>
</table>`);
quewe.addClass("quewe-table");
tableContainer.child(quewe);
}
/**
* metodo que agrega el contenedor conde se coloca la mayoria de items
* @date 2021-03-07
* @returns {any}
*/
function addcontainer() {
container = createDiv();
container.position(920, 0);
title = createP("<h1>Round Robin Scheduling</h1>");
container.child(title);
labelAT = createP("<p>Arraiving Time (aT)</p>");
container.child(labelAT);
inputAT = createInput(null, "number");
inputAT.id("inputAT");
container.child(inputAT);
LabelET = createP("<p>Execution time (xT)</p>");
container.child(LabelET);
inputET = createInput(null, "number");
inputET.id("inputET");
container.child(inputET);
LabelPr = createP("<p>Priority level</p>");
container.child(LabelPr);
inputPr = createInput(null, "number");
inputPr.id("inputPr");
container.child(inputPr);
btnSend = createButton("Add Process", "").size(200, 60);
btnSend.mousePressed(addProcess);
btnSend.addClass("btn-send");
container.child(btnSend);
labelQ = createP("<p>OS quantum</p>");
container.child(labelQ);
inputQ = createInput(10, "number");
inputQ.id("inputQ");
container.child(inputQ);
btnPlay = createButton("start", "").size(200, 35);
btnPlay.mousePressed(playRR);
btnPlay.addClass("btn-send");
container.child(btnPlay);
btnPause = createButton("pause", "").size(200, 35);
btnPause.mousePressed(pauseRR);
btnPause.addClass("btn-send");
container.child(btnPause);
btnRestart = createButton("reset", "").size(200, 35);
btnRestart.mousePressed(function () {
resetThis();
});
btnRestart.addClass("btn-send");
container.child(btnRestart);
// btnNQ = createButton("jump quantum", "").size(200, 35);
// btnNQ.mousePressed(jumpQuantum);
// btnNQ.addClass("btn-send");
// container.child(btnNQ);
container.size(300, 850);
container.addClass("form-Container");
}
/**
* metodo para arancar
* @date 2021-03-07
* @returns {any}
*/
function playRR() {
quantum = document.getElementById("inputQ").value;
running = true;
}
/**
* metodo para pausar
* @date 2021-03-07
* @returns {any}
*/
function pauseRR() {
running = false;
}
/**
* metodo para agregar un proceso
* @date 2021-03-07
* @returns {any}
*/
function addProcess() {
inputAT = document.getElementById("inputAT").value;
inputET = document.getElementById("inputET").value;
inputPr = document.getElementById("inputPr").value;
if (inputAT && inputET) {
newprocess = new RRprocess(
processesCounter,
inputAT,
inputET,
inputPr,
getRandomColor()
);
processesCounter++;
prlis.push(newprocess);
} else {
alert("Los campos no deben estar vacios");
}
}
/**
* metodo para resetear el sistema
* @date 2021-03-07
* @returns {any}
*/
function resetThis() {
processesCounter = 1;
prlis = [];
realTimeQuewe = [];
currentProcess = {};
burstcounter = 0;
quantum = 0 | document.getElementById("inputET").value;
running = false;
}
/**
* metodo para actualizar las tablas
* @date 2021-03-07
* @returns {any}
*/
function updateTables() {
document.getElementById("processT").innerHTML = "";
document.getElementById("processQ").innerHTML = "";
for (i = 0; i < prlis.length; i++) {
row = document.getElementById("processT").insertRow(i);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = prlis[i].id;
cell2.innerHTML = prlis[i].at;
cell3.innerHTML = prlis[i].ext;
cell4.innerHTML = prlis[i].prl;
}
for (j = 0; j < realTimeQuewe.length; j++) {
row = document.getElementById("processQ").insertRow(j);
var cellP = row.insertCell(0);
try {
cellP.innerHTML = "P" + realTimeQuewe[j].id+ '-' + realTimeQuewe[j].realtimeBuffer + '-'+realTimeQuewe[j].prl;
} catch (error) {
}
}
}
/**
* metodo para obtener un color aleatorio
* @date 2021-03-07
* @returns {any}
*/
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}