-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Add Earliest Deadline First scheduling code #11384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||||||
""" | ||||||||||
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] | ||||||||||
Comment on lines
+75
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
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}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please correct the docstrings or comments if they are incorrect. Is
ticks
correct or is it seconds, hours, months? The nameburst_time
is not obvious so please use the docstrings or comments to explain. Is the overall process time-boxed (e.g. all jobs must be completed in 60 ticks)?