forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highest_response_ratio_next.py
119 lines (99 loc) · 3.89 KB
/
highest_response_ratio_next.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
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
"""
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
) -> 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
current_time = max(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(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, # noqa: ARG001
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(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(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}")