Skip to content

Commit e274863

Browse files
pawanbuddy2000pawanbuddycclauss
authored
Add round_robin scheduling algorithm (TheAlgorithms#2158)
* round_robin and priority cpu scheduling algorithms * Delete priority_cpu_scheduling.py * Delete round_robin_algorithm.py * [add] cpu_scheduling_algorithms * [add] Round robin cpu scheduling algorithm * Update scheduling/round_robin_scheduling_algorithm.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update scheduling/round_robin.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update scheduling/round_robin_scheduling.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update scheduling/round_robin_scheduling.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update scheduling/round_robin.py Co-authored-by: Christian Clauss <cclauss@me.com> * Round_Robin * Update round_robin.py * Update round_robin.py * Update round_robin.py * Update round_robin.py Co-authored-by: pawanbuddy <46370996+pawanbuddy@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 2d3d660 commit e274863

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

scheduling/round_robin.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Round Robin is a scheduling algorithm.
3+
In Round Robin each process is assigned a fixed time slot in a cyclic way.
4+
https://en.wikipedia.org/wiki/Round-robin_scheduling
5+
"""
6+
from statistics import mean
7+
from typing import List
8+
9+
10+
def calculate_waiting_times(burst_times: List[int]) -> List[int]:
11+
"""
12+
Calculate the waiting times of a list of processes that have a specified duration.
13+
14+
Return: The waiting time for each process.
15+
>>> calculate_waiting_times([10, 5, 8])
16+
[13, 10, 13]
17+
>>> calculate_waiting_times([4, 6, 3, 1])
18+
[5, 8, 9, 6]
19+
>>> calculate_waiting_times([12, 2, 10])
20+
[12, 2, 12]
21+
"""
22+
quantum = 2
23+
rem_burst_times = list(burst_times)
24+
waiting_times = [0] * len(burst_times)
25+
t = 0
26+
while 1:
27+
done = True
28+
for i, burst_time in enumerate(burst_times):
29+
if rem_burst_times[i] > 0:
30+
done = False
31+
if rem_burst_times[i] > quantum:
32+
t += quantum
33+
rem_burst_times[i] -= quantum
34+
else:
35+
t += rem_burst_times[i]
36+
waiting_times[i] = t - burst_times[i]
37+
rem_burst_times[i] = 0
38+
if done is True:
39+
return waiting_times
40+
41+
42+
def calculate_turn_around_times(
43+
burst_times: List[int], waiting_times: List[int]
44+
) -> List[int]:
45+
"""
46+
>>> calculate_turn_around_times([1, 2, 3, 4], [0, 1, 3])
47+
[1, 3, 6]
48+
>>> calculate_turn_around_times([10, 3, 7], [10, 6, 11])
49+
[20, 9, 18]
50+
"""
51+
return [burst + waiting for burst, waiting in zip(burst_times, waiting_times)]
52+
53+
54+
if __name__ == "__main__":
55+
burst_times = [3, 5, 7]
56+
waiting_times = calculate_waiting_times(burst_times)
57+
turn_around_times = calculate_turn_around_times(burst_times, waiting_times)
58+
print("Process ID \tBurst Time \tWaiting Time \tTurnaround Time")
59+
for i, burst_time in enumerate(burst_times):
60+
print(
61+
f" {i + 1}\t\t {burst_time}\t\t {waiting_times[i]}\t\t "
62+
f"{turn_around_times[i]}"
63+
)
64+
print(f"\nAverage waiting time = {mean(waiting_times):.5f}")
65+
print(f"Average turn around time = {mean(turn_around_times):.5f}")

0 commit comments

Comments
 (0)