-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess Scheduling.html
713 lines (671 loc) · 37 KB
/
Process 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>OS Simulator - Process Scheduling</title>
<!-- Bootstrap -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<!-- CSS -->
<link rel="stylesheet" href="./CSS/Process Scheduling.css">
<!-- PyScript -->
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
<!-- JavaScript -->
<script>
let PID = 0
function GetAlgorithm()
{
return document.getElementsByClassName("Algorithm")[0].getElementsByTagName("select")[0].value;
}
function AddProcessToTheProcessDetailB(ArrivalTime, BurstTime, PriorityTime) {
var a = document.getElementsByClassName("Terminal-Process-Details")[0].getElementsByClassName('list-group')[0];
var temp = document.createElement('li');
temp.classList.add("list-group-item");
var newContent = `
<div class="col-3">${PID}</div>
<div class="col-3">${ArrivalTime}</div>
<div class="col-3">${BurstTime}</div>
<div class="col-3">${PriorityTime}</div>
`
temp.innerHTML = newContent
a.append(temp)
PID++
}
function AddProcessToTheProcessDetailA(ArrivalTime, BurstTime) {
var a = document.getElementsByClassName("Terminal-Process-Details")[0].getElementsByClassName('list-group')[0];
var temp = document.createElement('li');
temp.classList.add("list-group-item");
var newContent = `
<div class="col-4">${PID}</div>
<div class="col-4">${ArrivalTime}</div>
<div class="col-4">${BurstTime}</div>
`
temp.innerHTML = newContent
a.append(temp)
PID++
}
function CanAlgoBEAppiled() {
return document.getElementsByClassName("Terminal-Process-Details")[0].getElementsByClassName('list-group')[0].children.length;
}
function Output(no, pid, arrival, burst, priority, ct, tat, wt){
var a = document.getElementById("output").getElementsByTagName('table')[0];
var temp = document.createElement('tr');
var newContent = `
<td class="col-1"><span style="color: #0d6efd;">P</span>${no}</td>
<td class="col-1">${pid}</td>
<td class="col-2">${arrival}</td>
<td class="col-2">${burst}</td>
<td class="col-1">${priority}</td>
<td class="col-2">${ct}</td>
<td class="col-1">${tat}</td>
<td class="col-2">${wt}</td>
`;
temp.innerHTML = newContent;
a.append(temp)
};
function getDataOfProcess(i){
let count = document.getElementsByClassName("Terminal-Process-Details")[0].getElementsByClassName('list-group')[0].children
let a = [];
for (let j = 0; j < 3; j++){
let t = document.getElementsByClassName("Terminal-Process-Details")[0].getElementsByClassName('list-group')[0].children[i].children[j].innerText
a[j] = t;
}
return a;
}
function ClearOutput(){
var c = document.getElementById('output')
var d = document.createElement('div');
var newContentForOutput = `
<table class="container">
<tr>
<td class="col-1 fw-medium">PNo.</td>
<td class="col-1 fw-medium">PID</td>
<td class="col-2 fw-medium">Arrival Time</td>
<td class="col-2 fw-medium">Burst Time</td>
<td class="col-1 fw-medium">Priority</td>
<td class="col-2 fw-medium">Completion Time</td>
<td class="col-1 fw-medium">TAT</td>
<td class="col-2 fw-medium">Wait Time</td>
</tr>
</table>
`
d.setAttribute("id","output");
d.innerHTML = newContentForOutput;
c.parentElement.replaceChild(d, c);
PrintAVGWaitingTime(0)
PrintAVGTAT(0)
RemoveTimeQuantum(0)
}
function PrintAVGWaitingTime(AvgWT){
document.getElementById('AvgWT').innerHTML = "Average Waiting Time: <span style=\"color: #0d6efd;\">"+AvgWT+"</span> ";
}
function PrintAVGTAT(AvgTAT){
document.getElementById('AvgTAT').innerHTML = "Average Turn Around Time: <span style=\"color: #0d6efd;\">"+AvgTAT+"</span> ";
}
function PrintTimeQuantum(time_quantum){
document.getElementById('time_quantum').innerHTML = "Time Quantum: <span style=\"color: #0d6efd;\">"+time_quantum+"</span> ";
}
function RemoveTimeQuantum(){
document.getElementById('time_quantum').innerHTML = "Time Quantum: <span style=\"color: #0d6efd;\"> - </span> ";
}
function getTimeQuantum()
{
let a = document.getElementById('TimeQuantum').value
if (a != '' && a != 0 && a > 0)
{
return a;
}
else
{
return -1;
}
}
</script>
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="./Main.html">OS Simulator</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse nav-bar-section-02" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="./Process Scheduling.html">Process Scheduling</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./Virtual Memory.html">Virtual Memory</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./Memory Management.html">Memory Management</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./Our Team.html">Our Team</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="OS-Simulator-Heading container d-flex justify-content-center">
<div class="row display-4">Process Scheduling</div>
</div>
<div class="PSDiv-01 container-fluid">
<div class="lead fs-4 text-center col-lg-6 col-12" style="margin: 10px 5px">Select the respective algorithm for
the
demonstration</div>
<div class="col-lg-4 col-md-6 col-10 Algorithm">
<select class="form-select form-select-lg" aria-label=".form-select-lg example">
<option selected value="FCFS">FCFS Algorithm</option>
<option value="SJF Pre-emptive">SJF Algorithm (Pre-emptive)</option>
<option value="SJF Non Pre-emptive">SJF Algorithm (Non Pre-emptive)</option>
<option value="Priority">Priority Algorithm (Non Pre-emptive)</option>
<option value="Round Robin">Round Robin Algorithm</option>
</select>
<script>
document.getElementsByClassName("Algorithm")[0].getElementsByTagName("select")[0].onchange = (event) => {
var selectedAlgo = event.target.value;
if (selectedAlgo == "FCFS") {
document.getElementById("ArrivalTimeDiv").style.display = "flex";
document.getElementById("BurstTimeDiv").style.display = "flex";
document.getElementById("PriorityTimeDiv").style.display = "none";
document.getElementById("TimeQuantumDiv").style.display = "none";
// Replacing Terminal contents
var a = document.getElementsByClassName("Terminal-Process-Details")[0];
var b = document.createElement('div');
var newContent = `
<ul class="list-group list-group-flush" style="padding: 0px 8px;">
<li class="list-group-item">
<div class="col-4 fw-medium">PID</div>
<div class="col-4 fw-medium">Arrival Time</div>
<div class="col-4 fw-medium">Burst Time</div>
</li>
</ul>
`
b.innerHTML = newContent;
b.classList.add('Terminal-Process-Details')
a.parentNode.replaceChild(b, a);
}
else if (selectedAlgo == "SJF Pre-emptive" || selectedAlgo == "SJF Non Pre-emptive") {
document.getElementById("ArrivalTimeDiv").style.display = "flex";
document.getElementById("BurstTimeDiv").style.display = "flex";
document.getElementById("PriorityTimeDiv").style.display = "none";
document.getElementById("TimeQuantumDiv").style.display = "none";
// Replacing Terminal contents
var a = document.getElementsByClassName("Terminal-Process-Details")[0];
var b = document.createElement('div');
var newContent = `
<ul class="list-group list-group-flush" style="padding: 0px 8px;">
<li class="list-group-item">
<div class="col-4 fw-medium">PID</div>
<div class="col-4 fw-medium">Arrival Time</div>
<div class="col-4 fw-medium">Burst Time</div>
</li>
</ul>
`
b.innerHTML = newContent;
b.classList.add('Terminal-Process-Details')
a.parentNode.replaceChild(b, a);
}
else if (selectedAlgo == "Priority") {
document.getElementById("ArrivalTimeDiv").style.display = "flex";
document.getElementById("BurstTimeDiv").style.display = "flex";
document.getElementById("PriorityTimeDiv").style.display = "flex";
document.getElementById("TimeQuantumDiv").style.display = "none";
// Replacing Terminal contents
var a = document.getElementsByClassName("Terminal-Process-Details")[0];
var b = document.createElement('div');
var newContent = `
<ul class="list-group list-group-flush" style="padding: 0px 8px;">
<li class="list-group-item">
<div class="col-3 fw-medium">PID</div>
<div class="col-3 fw-medium">Arrival Time</div>
<div class="col-3 fw-medium">Burst Time</div>
<div class="col-3 fw-medium">Priority Time</div>
</li>
</ul>
`
b.innerHTML = newContent;
b.classList.add('Terminal-Process-Details')
a.parentNode.replaceChild(b, a);
}
else if (selectedAlgo == "Round Robin") {
document.getElementById("ArrivalTimeDiv").style.display = "flex";
document.getElementById("BurstTimeDiv").style.display = "flex";
document.getElementById("PriorityTimeDiv").style.display = "none";
document.getElementById("TimeQuantumDiv").style.display = "flex";
// Replacing Terminal contents
var a = document.getElementsByClassName("Terminal-Process-Details")[0];
var b = document.createElement('div');
var newContent = `
<ul class="list-group list-group-flush" style="padding: 0px 8px;">
<li class="list-group-item">
<div class="col-4 fw-medium">PID</div>
<div class="col-4 fw-medium">Arrival Time</div>
<div class="col-4 fw-medium">Burst Time</div>
</li>
</ul>
`
b.innerHTML = newContent;
b.classList.add('Terminal-Process-Details')
a.parentNode.replaceChild(b, a);
}
ClearOutput()
}
</script>
</div>
</div>
<div class="PSDiv-02 container-fluid">
<div class="PSDiv-02-01 col-10 col-lg-5">
<div class="Input-Field">
<div class="lead fs-4 text-center" style="margin: 10px 0px">Enter the following required details of
the process</div>
<div class="container Input-Field-Div-01">
<div class="col-10" id="ArrivalTimeDiv">
<label for="ArrivalTime" class="col-5">Arrival Time</label>
<input type="number" class="col-6" id="ArrivalTime" placeholder="e.g. 0">
</div>
<div class="col-10" id="BurstTimeDiv">
<label for="BurstTime" class="col-5">Burst Time</label>
<input type="number" class="col-6" id="BurstTime" placeholder="e.g. 10">
</div>
<div class="col-10" id="PriorityTimeDiv">
<label for="PriorityTime" class="col-5">Priority Time</label>
<input type="number" class="col-6" id="PriorityTime" placeholder="e.g. 3">
</div>
<div class="col-10" id="TimeQuantumDiv">
<label for="TimeQuantum" class="col-5">Time Quantum</label>
<input type="number" class="col-6" id="TimeQuantum" placeholder="e.g. 1">
</div>
<button type="button" class="btn btn-primary Add-Process" py-click="AddProcess()">Add
Process</button>
</div>
<py-script>
from js import GetAlgorithm, alert, AddProcessToTheProcessDetailA, AddProcessToTheProcessDetailB
def AddProcess():
algo = GetAlgorithm()
if algo == "FCFS" or algo == "SJF Non Pre-emptive" or algo == "SJF Pre-emptive":
ArrivalTimeDiv = Element("ArrivalTime");
BurstTimeDiv = Element("BurstTime");
if ArrivalTimeDiv.value == '' or BurstTimeDiv.value == '':
alert("Kindly enter all of the required data fields.")
else:
if int(ArrivalTimeDiv.value) < 0 or int(BurstTimeDiv.value) < 0:
alert("All values must be positive.")
elif (int(BurstTimeDiv.value) == 0 ):
alert("Burst time can not be zero.")
else:
AddProcessToTheProcessDetailA(int(ArrivalTimeDiv.value), int(BurstTimeDiv.value))
elif algo == "Priority":
ArrivalTimeDiv = Element("ArrivalTime");
BurstTimeDiv = Element("BurstTime");
PriorityTimeDiv = Element("PriorityTime");
if ArrivalTimeDiv.value == '' or BurstTimeDiv.value == '' or PriorityTimeDiv.value == '':
alert("Kindly enter all of the required data fields.")
else:
if int(ArrivalTimeDiv.value) < 0 or int(BurstTimeDiv.value) < 0 or int(PriorityTimeDiv.value) < 0:
alert("All values must be positive.")
elif (int(BurstTimeDiv.value) == 0 ):
alert("Burst time can not be zero.")
else:
AddProcessToTheProcessDetailB(int(ArrivalTimeDiv.value), int(BurstTimeDiv.value), int(PriorityTimeDiv.value))
elif algo == "Round Robin":
ArrivalTimeDiv = Element("ArrivalTime");
BurstTimeDiv = Element("BurstTime");
TimeQuantumDiv = Element("TimeQuantum");
if ArrivalTimeDiv.value == '' or BurstTimeDiv.value == '' or TimeQuantumDiv.value == '':
alert("Kindly enter all of the required data fields.")
else:
if int(ArrivalTimeDiv.value) < 0 or int(BurstTimeDiv.value) < 0 or int(TimeQuantumDiv.value) < 0:
alert("All values must be positive.")
elif (int(TimeQuantumDiv.value) == 0 ):
alert("Burst time can not be zero.")
else:
AddProcessToTheProcessDetailA(int(ArrivalTimeDiv.value), int(BurstTimeDiv.value))
</py-script>
</div>
</div>
<div class="PSDiv-02-02 col-10 col-lg-6">
<div class="Terminal">
<div class="lead fs-4" style="display: flex; justify-content: space-between; align-items: center; padding:10px 11px;">Processes Details
<button type="button" class="btn btn-primary Add-Process" py-click="Execute()">Execute</button>
</div>
<div class="Terminal-Process-Details">
<ul class="list-group list-group-flush" style="padding: 0px 8px;">
<li class="list-group-item">
<div class="col-4 fw-medium">PID</div>
<div class="col-4 fw-medium">Arrival Time</div>
<div class="col-4 fw-medium">Burst Time</div>
</li>
</ul>
</div>
</div>
<py-script>
from js import alert, GetAlgorithm, CanAlgoBEAppiled, Output, getDataOfProcess, PrintAVGWaitingTime, PrintAVGTAT, ClearOutput, getTimeQuantum, PrintTimeQuantum
class Process:
def __init__(self, pid, arrival_time, burst_time, priority):
self.pid = pid
self.arrival_time = arrival_time
self.burst_time = burst_time
self.priority = priority
self.remaining_time = burst_time
def Execute():
numberOfProcess = CanAlgoBEAppiled()
if numberOfProcess > 1:
algo = GetAlgorithm()
if algo == "FCFS":
ClearOutput()
FCFS()
elif algo == "SJF Pre-emptive":
ClearOutput()
SJF_Preemptive()
elif algo == "SJF Non Pre-emptive":
ClearOutput()
SJF_NonPreemptive()
elif algo == "Priority":
ClearOutput()
Priority()
elif algo == "Round Robin":
ClearOutput()
RoundRobin()
else:
alert("No process in the data.")
def FCFS():
# Getting data of processes
processes = []
count = CanAlgoBEAppiled()
a = []
for i in range(1, count, 1):
a = getDataOfProcess(i)
processes.append(Process(int(a[0]), int(a[1]), int(a[2]), 0))
# Sort the processes based on their arrival time
processes = sorted(processes, key=lambda p: p.arrival_time)
# Initialize the current time and waiting time
cur_time = processes[0].arrival_time
current_time = []
completion_time = []
wait_time = 0
waiting_time = []
ta_time = 0
turnaround_time = []
total_turnaround_time = 0
total_waiting_time = 0
j = 0
# Loop through the processes and calculate their waiting time
for process in processes:
# Calculate the waiting time for the current process
cur_time = max(cur_time, process.arrival_time)
current_time.append(cur_time)
compl_time = cur_time + process.burst_time
completion_time.append(compl_time)
ta_time = compl_time - process.arrival_time
turnaround_time.append(ta_time)
wait_time = ta_time - process.burst_time
waiting_time.append(wait_time)
# Update the current time
cur_time += process.burst_time
total_turnaround_time += ta_time
total_waiting_time += wait_time
Output(j, processes[j].pid, processes[j].arrival_time, processes[j].burst_time, processes[j].priority, compl_time, ta_time, wait_time)
j+=1
# Calculate the average waiting time
avg_waiting_time = total_waiting_time / len(processes)
avg_turnaround_time = total_turnaround_time / len(processes)
PrintAVGWaitingTime(avg_waiting_time)
PrintAVGTAT(avg_turnaround_time)
def SJF_Preemptive():
# Getting data of processes
processes = []
count = CanAlgoBEAppiled()
a = []
for i in range(1, count, 1):
a = getDataOfProcess(i)
processes.append(Process(int(a[0]), int(a[1]), int(a[2]), 0))
# Sort the processes based on their arrival time
processes = sorted(processes, key=lambda p: p.arrival_time)
cur_time = processes[0].arrival_time
n = len(processes)
completed = [False] * n
current_time = []
completion_time = []
turnaround_time = []
waiting_time = []
total_turnaround_time = 0
total_waiting_time = 0
j = 0
while True:
# Find the process with the shortest remaining time
min_remaining_time = float('inf')
min_index = -1
for i in range(n):
if not completed[i] and processes[i].arrival_time <= cur_time and processes[i].remaining_time < min_remaining_time:
min_remaining_time = processes[i].remaining_time
min_index = i
if min_index == -1:
# All processes have been executed
break
# Execute the process for 1 time unit
process = processes[min_index]
process.remaining_time -= 1
# If the process has completed, calculate its completion time, turnaround time, and waiting time
if process.remaining_time == 0:
current_time.append(cur_time)
compl_time = cur_time + 1
completion_time.append(compl_time)
ta_time = compl_time - process.arrival_time
turnaround_time.append(ta_time)
wait_time = ta_time - process.burst_time
waiting_time.append(wait_time)
total_turnaround_time += ta_time
total_waiting_time += wait_time
completed[min_index] = True
Output(j, processes[j].pid, processes[j].arrival_time, processes[j].burst_time, processes[j].priority, compl_time, ta_time, wait_time)
j+=1
# Update the current time
cur_time += 1
# Calculate the average waiting time
avg_waiting_time = total_waiting_time / n
avg_turnaround_time = total_turnaround_time / n
PrintAVGWaitingTime(avg_waiting_time)
PrintAVGTAT(avg_turnaround_time)
def SJF_NonPreemptive():
# Getting data of processes
processes = []
count = CanAlgoBEAppiled()
a = []
for i in range(1, count, 1):
a = getDataOfProcess(i)
processes.append(Process(int(a[0]), int(a[1]), int(a[2]), 0))
# Sort the processes based on their burst time
processes = sorted(processes, key=lambda p: p.arrival_time)
cur_time = processes[0].arrival_time
processes = sorted(processes, key=lambda p: p.burst_time)
n = len(processes)
completed = [False] * n
current_time = []
completion_time = []
turnaround_time = []
waiting_time = []
total_turnaround_time = 0
total_waiting_time = 0
j = 0
while True:
# Find the shortest job that has arrived but has not been executed
min_burst_time = float('inf')
min_index = -1
for i in range(n):
if (not completed[i] and processes[i].arrival_time <= cur_time and processes[i].burst_time < min_burst_time):
min_burst_time = processes[i].burst_time
min_index = i
if min_index == -1:
# All processes have been executed
break
# Execute the shortest job
process = processes[min_index]
current_time.append(cur_time)
compl_time = cur_time + process.burst_time
completion_time.append(compl_time)
ta_time = compl_time - process.arrival_time
turnaround_time.append(ta_time)
wait_time = ta_time - process.burst_time
waiting_time.append(wait_time)
total_turnaround_time += ta_time
total_waiting_time += wait_time
completed[min_index] = True
Output(j, processes[j].pid, processes[j].arrival_time, processes[j].burst_time, processes[j].priority, compl_time, ta_time, wait_time)
j+=1
# Update the current time
cur_time = compl_time
# Calculate the average waiting time
avg_waiting_time = total_waiting_time / n
avg_turnaround_time = total_turnaround_time / n
# Print the average waiting time
PrintAVGWaitingTime(avg_waiting_time)
PrintAVGTAT(avg_turnaround_time)
def Priority():
# Getting data of processes
processes = []
count = CanAlgoBEAppiled()
a = []
for i in range(1, count, 1):
a = getDataOfProcess(i)
processes.append(Process(int(a[0]), int(a[1]), int(a[2]), 0))
processes = sorted(processes, key=lambda p: p.arrival_time)
cur_time = processes[0].arrival_time
# Sort the processes based on their priority
processes = sorted(processes, key=lambda p: p.priority, reverse=True)
n = len(processes)
completed = [False] * n
current_time = []
completion_time = []
turnaround_time = []
waiting_time = []
total_turnaround_time = 0
total_waiting_time = 0
j = 0
while True:
# Find the highest priority process that has arrived but has not been executed
highest_priority = 1000000 #Supposing a max
min_index = -1
for i in range(n):
if not completed[i] and processes[i].arrival_time <= cur_time and processes[i].priority < highest_priority:
highest_priority = processes[i].priority
min_index = i
if min_index == -1:
# All processes have been executed
break
# Execute the highest priority process
process = processes[min_index]
current_time.append(cur_time)
compl_time = cur_time + process.burst_time
completion_time.append(compl_time)
ta_time = compl_time - process.arrival_time
turnaround_time.append(ta_time)
wait_time = ta_time - process.burst_time
waiting_time.append(wait_time)
total_turnaround_time += ta_time
total_waiting_time += wait_time
completed[min_index] = True
Output(j, process.pid, process.arrival_time, process.burst_time, process.priority, compl_time, ta_time, wait_time)
j+=1
# Update the current time
cur_time = compl_time
# Calculate the average waiting time
avg_waiting_time = total_waiting_time / n
avg_turnaround_time = total_turnaround_time / n
# Print the average waiting time
PrintAVGWaitingTime(avg_waiting_time)
PrintAVGTAT(avg_turnaround_time)
def RoundRobin():
time_quantum = int(getTimeQuantum())
if time_quantum == -1:
alert("Invalid Time Quantum")
else:
# Getting data of processes
processes = []
count = CanAlgoBEAppiled()
a = []
for i in range(1, count, 1):
a = getDataOfProcess(i)
processes.append(Process(int(a[0]), int(a[1]), int(a[2]), 0))
current_time = 0
waiting_time = 0
total_turnaround_time = 0
total_waiting_time = 0
n = len(processes)
# Initialize a list to keep track of completed processes
completed = [False] * n
j = 0
# Loop through the processes and calculate their waiting time
while True:
all_completed = True
# Loop through the processes and execute them for the time quantum
for i in range(n):
if not completed[i]:
all_completed = False
process = processes[i]
if process.remaining_time > time_quantum:
# Execute the process for the time quantum
process.remaining_time -= time_quantum
current_time += time_quantum
else:
# Execute the process for the remaining time
current_time += process.remaining_time
process.remaining_time = 0
completed[i] = True
# Calculate the waiting time for the completed process
completion_time = current_time
turnaround_time = completion_time - process.arrival_time
waiting_time = turnaround_time - process.burst_time
total_turnaround_time += turnaround_time
total_waiting_time += waiting_time
Output(j, process.pid, process.arrival_time, process.burst_time, process.priority, completion_time, turnaround_time, waiting_time)
j+=1
if all_completed:
break
# Calculate the average waiting time
avg_waiting_time = total_waiting_time / n
avg_turnaround_time = total_turnaround_time / n
# Print the average waiting time
PrintAVGWaitingTime(avg_waiting_time)
PrintAVGTAT(avg_turnaround_time)
PrintTimeQuantum(time_quantum)
</py-script>
</div>
</div>
<div class="PSDiv-03 container-fluid offset-1 col-10">
<div class="lead fs-4" style="margin: 10px 5px">Output</div>
<div id="output">
<table class="container">
<tr>
<td class="col-1 fw-medium">PNo.</td>
<td class="col-1 fw-medium">PID</td>
<td class="col-2 fw-medium">Arrival Time</td>
<td class="col-2 fw-medium">Burst Time</td>
<td class="col-1 fw-medium">Priority</td>
<td class="col-2 fw-medium">Completion Time</td>
<td class="col-1 fw-medium">TAT</td>
<td class="col-2 fw-medium">Wait Time</td>
</tr>
</table>
</div>
<div id="AvgWT">
Average Waiting Time: <span style="color: #0d6efd;">0</span>
</div>
<div id="AvgTAT">
Average Turn Around Time: <span style="color: #0d6efd;">0</span>
</div>
<div id="time_quantum">
Time Quantum: <span style="color: #0d6efd;"> - </span>
</div>
</div>
</body>
</html>