Skip to content

Commit 45345ae

Browse files
committed
Add EDF scheduling script
1 parent 4259348 commit 45345ae

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

scheduling/earliest_deadline_first.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Earliest Deadline First (EDF) scheduling algorithm
3+
In EDF, the process with the earliest deadline is selected for execution.
4+
https://en.wikipedia.org/wiki/Earliest_deadline_first_scheduling
5+
Author: KelvinPuyam
6+
"""
7+
8+
from __future__ import annotations
9+
10+
11+
def calculate_waiting_times(
12+
arrival_times: list[int],
13+
burst_times: list[int],
14+
deadlines: list[int]
15+
) -> list[int]:
16+
"""
17+
Calculate the waiting times of processes using EDF algorithm.
18+
Return: List of waiting times for each process.
19+
"""
20+
n = len(arrival_times)
21+
waiting_times = [0] * n
22+
remaining_times = burst_times.copy()
23+
24+
current_time = 0
25+
process_executed = 0
26+
while process_executed < n:
27+
min_deadline = float('inf')
28+
selected_process = -1
29+
for i in range(n):
30+
if (
31+
arrival_times[i] <= current_time
32+
and remaining_times[i] > 0
33+
and deadlines[i] < min_deadline
34+
):
35+
min_deadline = deadlines[i]
36+
selected_process = i
37+
38+
if selected_process == -1:
39+
current_time += 1
40+
continue
41+
42+
waiting_times[selected_process] = current_time - arrival_times[selected_process]
43+
current_time += burst_times[selected_process]
44+
remaining_times[selected_process] = 0
45+
process_executed += 1
46+
47+
return waiting_times
48+
49+
def calculate_turnaround_times(
50+
burst_times: list[int],
51+
waiting_times: list[int]
52+
) -> list[int]:
53+
"""
54+
Calculate the turnaround times of processes.
55+
Return: List of turnaround times for each process.
56+
"""
57+
return [burst_times[i] + waiting_times[i] for i in range(len(burst_times))]
58+
59+
def calculate_average_turnaround_time(turnaround_times: list[int]) -> float:
60+
"""
61+
Calculate the average turnaround time.
62+
Return: The average turnaround time.
63+
"""
64+
return sum(turnaround_times) / len(turnaround_times)
65+
66+
def calculate_average_waiting_time(waiting_times: list[int]) -> float:
67+
"""
68+
Calculate the average waiting time.
69+
Return: The average waiting time.
70+
"""
71+
return sum(waiting_times) / len(waiting_times)
72+
73+
if __name__ == "__main__":
74+
arrival_times = [0, 1, 2]
75+
burst_times = [3, 5, 2]
76+
deadlines = [5, 7, 6]
77+
78+
waiting_times = calculate_waiting_times(arrival_times, burst_times, deadlines)
79+
turnaround_times = calculate_turnaround_times(burst_times, waiting_times)
80+
81+
average_waiting_time = calculate_average_waiting_time(waiting_times)
82+
average_turnaround_time = calculate_average_turnaround_time(turnaround_times)
83+
84+
print("Process\tBurst Time\tWaiting Time\tTurnaround Time")
85+
for i in range(len(arrival_times)):
86+
print(f"{i+1}\t{burst_times[i]}\t\t{waiting_times[i]}\t\t{turnaround_times[i]}")
87+
88+
print(f"Average waiting time: {average_waiting_time}")
89+
print(f"Average turnaround time: {average_turnaround_time}")

0 commit comments

Comments
 (0)