-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add round_robin scheduling algorithm #2158
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
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
210389d
round_robin and priority cpu scheduling algorithms
pawanbuddy2000 ad82bcf
Delete priority_cpu_scheduling.py
pawanbuddy2000 d26a0ad
Delete round_robin_algorithm.py
pawanbuddy2000 7db666d
[add] cpu_scheduling_algorithms
pawanbuddy2000 b585f94
[add] Round robin cpu scheduling algorithm
pawanbuddy 2151f4a
Update scheduling/round_robin_scheduling_algorithm.py
pawanbuddy2000 0e156d6
Update scheduling/round_robin.py
pawanbuddy2000 8df2f10
Update scheduling/round_robin_scheduling.py
pawanbuddy2000 5620bd9
Update scheduling/round_robin_scheduling.py
pawanbuddy2000 2aa35dd
Update scheduling/round_robin.py
pawanbuddy2000 e91cbfb
Round_Robin
pawanbuddy2000 39d017f
Update round_robin.py
cclauss a4244bc
Update round_robin.py
cclauss 277c209
Update round_robin.py
cclauss 5581e58
Update round_robin.py
cclauss 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,102 @@ | ||
""" | ||
Round Robin is a CPU scheduling algorithm. | ||
In Round Robin each process is assigned a fixed time slot in a cyclic way. | ||
https://en.wikipedia.org/wiki/Round-robin_scheduling | ||
""" | ||
|
||
|
||
def calculate_waiting_time(processes, n, burst_time, waiting_time, quantum): | ||
""" | ||
This function calculates the waiting time of some processes that have a | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
specified duration time. | ||
Return: The waiting time for each process. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> calculate_waiting_times([5, 10, 15]) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[0, 5, 15] | ||
>>> calculate_waiting_times([1, 2, 3, 4, 5]) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[0, 1, 3, 6, 10] | ||
>>> calculate_waiting_times([10, 3]) | ||
pawanbuddy2000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[0, 10] | ||
""" | ||
rem_burst_time = [0] * n | ||
for i in range(n): | ||
rem_burst_time[i] = burst_time[i] | ||
t = 0 | ||
while 1: | ||
done = True | ||
for i in range(n): | ||
if rem_burst_time[i] > 0: | ||
done = False | ||
if rem_burst_time[i] > quantum: | ||
t += quantum | ||
rem_burst_time[i] -= quantum | ||
else: | ||
t = t + rem_burst_time[i] | ||
waiting_time[i] = t - burst_time[i] | ||
rem_burst_time[i] = 0 | ||
if done is True: | ||
break | ||
|
||
|
||
def calculate_turn_around_time( | ||
processes, n, burst_time, waiting_time, turn_around_time | ||
): | ||
|
||
""" | ||
This function calculates the turnaround time of some processes. | ||
pawanbuddy2000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Return: The time difference between the completion time and the | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
arrival time. | ||
Practically waiting_time + duration_time | ||
>>> calculate_turnaround_times([5, 10, 15], [0, 5, 15]) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[5, 15, 30] | ||
>>> calculate_turnaround_times([1, 2, 3, 4, 5], [0, 1, 3, 6, 10]) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[1, 3, 6, 10, 15] | ||
>>> calculate_turnaround_times([10, 3], [0, 10]) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[10, 13] | ||
""" | ||
|
||
for i in range(n): | ||
turn_around_time[i] = burst_time[i] + waiting_time[i] | ||
|
||
|
||
def calculate_avg_time(processes, n, burst_time, quantum): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
This function calculates the average of the waiting times | ||
Return: The average of the waiting times. | ||
>>> calculate_average_waiting_time([0, 5, 16]) | ||
7.0 | ||
>>> calculate_average_waiting_time([1, 5, 8, 12]) | ||
6.5 | ||
>>> calculate_average_waiting_time([10, 24]) | ||
17.0 | ||
""" | ||
waiting_time = [0] * n | ||
turn_around_time = [0] * n | ||
calculate_waiting_time(processes, n, burst_time, waiting_time, quantum) | ||
calculate_turn_around_time(processes, n, burst_time, waiting_time, turn_around_time) | ||
print("Processes Burst Time Waiting Time Turn-Around Time") | ||
total_waiting_time = 0 | ||
total_turn_around_time = 0 | ||
for i in range(n): | ||
total_waiting_time = total_waiting_time + waiting_time[i] | ||
total_turn_around_time = total_turn_around_time + turn_around_time[i] | ||
print( | ||
" ", | ||
i + 1, | ||
"\t\t", | ||
burst_time[i], | ||
"\t\t", | ||
waiting_time[i], | ||
"\t\t", | ||
turn_around_time[i], | ||
) | ||
print("\nAverage waiting time = %.5f " % (total_waiting_time / n)) | ||
print("Average turn around time = %.5f " % (total_turn_around_time / n)) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Process id | ||
processes = [1, 2, 3] | ||
n = 3 | ||
burst_time = [10, 5, 8] | ||
quantum = 2 | ||
calculate_avg_time(processes, n, burst_time, quantum) |
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.
Uh oh!
There was an error while loading. Please reload this page.