-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create non_preemptive_shortest_job_first.py #6169
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
Merged
poyea
merged 8 commits into
TheAlgorithms:master
from
Open-Source-Contribute:non_preemptive_shortest_job_first
Jun 6, 2022
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
49029dd
Create non_preemptive_shortest_job_first.py
Bynnn 51ef0ed
파일 위치 변경
Bynnn 8a2cbc5
Delete non_preemptive_shortest_job_first.py
Bynnn e36c13f
delete Korean comments
Bynnn b49da78
Merge branch 'non_preemptive_shortest_job_first' of https://github.co…
Bynnn 698c74d
change comments, & to and, type annotation
Bynnn 65fdf4c
type annotation
Bynnn d18a6cc
delete unnecessary comment
Bynnn 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 hidden or 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,113 @@ | ||
""" | ||
Non-preemptive Shortest Job First | ||
Shortest execution time process is chosen for the next execution. | ||
https://www.guru99.com/shortest-job-first-sjf-scheduling.html | ||
https://en.wikipedia.org/wiki/Shortest_job_next | ||
""" | ||
|
||
|
||
from __future__ import annotations | ||
|
||
from statistics import mean | ||
|
||
"from typing import List" | ||
Bynnn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def calculate_waitingtime( | ||
arrival_time: list[int], burst_time: list[int], no_of_processes: int | ||
) -> list[int]: | ||
""" | ||
Calculate the waiting time of each processes | ||
|
||
Return: The waiting time for each process. | ||
>>> calculate_waitingtime([0,1,2], [10, 5, 8], 3) | ||
[0, 9, 13] | ||
>>> calculate_waitingtime([1,2,2,4], [4, 6, 3, 1], 4) | ||
[0, 7, 4, 1] | ||
>>> calculate_waitingtime([0,0,0], [12, 2, 10],3) | ||
[12, 0, 2] | ||
""" | ||
|
||
waiting_time = [0] * no_of_processes | ||
remaining_time = [0] * no_of_processes | ||
|
||
# Initialize remaining_time to waiting_time. | ||
|
||
for i in range(no_of_processes): | ||
remaining_time[i] = burst_time[i] | ||
ready_process: list[int] = [] | ||
|
||
completed = 0 | ||
total_time = 0 | ||
|
||
# When processes are not completed, | ||
# A process whose arrival time has passed \ | ||
# and has remaining execution time is put into the ready_process. | ||
# The shortest process in the ready_process, target_process is executed. | ||
|
||
while completed != no_of_processes: | ||
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. This block of code can be used to improve https://github.com/TheAlgorithms/Python/blob/master/scheduling/shortest_job_first.py |
||
ready_process = [] | ||
target_process = -1 | ||
|
||
for i in range(no_of_processes): | ||
if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): | ||
ready_process.append(i) | ||
|
||
if len(ready_process) > 0: | ||
target_process = ready_process[0] | ||
for i in ready_process: | ||
if remaining_time[i] < remaining_time[target_process]: | ||
target_process = i | ||
total_time += burst_time[target_process] | ||
completed += 1 | ||
remaining_time[target_process] = 0 | ||
waiting_time[target_process] = ( | ||
total_time - arrival_time[target_process] - burst_time[target_process] | ||
) | ||
else: | ||
total_time += 1 | ||
|
||
return waiting_time | ||
|
||
|
||
def calculate_turnaroundtime( | ||
burst_time: list[int], no_of_processes: int, waiting_time: list[int] | ||
) -> list[int]: | ||
""" | ||
Calculate the turnaround time of each process. | ||
|
||
Return: The turnaround time for each process. | ||
>>> calculate_turnaroundtime([0,1,2], 3, [0, 10, 15]) | ||
[0, 11, 17] | ||
>>> calculate_turnaroundtime([1,2,2,4], 4, [1, 8, 5, 4]) | ||
[2, 10, 7, 8] | ||
>>> calculate_turnaroundtime([0,0,0], 3, [12, 0, 2]) | ||
[12, 0, 2] | ||
""" | ||
|
||
turn_around_time = [0] * no_of_processes | ||
for i in range(no_of_processes): | ||
turn_around_time[i] = burst_time[i] + waiting_time[i] | ||
return turn_around_time | ||
|
||
|
||
if __name__ == "__main__": | ||
print("[TEST CASE 01]") | ||
|
||
no_of_processes = 4 | ||
burst_time = [2, 5, 3, 7] | ||
arrival_time = [0, 0, 0, 0] | ||
waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes) | ||
turn_around_time = calculate_turnaroundtime( | ||
burst_time, no_of_processes, waiting_time | ||
) | ||
|
||
# Printing the Result | ||
print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") | ||
for i, process_ID in enumerate(list(range(1, 5))): | ||
print( | ||
f"{process_ID}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" | ||
f"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}" | ||
) | ||
print(f"\nAverage waiting time = {mean(waiting_time):.5f}") | ||
print(f"Average turnaround time = {mean(turn_around_time):.5f}") |
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.
I think SJF is non-preemptive itself? So I think this program is similar to https://github.com/TheAlgorithms/Python/blob/master/scheduling/shortest_job_first.py
so we can get rid of
non_preemptive_
from filenameThere 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.
The SJF is a non-preemptive scheduling algorithm.
I should name mine without
non_preemptive_
, but there is the same name but different algorithm. (what you mentioned)The existing program is a preemptive variant of SJF.
So I add
non_preemptive_
to distinguish them.If we get rid of
non_preemptive_
, I think the other filename(originalshortest_job_first.py
) also should be renamed toshortest_remaining_time.py
.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.
Ah ok, you may rename
scheduling/shortest_job_first.py
toscheduling/preemptive_shortest_job_first.py