|
| 1 | +""" |
| 2 | +Non-preemptive Shortest Job First |
| 3 | +Shortest execution time process is chosen for the next execution. |
| 4 | +https://www.guru99.com/shortest-job-first-sjf-scheduling.html |
| 5 | +https://en.wikipedia.org/wiki/Shortest_job_next |
| 6 | +""" |
| 7 | + |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from statistics import mean |
| 12 | + |
| 13 | + |
| 14 | +def calculate_waitingtime( |
| 15 | + arrival_time: list[int], burst_time: list[int], no_of_processes: int |
| 16 | +) -> list[int]: |
| 17 | + """ |
| 18 | + Calculate the waiting time of each processes |
| 19 | +
|
| 20 | + Return: The waiting time for each process. |
| 21 | + >>> calculate_waitingtime([0,1,2], [10, 5, 8], 3) |
| 22 | + [0, 9, 13] |
| 23 | + >>> calculate_waitingtime([1,2,2,4], [4, 6, 3, 1], 4) |
| 24 | + [0, 7, 4, 1] |
| 25 | + >>> calculate_waitingtime([0,0,0], [12, 2, 10],3) |
| 26 | + [12, 0, 2] |
| 27 | + """ |
| 28 | + |
| 29 | + waiting_time = [0] * no_of_processes |
| 30 | + remaining_time = [0] * no_of_processes |
| 31 | + |
| 32 | + # Initialize remaining_time to waiting_time. |
| 33 | + |
| 34 | + for i in range(no_of_processes): |
| 35 | + remaining_time[i] = burst_time[i] |
| 36 | + ready_process: list[int] = [] |
| 37 | + |
| 38 | + completed = 0 |
| 39 | + total_time = 0 |
| 40 | + |
| 41 | + # When processes are not completed, |
| 42 | + # A process whose arrival time has passed \ |
| 43 | + # and has remaining execution time is put into the ready_process. |
| 44 | + # The shortest process in the ready_process, target_process is executed. |
| 45 | + |
| 46 | + while completed != no_of_processes: |
| 47 | + ready_process = [] |
| 48 | + target_process = -1 |
| 49 | + |
| 50 | + for i in range(no_of_processes): |
| 51 | + if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): |
| 52 | + ready_process.append(i) |
| 53 | + |
| 54 | + if len(ready_process) > 0: |
| 55 | + target_process = ready_process[0] |
| 56 | + for i in ready_process: |
| 57 | + if remaining_time[i] < remaining_time[target_process]: |
| 58 | + target_process = i |
| 59 | + total_time += burst_time[target_process] |
| 60 | + completed += 1 |
| 61 | + remaining_time[target_process] = 0 |
| 62 | + waiting_time[target_process] = ( |
| 63 | + total_time - arrival_time[target_process] - burst_time[target_process] |
| 64 | + ) |
| 65 | + else: |
| 66 | + total_time += 1 |
| 67 | + |
| 68 | + return waiting_time |
| 69 | + |
| 70 | + |
| 71 | +def calculate_turnaroundtime( |
| 72 | + burst_time: list[int], no_of_processes: int, waiting_time: list[int] |
| 73 | +) -> list[int]: |
| 74 | + """ |
| 75 | + Calculate the turnaround time of each process. |
| 76 | +
|
| 77 | + Return: The turnaround time for each process. |
| 78 | + >>> calculate_turnaroundtime([0,1,2], 3, [0, 10, 15]) |
| 79 | + [0, 11, 17] |
| 80 | + >>> calculate_turnaroundtime([1,2,2,4], 4, [1, 8, 5, 4]) |
| 81 | + [2, 10, 7, 8] |
| 82 | + >>> calculate_turnaroundtime([0,0,0], 3, [12, 0, 2]) |
| 83 | + [12, 0, 2] |
| 84 | + """ |
| 85 | + |
| 86 | + turn_around_time = [0] * no_of_processes |
| 87 | + for i in range(no_of_processes): |
| 88 | + turn_around_time[i] = burst_time[i] + waiting_time[i] |
| 89 | + return turn_around_time |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + print("[TEST CASE 01]") |
| 94 | + |
| 95 | + no_of_processes = 4 |
| 96 | + burst_time = [2, 5, 3, 7] |
| 97 | + arrival_time = [0, 0, 0, 0] |
| 98 | + waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes) |
| 99 | + turn_around_time = calculate_turnaroundtime( |
| 100 | + burst_time, no_of_processes, waiting_time |
| 101 | + ) |
| 102 | + |
| 103 | + # Printing the Result |
| 104 | + print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") |
| 105 | + for i, process_ID in enumerate(list(range(1, 5))): |
| 106 | + print( |
| 107 | + f"{process_ID}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" |
| 108 | + f"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}" |
| 109 | + ) |
| 110 | + print(f"\nAverage waiting time = {mean(waiting_time):.5f}") |
| 111 | + print(f"Average turnaround time = {mean(turn_around_time):.5f}") |
0 commit comments