-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority.py
35 lines (26 loc) · 1.21 KB
/
priority.py
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
from typing import List
from utils import print_table, Process, print_gantt_chart, GanttChart
from utils.examples import PRIORITY_EXAMPLES
def priority(processes: List[Process]) -> None:
"""
**Priority** scheduling algorithm implementation
``An algorithm in which the process having the highest priority execution time is chosen for the next execution.``
`read more <https://www.guru99.com/priority-scheduling-program.html>`_
"""
gantt_chart = []
executed_queue = []
current_time = 0
while processes:
arrived_processes = list(filter(lambda p: p.arrival_time <= current_time, processes))
arrived_processes.sort(key=lambda p: p.priority)
process = arrived_processes[0]
processes.remove(process)
process.waiting_time = max(0, current_time - process.arrival_time)
process.turnaround_time = process.burst_time + process.waiting_time
current_time += process.burst_time
executed_queue.append(process)
gantt_chart.append(GanttChart(name=process.name, arrival_time=current_time))
print_gantt_chart(gantt_chart)
print_table(executed_queue, show_priority=True)
if __name__ == '__main__':
priority(PRIORITY_EXAMPLES.example2)