From 26490de883e53739a21c448cb652a9eca19c53e6 Mon Sep 17 00:00:00 2001 From: Han Date: Tue, 7 Jun 2022 15:39:15 +0900 Subject: [PATCH 1/3] add highest_response_ratio_next.py --- scheduling/highest_response_ratio_next.py | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 scheduling/highest_response_ratio_next.py diff --git a/scheduling/highest_response_ratio_next.py b/scheduling/highest_response_ratio_next.py new file mode 100644 index 000000000000..aaa120235d55 --- /dev/null +++ b/scheduling/highest_response_ratio_next.py @@ -0,0 +1,113 @@ +""" +Highest response ratio next (HRRN) scheduling is a non-preemptive discipline. +It was developed as modification of shortest job next or shortest job first (SJN or SJF) +to mitigate the problem of process starvation. +https://en.wikipedia.org/wiki/Highest_response_ratio_next +""" +import numpy as np +from statistics import mean + + +def calculate_turn_around_time( + process_name: list, arrival_time: list, burst_time: list, no_of_process: int +) -> list: + """ + Calculate the turn around time of each processes + + Return: The turn around time time for each process. + >>> calculate_turn_around_time(['A', 'B', 'C'], [3, 5, 8], [2, 4, 6], 3) + [2, 4, 7] + >>> calculate_turn_around_time(['A', 'B', 'C'], [0, 2, 4], [3, 5, 7], 3) + [3, 6, 11] + """ + + current_time = 0 + # Number of processes finished + finished_process_count = 0 + # Displays the finished process. + # If it is 0, the performance is completed if it is 1, before the performance. + finished_process = [0] * no_of_process + # List to include calculation results + turn_around_time = [0] * no_of_process + + # Sort by arrival time. + burst_time = [burst_time[i] for i in np.argsort(arrival_time)] + process_name = [process_name[i] for i in np.argsort(arrival_time)] + arrival_time.sort() + + while no_of_process > finished_process_count: + + """ + If the current time is less than the arrival time of + the process that arrives first among the processes that have not been performed, + change the current time. + """ + i = 0 + while(finished_process[i] == 1): + i += 1 + if current_time < arrival_time[i]: + current_time = arrival_time[i] + + response_ratio = 0 + # Index showing the location of the process being performed + loc = 0 + # Saves the current response ratio. + temp = 0 + for i in range(0, no_of_process): + if finished_process[i] == 0 and arrival_time[i] <= current_time: + temp = (burst_time[i] + (current_time - arrival_time[i])) /\ + burst_time[i] + if response_ratio < temp: + response_ratio = temp + loc = i + + # Calculate the turn around time + turn_around_time[loc] = current_time + burst_time[loc] - arrival_time[loc] + current_time += burst_time[loc] + # Indicates that the process has been performed. + finished_process[loc] = 1 + # Increase finished_process_count by 1 + finished_process_count += 1 + + return turn_around_time + + +def calculate_waiting_time( + process_name: list, turn_around_time: list, burst_time: list, no_of_process: int +) -> list: + """ + Calculate the waiting time of each processes. + + Return: The waiting time for each process. + >>> calculate_waiting_time(['A', 'B', 'C'], [2, 4, 7], [2, 4, 6], 3) + [0, 0, 1] + >>> calculate_waiting_time(['A', 'B', 'C'], [3, 6, 11], [3, 5, 7], 3) + [0, 1, 4] + """ + + waiting_time = [0] * no_of_process + for i in range(0, no_of_process): + waiting_time[i] = turn_around_time[i] - burst_time[i] + return waiting_time + + +if __name__ == "__main__": + + no_of_process = 5 + process_name = ['A', 'B', 'C', 'D', 'E'] + arrival_time = [1, 2, 3, 4, 5] + burst_time = [1, 2, 3, 4, 5] + + turn_around_time = calculate_turn_around_time( + process_name, arrival_time, burst_time, no_of_process) + waiting_time = calculate_waiting_time( + process_name, turn_around_time, burst_time, no_of_process) + + print("Process name \tArrival time \tBurst time \tTurn around time \tWaiting time") + for i in range(0, no_of_process): + print( + f"{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t" + f"{turn_around_time[i]}\t\t\t{waiting_time[i]}") + + print(f"average waiting time : {mean(waiting_time):.5f}") + print(f"average turn around time : {mean(turn_around_time):.5f}") From ecb5b8546113fb42be5ab88912a9c79daa8efa4e Mon Sep 17 00:00:00 2001 From: yulmam <70622601+yulmam@users.noreply.github.com> Date: Sun, 12 Jun 2022 08:34:41 +0900 Subject: [PATCH 2/3] Update highest_response_ratio_next.py --- scheduling/highest_response_ratio_next.py | 30 ++++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/scheduling/highest_response_ratio_next.py b/scheduling/highest_response_ratio_next.py index aaa120235d55..81d200122d1c 100644 --- a/scheduling/highest_response_ratio_next.py +++ b/scheduling/highest_response_ratio_next.py @@ -4,9 +4,10 @@ to mitigate the problem of process starvation. https://en.wikipedia.org/wiki/Highest_response_ratio_next """ -import numpy as np from statistics import mean +import numpy as np + def calculate_turn_around_time( process_name: list, arrival_time: list, burst_time: list, no_of_process: int @@ -15,9 +16,9 @@ def calculate_turn_around_time( Calculate the turn around time of each processes Return: The turn around time time for each process. - >>> calculate_turn_around_time(['A', 'B', 'C'], [3, 5, 8], [2, 4, 6], 3) + >>> calculate_turn_around_time(["A", "B", "C"], [3, 5, 8], [2, 4, 6], 3) [2, 4, 7] - >>> calculate_turn_around_time(['A', 'B', 'C'], [0, 2, 4], [3, 5, 7], 3) + >>> calculate_turn_around_time(["A", "B", "C"], [0, 2, 4], [3, 5, 7], 3) [3, 6, 11] """ @@ -43,7 +44,7 @@ def calculate_turn_around_time( change the current time. """ i = 0 - while(finished_process[i] == 1): + while finished_process[i] == 1: i += 1 if current_time < arrival_time[i]: current_time = arrival_time[i] @@ -55,8 +56,9 @@ def calculate_turn_around_time( temp = 0 for i in range(0, no_of_process): if finished_process[i] == 0 and arrival_time[i] <= current_time: - temp = (burst_time[i] + (current_time - arrival_time[i])) /\ - burst_time[i] + temp = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[ + i + ] if response_ratio < temp: response_ratio = temp loc = i @@ -79,9 +81,9 @@ def calculate_waiting_time( Calculate the waiting time of each processes. Return: The waiting time for each process. - >>> calculate_waiting_time(['A', 'B', 'C'], [2, 4, 7], [2, 4, 6], 3) + >>> calculate_waiting_time(["A", "B", "C"], [2, 4, 7], [2, 4, 6], 3) [0, 0, 1] - >>> calculate_waiting_time(['A', 'B', 'C'], [3, 6, 11], [3, 5, 7], 3) + >>> calculate_waiting_time(["A", "B", "C"], [3, 6, 11], [3, 5, 7], 3) [0, 1, 4] """ @@ -94,20 +96,24 @@ def calculate_waiting_time( if __name__ == "__main__": no_of_process = 5 - process_name = ['A', 'B', 'C', 'D', 'E'] + process_name = ["A", "B", "C", "D", "E"] arrival_time = [1, 2, 3, 4, 5] burst_time = [1, 2, 3, 4, 5] turn_around_time = calculate_turn_around_time( - process_name, arrival_time, burst_time, no_of_process) + process_name, arrival_time, burst_time, no_of_process + ) waiting_time = calculate_waiting_time( - process_name, turn_around_time, burst_time, no_of_process) + process_name, turn_around_time, burst_time, no_of_process + ) print("Process name \tArrival time \tBurst time \tTurn around time \tWaiting time") for i in range(0, no_of_process): print( f"{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t" - f"{turn_around_time[i]}\t\t\t{waiting_time[i]}") + f"{turn_around_time[i]}\t\t\t{waiting_time[i]}" + ) print(f"average waiting time : {mean(waiting_time):.5f}") print(f"average turn around time : {mean(turn_around_time):.5f}") + From bd6e00ac64099490c30767823e2536bc6bd7ec9f Mon Sep 17 00:00:00 2001 From: yulmam <70622601+yulmam@users.noreply.github.com> Date: Sun, 12 Jun 2022 08:36:13 +0900 Subject: [PATCH 3/3] Update highest_response_ratio_next.py --- scheduling/highest_response_ratio_next.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scheduling/highest_response_ratio_next.py b/scheduling/highest_response_ratio_next.py index 81d200122d1c..a5c62ddbe952 100644 --- a/scheduling/highest_response_ratio_next.py +++ b/scheduling/highest_response_ratio_next.py @@ -116,4 +116,3 @@ def calculate_waiting_time( print(f"average waiting time : {mean(waiting_time):.5f}") print(f"average turn around time : {mean(turn_around_time):.5f}") -