|
| 1 | +""" |
| 2 | +Shortest Remaining Time First (SRTF) scheduling is a preemptive version of Shortest Job First (SJF). |
| 3 | +At every unit of time, it selects the process with the smallest remaining burst time |
| 4 | +among the processes that have arrived. |
| 5 | +https://en.wikipedia.org/wiki/Shortest_job_next#Preemptive_SJF_(SRTF) |
| 6 | +""" |
| 7 | + |
| 8 | +from statistics import mean |
| 9 | +from typing import List |
| 10 | + |
| 11 | +def calculate_srtf_waiting_time(arrival: List[int], burst: List[int]) -> List[int]: |
| 12 | + """ |
| 13 | + Calculate waiting time for each process using Shortest Remaining Time First (SRTF). |
| 14 | +
|
| 15 | + Args: |
| 16 | + arrival: List of arrival times of processes. |
| 17 | + burst: List of burst times of processes. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + List of waiting times for each process. |
| 21 | +
|
| 22 | + >>> calculate_srtf_waiting_time([0, 1, 2, 3], [8, 4, 9, 5]) |
| 23 | + [9, 0, 15, 2] |
| 24 | + """ |
| 25 | + n = len(arrival) |
| 26 | + remaining = burst.copy() |
| 27 | + waiting = [0] * n |
| 28 | + completed = 0 |
| 29 | + t = 0 |
| 30 | + last_executed = -1 |
| 31 | + completion_time = [0] * n |
| 32 | + |
| 33 | + while completed < n: |
| 34 | + # Find process with smallest remaining time that has arrived |
| 35 | + idx = -1 |
| 36 | + min_remaining = float('inf') |
| 37 | + for i in range(n): |
| 38 | + if arrival[i] <= t and remaining[i] > 0 and remaining[i] < min_remaining: |
| 39 | + min_remaining = remaining[i] |
| 40 | + idx = i |
| 41 | + |
| 42 | + if idx == -1: |
| 43 | + t += 1 # No process ready, advance time |
| 44 | + continue |
| 45 | + |
| 46 | + # Execute process for 1 unit of time |
| 47 | + remaining[idx] -= 1 |
| 48 | + t += 1 |
| 49 | + |
| 50 | + # If process finished, record completion and calculate waiting |
| 51 | + if remaining[idx] == 0: |
| 52 | + completed += 1 |
| 53 | + completion_time[idx] = t |
| 54 | + waiting[idx] = completion_time[idx] - arrival[idx] - burst[idx] |
| 55 | + |
| 56 | + return waiting |
| 57 | + |
| 58 | +def calculate_srtf_turnaround_time(arrival: List[int], burst: List[int], waiting: List[int]) -> List[int]: |
| 59 | + """ |
| 60 | + Calculate turnaround time for each process using waiting time. |
| 61 | +
|
| 62 | + Args: |
| 63 | + arrival: List of arrival times. |
| 64 | + burst: List of burst times. |
| 65 | + waiting: List of waiting times. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + List of turnaround times. |
| 69 | +
|
| 70 | + >>> calculate_srtf_turnaround_time([0,1,2,3],[8,4,9,5],[6,0,12,0]) |
| 71 | + [14, 4, 21, 5] |
| 72 | + """ |
| 73 | + return [burst[i] + waiting[i] for i in range(len(burst))] |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + arrival_time = [0, 1, 2, 3] |
| 77 | + burst_time = [8, 4, 9, 5] |
| 78 | + |
| 79 | + waiting_time = calculate_srtf_waiting_time(arrival_time, burst_time) |
| 80 | + turnaround_time = calculate_srtf_turnaround_time(arrival_time, burst_time, waiting_time) |
| 81 | + |
| 82 | + print("PID\tArrival\tBurst\tWaiting\tTurnaround") |
| 83 | + for i in range(len(arrival_time)): |
| 84 | + print(f"P{i+1}\t{arrival_time[i]}\t{burst_time[i]}\t{waiting_time[i]}\t{turnaround_time[i]}") |
| 85 | + |
| 86 | + print(f"Average Waiting Time: {mean(waiting_time):.2f}") |
| 87 | + print(f"Average Turnaround Time: {mean(turnaround_time):.2f}") |
0 commit comments