33Shortest Job First (SJF).
44At every unit of time, it selects the process with the smallest remaining
55burst time among the processes that have arrived.
6+
67https://en.wikipedia.org/wiki/Shortest_job_next#Preemptive_SJF_(SRTF)
78"""
89
9- def mean(a):
10- return sum(a) / len(a)
10+ from __future__ import annotations
11+
12+
13+ def mean (values : list [int ]) -> float :
14+ """
15+ Return arithmetic mean of a list of numbers.
16+
17+ >>> mean([2, 4, 6, 8])
18+ 5.0
19+ """
20+ return sum (values ) / len (values )
21+
1122
1223def calculate_srtf_waiting_time (arrival : list [int ], burst : list [int ]) -> list [int ]:
1324 """
14- Calculate waiting time for each process using Shortest Remaining Time First (SRTF) .
25+ Calculate waiting time for each process using Shortest Remaining Time First.
1526
1627 Args:
1728 arrival: List of arrival times of processes.
@@ -28,13 +39,12 @@ def calculate_srtf_waiting_time(arrival: list[int], burst: list[int]) -> list[in
2839 waiting = [0 ] * n
2940 completed = 0
3041 t = 0
31- last_executed = -1
3242 completion_time = [0 ] * n
3343
3444 while completed < n :
3545 # Find process with smallest remaining time that has arrived
3646 idx = - 1
37- min_remaining = float(' inf' )
47+ min_remaining = float (" inf" )
3848 for i in range (n ):
3949 if arrival [i ] <= t and remaining [i ] > 0 and remaining [i ] < min_remaining :
4050 min_remaining = remaining [i ]
@@ -56,33 +66,37 @@ def calculate_srtf_waiting_time(arrival: list[int], burst: list[int]) -> list[in
5666
5767 return waiting
5868
59- def calculate_srtf_turnaround_time(arrival: list[int], burst: list[int], waiting: list[int]) -> list[int]:
69+
70+ def calculate_srtf_turnaround_time (burst : list [int ], waiting : list [int ]) -> list [int ]:
6071 """
6172 Calculate turnaround time for each process using waiting time.
6273
6374 Args:
64- arrival: List of arrival times.
6575 burst: List of burst times.
6676 waiting: List of waiting times.
6777
6878 Returns:
6979 List of turnaround times.
7080
71- >>> calculate_srtf_turnaround_time([0,1,2,3],[8,4,9, 5],[6,0,12,0 ])
72- [14 , 4, 21, 5 ]
81+ >>> calculate_srtf_turnaround_time([8, 4, 9, 5], [9, 0, 15, 2 ])
82+ [17 , 4, 24, 7 ]
7383 """
7484 return [burst [i ] + waiting [i ] for i in range (len (burst ))]
7585
86+
7687if __name__ == "__main__" :
7788 arrival_time = [0 , 1 , 2 , 3 ]
7889 burst_time = [8 , 4 , 9 , 5 ]
7990
8091 waiting_time = calculate_srtf_waiting_time (arrival_time , burst_time )
81- turnaround_time = calculate_srtf_turnaround_time(arrival_time, burst_time, waiting_time)
92+ turnaround_time = calculate_srtf_turnaround_time (burst_time , waiting_time )
8293
8394 print ("PID\t Arrival\t Burst\t Waiting\t Turnaround" )
8495 for i in range (len (arrival_time )):
85- print(f"P{i+1}\t{arrival_time[i]}\t{burst_time[i]}\t{waiting_time[i]}\t{turnaround_time[i]}")
96+ print (
97+ f"P{ i + 1 } \t { arrival_time [i ]} \t { burst_time [i ]} \t "
98+ f"{ waiting_time [i ]} \t { turnaround_time [i ]} "
99+ )
86100
87101 print (f"Average Waiting Time: { mean (waiting_time ):.2f} " )
88102 print (f"Average Turnaround Time: { mean (turnaround_time ):.2f} " )
0 commit comments