Skip to content

Commit 5e58b2c

Browse files
Add Shortest Remaining Time First (SRTF) scheduling algorithm
1 parent 788d95b commit 5e58b2c

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
Shortest Remaining Time First (SRTF) Scheduling Algorithm.
3+
SRTF is the preemptive version of Shortest Job First (SJF).
4+
At every moment, the process with the smallest remaining burst time is executed.
5+
https://en.wikipedia.org/wiki/Shortest_remaining_time
6+
"""
7+
8+
from __future__ import annotations
9+
from statistics import mean
10+
11+
12+
def calculate_waiting_times(burst_times: list[int], arrival_times: list[int]) -> list[int]:
13+
"""
14+
Calculate the waiting times of processes using SRTF scheduling.
15+
16+
Args:
17+
burst_times: List of burst times for each process.
18+
arrival_times: List of arrival times for each process.
19+
20+
Returns:
21+
A list containing waiting time for each process.
22+
23+
Examples:
24+
>>> calculate_waiting_times([6, 8, 7, 3], [0, 1, 2, 3])
25+
[9, 15, 10, 0]
26+
>>> calculate_waiting_times([5, 4, 2, 1], [0, 1, 2, 3])
27+
[6, 3, 1, 0]
28+
"""
29+
n = len(burst_times)
30+
remaining_times = burst_times.copy()
31+
waiting_times = [0] * n
32+
complete = 0
33+
t = 0
34+
min_remaining = float("inf")
35+
shortest = 0
36+
check = False
37+
finish_time = 0
38+
39+
while complete != n:
40+
# Find process with minimum remaining time at current time
41+
for j in range(n):
42+
if arrival_times[j] <= t and remaining_times[j] < min_remaining and remaining_times[j] > 0:
43+
min_remaining = remaining_times[j]
44+
shortest = j
45+
check = True
46+
47+
if not check:
48+
t += 1
49+
continue
50+
51+
# Reduce remaining time of current process
52+
remaining_times[shortest] -= 1
53+
min_remaining = remaining_times[shortest]
54+
if min_remaining == 0:
55+
min_remaining = float("inf")
56+
57+
# If a process finishes
58+
if remaining_times[shortest] == 0:
59+
complete += 1
60+
check = False
61+
finish_time = t + 1
62+
waiting_times[shortest] = (
63+
finish_time - burst_times[shortest] - arrival_times[shortest]
64+
)
65+
if waiting_times[shortest] < 0:
66+
waiting_times[shortest] = 0
67+
68+
t += 1
69+
70+
return waiting_times
71+
72+
73+
def calculate_turn_around_times(
74+
burst_times: list[int], waiting_times: list[int]
75+
) -> list[int]:
76+
"""
77+
Calculate turn-around times for each process.
78+
79+
>>> calculate_turn_around_times([6, 8, 7, 3], [9, 15, 10, 0])
80+
[15, 23, 17, 3]
81+
"""
82+
return [burst + waiting for burst, waiting in zip(burst_times, waiting_times)]
83+
84+
85+
if __name__ == "__main__":
86+
burst_times = [6, 8, 7, 3]
87+
arrival_times = [0, 1, 2, 3]
88+
waiting_times = calculate_waiting_times(burst_times, arrival_times)
89+
turn_around_times = calculate_turn_around_times(burst_times, waiting_times)
90+
91+
print("Process ID \tArrival Time \tBurst Time \tWaiting Time \tTurnaround Time")
92+
for i, burst_time in enumerate(burst_times):
93+
print(
94+
f" {i + 1}\t\t {arrival_times[i]}\t\t {burst_time}\t\t {waiting_times[i]}\t\t {turn_around_times[i]}"
95+
)
96+
print(f"\nAverage waiting time = {mean(waiting_times):.5f}")
97+
print(f"Average turn around time = {mean(turn_around_times):.5f}")
98+

0 commit comments

Comments
 (0)