From 45345aeefa2c603054aed663e77ade32177a610c Mon Sep 17 00:00:00 2001 From: KelvinPuyam Date: Tue, 23 Apr 2024 18:46:00 +0530 Subject: [PATCH 1/2] Add EDF scheduling script --- scheduling/earliest_deadline_first.py | 89 +++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 scheduling/earliest_deadline_first.py diff --git a/scheduling/earliest_deadline_first.py b/scheduling/earliest_deadline_first.py new file mode 100644 index 000000000000..fa3fa8b4f82a --- /dev/null +++ b/scheduling/earliest_deadline_first.py @@ -0,0 +1,89 @@ +""" +Earliest Deadline First (EDF) scheduling algorithm +In EDF, the process with the earliest deadline is selected for execution. +https://en.wikipedia.org/wiki/Earliest_deadline_first_scheduling +Author: KelvinPuyam +""" + +from __future__ import annotations + + +def calculate_waiting_times( + arrival_times: list[int], + burst_times: list[int], + deadlines: list[int] +) -> list[int]: + """ + Calculate the waiting times of processes using EDF algorithm. + Return: List of waiting times for each process. + """ + n = len(arrival_times) + waiting_times = [0] * n + remaining_times = burst_times.copy() + + current_time = 0 + process_executed = 0 + while process_executed < n: + min_deadline = float('inf') + selected_process = -1 + for i in range(n): + if ( + arrival_times[i] <= current_time + and remaining_times[i] > 0 + and deadlines[i] < min_deadline + ): + min_deadline = deadlines[i] + selected_process = i + + if selected_process == -1: + current_time += 1 + continue + + waiting_times[selected_process] = current_time - arrival_times[selected_process] + current_time += burst_times[selected_process] + remaining_times[selected_process] = 0 + process_executed += 1 + + return waiting_times + +def calculate_turnaround_times( + burst_times: list[int], + waiting_times: list[int] +) -> list[int]: + """ + Calculate the turnaround times of processes. + Return: List of turnaround times for each process. + """ + return [burst_times[i] + waiting_times[i] for i in range(len(burst_times))] + +def calculate_average_turnaround_time(turnaround_times: list[int]) -> float: + """ + Calculate the average turnaround time. + Return: The average turnaround time. + """ + return sum(turnaround_times) / len(turnaround_times) + +def calculate_average_waiting_time(waiting_times: list[int]) -> float: + """ + Calculate the average waiting time. + Return: The average waiting time. + """ + return sum(waiting_times) / len(waiting_times) + +if __name__ == "__main__": + arrival_times = [0, 1, 2] + burst_times = [3, 5, 2] + deadlines = [5, 7, 6] + + waiting_times = calculate_waiting_times(arrival_times, burst_times, deadlines) + turnaround_times = calculate_turnaround_times(burst_times, waiting_times) + + average_waiting_time = calculate_average_waiting_time(waiting_times) + average_turnaround_time = calculate_average_turnaround_time(turnaround_times) + + print("Process\tBurst Time\tWaiting Time\tTurnaround Time") + for i in range(len(arrival_times)): + print(f"{i+1}\t{burst_times[i]}\t\t{waiting_times[i]}\t\t{turnaround_times[i]}") + + print(f"Average waiting time: {average_waiting_time}") + print(f"Average turnaround time: {average_turnaround_time}") From a8c3af7191ff6adb415e8f7e23d9e6e44d450df3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:18:36 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- scheduling/earliest_deadline_first.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/scheduling/earliest_deadline_first.py b/scheduling/earliest_deadline_first.py index fa3fa8b4f82a..04eb14ff0ef5 100644 --- a/scheduling/earliest_deadline_first.py +++ b/scheduling/earliest_deadline_first.py @@ -9,9 +9,7 @@ def calculate_waiting_times( - arrival_times: list[int], - burst_times: list[int], - deadlines: list[int] + arrival_times: list[int], burst_times: list[int], deadlines: list[int] ) -> list[int]: """ Calculate the waiting times of processes using EDF algorithm. @@ -24,7 +22,7 @@ def calculate_waiting_times( current_time = 0 process_executed = 0 while process_executed < n: - min_deadline = float('inf') + min_deadline = float("inf") selected_process = -1 for i in range(n): if ( @@ -32,8 +30,8 @@ def calculate_waiting_times( and remaining_times[i] > 0 and deadlines[i] < min_deadline ): - min_deadline = deadlines[i] - selected_process = i + min_deadline = deadlines[i] + selected_process = i if selected_process == -1: current_time += 1 @@ -46,9 +44,9 @@ def calculate_waiting_times( return waiting_times + def calculate_turnaround_times( - burst_times: list[int], - waiting_times: list[int] + burst_times: list[int], waiting_times: list[int] ) -> list[int]: """ Calculate the turnaround times of processes. @@ -56,6 +54,7 @@ def calculate_turnaround_times( """ return [burst_times[i] + waiting_times[i] for i in range(len(burst_times))] + def calculate_average_turnaround_time(turnaround_times: list[int]) -> float: """ Calculate the average turnaround time. @@ -63,6 +62,7 @@ def calculate_average_turnaround_time(turnaround_times: list[int]) -> float: """ return sum(turnaround_times) / len(turnaround_times) + def calculate_average_waiting_time(waiting_times: list[int]) -> float: """ Calculate the average waiting time. @@ -70,6 +70,7 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: """ return sum(waiting_times) / len(waiting_times) + if __name__ == "__main__": arrival_times = [0, 1, 2] burst_times = [3, 5, 2]