From 49029dd98d4efdac87970ae5fbe8e8b383c2a995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=B9=88?= <76545238+Bynnn@users.noreply.github.com> Date: Tue, 24 May 2022 15:38:48 +0900 Subject: [PATCH 1/7] Create non_preemptive_shortest_job_first.py --- non_preemptive_shortest_job_first.py | 128 +++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 non_preemptive_shortest_job_first.py diff --git a/non_preemptive_shortest_job_first.py b/non_preemptive_shortest_job_first.py new file mode 100644 index 000000000000..e0dbf33cf4a3 --- /dev/null +++ b/non_preemptive_shortest_job_first.py @@ -0,0 +1,128 @@ +# SJF +""" +Non-preemptive Shortest Job First +Shortest execution time process is chosen for the next execution. +https://www.guru99.com/shortest-job-first-sjf-scheduling.html +https://en.wikipedia.org/wiki/Shortest_job_next +""" + +from __future__ import annotations + +from statistics import mean + + +def calculate_waitingtime( + arrival_time: list[int], burst_time: list[int], no_of_processes: int +) -> list[int]: + # Arrival_time : 도착 시간을 담은 리스트 + # Burst_time : 수행시간 + # No_of_processes : 프로세스의 개수, the number of processes + + """ + Calculate the waiting time of each processes + + Return: The waiting time for each process. + >>> calculate_waitingtime([0,1,2], [10, 5, 8], 3) + [0, 9, 13] + >>> calculate_waitingtime([1,2,2,4], [4, 6, 3, 1], 4) + [0, 7, 4, 1] + >>> calculate_waitingtime([0,0,0], [12, 2, 10],3) + [12, 0, 2] + """ + + # waiting_time : 대기 시간 + # remaining_time : 남은 수행 시간 + waiting_time = [0] * no_of_processes + remaining_time = [0] * no_of_processes + + """ + Initialize remaining_time to waiting_time. + 남은 수행 시간을 수행 시간으로 초기화한다. + """ + for i in range(no_of_processes): + remaining_time[i] = burst_time[i] + ready_process = [] + + # completed : 수행 완료된 프로세스의 수, The number of processes completed + # total_time : 프로세스와 무관하게 진행되는 시간 + completed = 0 + total_time = 0 + + """ + 수행할 프로세스가 남아 있을 때, + 도착 시간이 지났고 남아 있는 수행 시간이 있는 프로세스는 ready_process에 들어간다. + ready_process에서 가장 짧은 프로세스인 target_process가 실행된다. + + When processes are not completed, + A process whose arrival time has passed \ + and has remaining execution time is put into the ready_process. + The shortest process in the ready_process, target_process is executed. + """ + while completed != no_of_processes: + ready_process = [] + target_process = -1 + + for i in range(no_of_processes): + if (arrival_time[i] <= total_time) & (remaining_time[i] > 0): + ready_process.append(i) + + if len(ready_process) > 0: + target_process = ready_process[0] + for i in ready_process: + if remaining_time[i] < remaining_time[target_process]: + target_process = i + total_time += burst_time[target_process] + completed += 1 + remaining_time[target_process] = 0 + waiting_time[target_process] = \ + total_time - arrival_time[target_process]\ + - burst_time[target_process] + else: + total_time += 1 + + return waiting_time + + +def calculate_turnaroundtime( + burst_time: list[int], no_of_processes: int, waiting_time: list[int] +) -> list[int]: + """ + Calculate the turnaround time of each Processes + + Return: The turnaround time for each process. + >>> calculate_turnaroundtime([0,1,2], 3, [0, 10, 15]) + [0, 11, 17] + >>> calculate_turnaroundtime([1,2,2,4], 4, [1, 8, 5, 4]) + [2, 10, 7, 8] + >>> calculate_turnaroundtime([0,0,0], 3, [12, 0, 2]) + [12, 0, 2] + """ + + turn_around_time = [0] * no_of_processes + for i in range(no_of_processes): + turn_around_time[i] = burst_time[i] \ + + waiting_time[i] + return turn_around_time + + +if __name__ == "__main__": + print("[TEST CASE 01]") + + no_of_processes = 4 + burst_time = [2, 5, 3, 7] + arrival_time = [0, 0, 0, 0] + processes = list(range(1, 5)) + waiting_time = calculate_waitingtime(arrival_time, + burst_time, no_of_processes) + turn_around_time = calculate_turnaroundtime(burst_time, + no_of_processes, waiting_time) + + # Printing the Result + print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") + for i, processes in enumerate(processes): + print( + f"{processes}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" + f"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}" + ) + print(f"\nAverage waiting time = {mean(waiting_time):.5f}") + print(f"Average turnaround time = {mean(turn_around_time):.5f}") From 51ef0ed80d058f36d3b71cf558f972ad29ce68dc Mon Sep 17 00:00:00 2001 From: BYN Date: Wed, 25 May 2022 17:21:04 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9C=84=EC=B9=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../non_preemptive_shortest_job_first.py | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 scheduling/non_preemptive_shortest_job_first.py diff --git a/scheduling/non_preemptive_shortest_job_first.py b/scheduling/non_preemptive_shortest_job_first.py new file mode 100644 index 000000000000..e07c55f4009c --- /dev/null +++ b/scheduling/non_preemptive_shortest_job_first.py @@ -0,0 +1,129 @@ +# SJF +""" +Non-preemptive Shortest Job First +Shortest execution time process is chosen for the next execution. +https://www.guru99.com/shortest-job-first-sjf-scheduling.html +https://en.wikipedia.org/wiki/Shortest_job_next +""" + + +from __future__ import annotations + +from statistics import mean + + +def calculate_waitingtime( + arrival_time: list[int], burst_time: list[int], no_of_processes: int +) -> list[int]: + # Arrival_time : 도착 시간을 담은 리스트 + # Burst_time : 수행시간 + # No_of_processes : 프로세스의 개수, the number of processes + + """ + Calculate the waiting time of each processes + + Return: The waiting time for each process. + >>> calculate_waitingtime([0,1,2], [10, 5, 8], 3) + [0, 9, 13] + >>> calculate_waitingtime([1,2,2,4], [4, 6, 3, 1], 4) + [0, 7, 4, 1] + >>> calculate_waitingtime([0,0,0], [12, 2, 10],3) + [12, 0, 2] + """ + + # waiting_time : 대기 시간 + # remaining_time : 남은 수행 시간 + waiting_time = [0] * no_of_processes + remaining_time = [0] * no_of_processes + + """ + Initialize remaining_time to waiting_time. + 남은 수행 시간을 수행 시간으로 초기화한다. + """ + for i in range(no_of_processes): + remaining_time[i] = burst_time[i] + ready_process = [] + + # completed : 수행 완료된 프로세스의 수, The number of processes completed + # total_time : 프로세스와 무관하게 진행되는 시간 + completed = 0 + total_time = 0 + + """ + 수행할 프로세스가 남아 있을 때, + 도착 시간이 지났고 남아 있는 수행 시간이 있는 프로세스는 ready_process에 들어간다. + ready_process에서 가장 짧은 프로세스인 target_process가 실행된다. + + When processes are not completed, + A process whose arrival time has passed \ + and has remaining execution time is put into the ready_process. + The shortest process in the ready_process, target_process is executed. + """ + while completed != no_of_processes: + ready_process = [] + target_process = -1 + + for i in range(no_of_processes): + if (arrival_time[i] <= total_time) & (remaining_time[i] > 0): + ready_process.append(i) + + if len(ready_process) > 0: + target_process = ready_process[0] + for i in ready_process: + if remaining_time[i] < remaining_time[target_process]: + target_process = i + total_time += burst_time[target_process] + completed += 1 + remaining_time[target_process] = 0 + waiting_time[target_process] = \ + total_time - arrival_time[target_process]\ + - burst_time[target_process] + else: + total_time += 1 + + return waiting_time + + +def calculate_turnaroundtime( + burst_time: list[int], no_of_processes: int, waiting_time: list[int] +) -> list[int]: + """ + Calculate the turnaround time of each Processes + + Return: The turnaround time for each process. + >>> calculate_turnaroundtime([0,1,2], 3, [0, 10, 15]) + [0, 11, 17] + >>> calculate_turnaroundtime([1,2,2,4], 4, [1, 8, 5, 4]) + [2, 10, 7, 8] + >>> calculate_turnaroundtime([0,0,0], 3, [12, 0, 2]) + [12, 0, 2] + """ + + turn_around_time = [0] * no_of_processes + for i in range(no_of_processes): + turn_around_time[i] = burst_time[i] \ + + waiting_time[i] + return turn_around_time + + +if __name__ == "__main__": + print("[TEST CASE 01]") + + no_of_processes = 4 + burst_time = [2, 5, 3, 7] + arrival_time = [0, 0, 0, 0] + processes = list(range(1, 5)) + waiting_time = calculate_waitingtime(arrival_time, + burst_time, no_of_processes) + turn_around_time = calculate_turnaroundtime(burst_time, + no_of_processes, waiting_time) + + # Printing the Result + print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") + for i, processes in enumerate(processes): + print( + f"{processes}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" + f"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}" + ) + print(f"\nAverage waiting time = {mean(waiting_time):.5f}") + print(f"Average turnaround time = {mean(turn_around_time):.5f}") From 8a2cbc5ef2c34e9b2f8fc6f69fe17f402fe36688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=B9=88?= <76545238+Bynnn@users.noreply.github.com> Date: Wed, 25 May 2022 17:22:20 +0900 Subject: [PATCH 3/7] Delete non_preemptive_shortest_job_first.py --- non_preemptive_shortest_job_first.py | 128 --------------------------- 1 file changed, 128 deletions(-) delete mode 100644 non_preemptive_shortest_job_first.py diff --git a/non_preemptive_shortest_job_first.py b/non_preemptive_shortest_job_first.py deleted file mode 100644 index e0dbf33cf4a3..000000000000 --- a/non_preemptive_shortest_job_first.py +++ /dev/null @@ -1,128 +0,0 @@ -# SJF -""" -Non-preemptive Shortest Job First -Shortest execution time process is chosen for the next execution. -https://www.guru99.com/shortest-job-first-sjf-scheduling.html -https://en.wikipedia.org/wiki/Shortest_job_next -""" - -from __future__ import annotations - -from statistics import mean - - -def calculate_waitingtime( - arrival_time: list[int], burst_time: list[int], no_of_processes: int -) -> list[int]: - # Arrival_time : 도착 시간을 담은 리스트 - # Burst_time : 수행시간 - # No_of_processes : 프로세스의 개수, the number of processes - - """ - Calculate the waiting time of each processes - - Return: The waiting time for each process. - >>> calculate_waitingtime([0,1,2], [10, 5, 8], 3) - [0, 9, 13] - >>> calculate_waitingtime([1,2,2,4], [4, 6, 3, 1], 4) - [0, 7, 4, 1] - >>> calculate_waitingtime([0,0,0], [12, 2, 10],3) - [12, 0, 2] - """ - - # waiting_time : 대기 시간 - # remaining_time : 남은 수행 시간 - waiting_time = [0] * no_of_processes - remaining_time = [0] * no_of_processes - - """ - Initialize remaining_time to waiting_time. - 남은 수행 시간을 수행 시간으로 초기화한다. - """ - for i in range(no_of_processes): - remaining_time[i] = burst_time[i] - ready_process = [] - - # completed : 수행 완료된 프로세스의 수, The number of processes completed - # total_time : 프로세스와 무관하게 진행되는 시간 - completed = 0 - total_time = 0 - - """ - 수행할 프로세스가 남아 있을 때, - 도착 시간이 지났고 남아 있는 수행 시간이 있는 프로세스는 ready_process에 들어간다. - ready_process에서 가장 짧은 프로세스인 target_process가 실행된다. - - When processes are not completed, - A process whose arrival time has passed \ - and has remaining execution time is put into the ready_process. - The shortest process in the ready_process, target_process is executed. - """ - while completed != no_of_processes: - ready_process = [] - target_process = -1 - - for i in range(no_of_processes): - if (arrival_time[i] <= total_time) & (remaining_time[i] > 0): - ready_process.append(i) - - if len(ready_process) > 0: - target_process = ready_process[0] - for i in ready_process: - if remaining_time[i] < remaining_time[target_process]: - target_process = i - total_time += burst_time[target_process] - completed += 1 - remaining_time[target_process] = 0 - waiting_time[target_process] = \ - total_time - arrival_time[target_process]\ - - burst_time[target_process] - else: - total_time += 1 - - return waiting_time - - -def calculate_turnaroundtime( - burst_time: list[int], no_of_processes: int, waiting_time: list[int] -) -> list[int]: - """ - Calculate the turnaround time of each Processes - - Return: The turnaround time for each process. - >>> calculate_turnaroundtime([0,1,2], 3, [0, 10, 15]) - [0, 11, 17] - >>> calculate_turnaroundtime([1,2,2,4], 4, [1, 8, 5, 4]) - [2, 10, 7, 8] - >>> calculate_turnaroundtime([0,0,0], 3, [12, 0, 2]) - [12, 0, 2] - """ - - turn_around_time = [0] * no_of_processes - for i in range(no_of_processes): - turn_around_time[i] = burst_time[i] \ - + waiting_time[i] - return turn_around_time - - -if __name__ == "__main__": - print("[TEST CASE 01]") - - no_of_processes = 4 - burst_time = [2, 5, 3, 7] - arrival_time = [0, 0, 0, 0] - processes = list(range(1, 5)) - waiting_time = calculate_waitingtime(arrival_time, - burst_time, no_of_processes) - turn_around_time = calculate_turnaroundtime(burst_time, - no_of_processes, waiting_time) - - # Printing the Result - print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") - for i, processes in enumerate(processes): - print( - f"{processes}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" - f"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}" - ) - print(f"\nAverage waiting time = {mean(waiting_time):.5f}") - print(f"Average turnaround time = {mean(turn_around_time):.5f}") From e36c13f95850aabef55a327324b0ca770fe1a0c2 Mon Sep 17 00:00:00 2001 From: BYN Date: Sun, 29 May 2022 20:20:29 +0900 Subject: [PATCH 4/7] delete Korean comments --- non_preemptive_shortest_job_first.py | 128 ------------------ .../non_preemptive_shortest_job_first.py | 35 ++--- 2 files changed, 11 insertions(+), 152 deletions(-) delete mode 100644 non_preemptive_shortest_job_first.py diff --git a/non_preemptive_shortest_job_first.py b/non_preemptive_shortest_job_first.py deleted file mode 100644 index e0dbf33cf4a3..000000000000 --- a/non_preemptive_shortest_job_first.py +++ /dev/null @@ -1,128 +0,0 @@ -# SJF -""" -Non-preemptive Shortest Job First -Shortest execution time process is chosen for the next execution. -https://www.guru99.com/shortest-job-first-sjf-scheduling.html -https://en.wikipedia.org/wiki/Shortest_job_next -""" - -from __future__ import annotations - -from statistics import mean - - -def calculate_waitingtime( - arrival_time: list[int], burst_time: list[int], no_of_processes: int -) -> list[int]: - # Arrival_time : 도착 시간을 담은 리스트 - # Burst_time : 수행시간 - # No_of_processes : 프로세스의 개수, the number of processes - - """ - Calculate the waiting time of each processes - - Return: The waiting time for each process. - >>> calculate_waitingtime([0,1,2], [10, 5, 8], 3) - [0, 9, 13] - >>> calculate_waitingtime([1,2,2,4], [4, 6, 3, 1], 4) - [0, 7, 4, 1] - >>> calculate_waitingtime([0,0,0], [12, 2, 10],3) - [12, 0, 2] - """ - - # waiting_time : 대기 시간 - # remaining_time : 남은 수행 시간 - waiting_time = [0] * no_of_processes - remaining_time = [0] * no_of_processes - - """ - Initialize remaining_time to waiting_time. - 남은 수행 시간을 수행 시간으로 초기화한다. - """ - for i in range(no_of_processes): - remaining_time[i] = burst_time[i] - ready_process = [] - - # completed : 수행 완료된 프로세스의 수, The number of processes completed - # total_time : 프로세스와 무관하게 진행되는 시간 - completed = 0 - total_time = 0 - - """ - 수행할 프로세스가 남아 있을 때, - 도착 시간이 지났고 남아 있는 수행 시간이 있는 프로세스는 ready_process에 들어간다. - ready_process에서 가장 짧은 프로세스인 target_process가 실행된다. - - When processes are not completed, - A process whose arrival time has passed \ - and has remaining execution time is put into the ready_process. - The shortest process in the ready_process, target_process is executed. - """ - while completed != no_of_processes: - ready_process = [] - target_process = -1 - - for i in range(no_of_processes): - if (arrival_time[i] <= total_time) & (remaining_time[i] > 0): - ready_process.append(i) - - if len(ready_process) > 0: - target_process = ready_process[0] - for i in ready_process: - if remaining_time[i] < remaining_time[target_process]: - target_process = i - total_time += burst_time[target_process] - completed += 1 - remaining_time[target_process] = 0 - waiting_time[target_process] = \ - total_time - arrival_time[target_process]\ - - burst_time[target_process] - else: - total_time += 1 - - return waiting_time - - -def calculate_turnaroundtime( - burst_time: list[int], no_of_processes: int, waiting_time: list[int] -) -> list[int]: - """ - Calculate the turnaround time of each Processes - - Return: The turnaround time for each process. - >>> calculate_turnaroundtime([0,1,2], 3, [0, 10, 15]) - [0, 11, 17] - >>> calculate_turnaroundtime([1,2,2,4], 4, [1, 8, 5, 4]) - [2, 10, 7, 8] - >>> calculate_turnaroundtime([0,0,0], 3, [12, 0, 2]) - [12, 0, 2] - """ - - turn_around_time = [0] * no_of_processes - for i in range(no_of_processes): - turn_around_time[i] = burst_time[i] \ - + waiting_time[i] - return turn_around_time - - -if __name__ == "__main__": - print("[TEST CASE 01]") - - no_of_processes = 4 - burst_time = [2, 5, 3, 7] - arrival_time = [0, 0, 0, 0] - processes = list(range(1, 5)) - waiting_time = calculate_waitingtime(arrival_time, - burst_time, no_of_processes) - turn_around_time = calculate_turnaroundtime(burst_time, - no_of_processes, waiting_time) - - # Printing the Result - print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") - for i, processes in enumerate(processes): - print( - f"{processes}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" - f"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}" - ) - print(f"\nAverage waiting time = {mean(waiting_time):.5f}") - print(f"Average turnaround time = {mean(turn_around_time):.5f}") diff --git a/scheduling/non_preemptive_shortest_job_first.py b/scheduling/non_preemptive_shortest_job_first.py index e07c55f4009c..e38fe3146fbf 100644 --- a/scheduling/non_preemptive_shortest_job_first.py +++ b/scheduling/non_preemptive_shortest_job_first.py @@ -13,11 +13,8 @@ def calculate_waitingtime( - arrival_time: list[int], burst_time: list[int], no_of_processes: int + arrival_time: list[int], burst_time: list[int], no_of_processes: int ) -> list[int]: - # Arrival_time : 도착 시간을 담은 리스트 - # Burst_time : 수행시간 - # No_of_processes : 프로세스의 개수, the number of processes """ Calculate the waiting time of each processes @@ -31,30 +28,21 @@ def calculate_waitingtime( [12, 0, 2] """ - # waiting_time : 대기 시간 - # remaining_time : 남은 수행 시간 waiting_time = [0] * no_of_processes remaining_time = [0] * no_of_processes """ Initialize remaining_time to waiting_time. - 남은 수행 시간을 수행 시간으로 초기화한다. """ for i in range(no_of_processes): remaining_time[i] = burst_time[i] ready_process = [] - # completed : 수행 완료된 프로세스의 수, The number of processes completed - # total_time : 프로세스와 무관하게 진행되는 시간 completed = 0 total_time = 0 """ - 수행할 프로세스가 남아 있을 때, - 도착 시간이 지났고 남아 있는 수행 시간이 있는 프로세스는 ready_process에 들어간다. - ready_process에서 가장 짧은 프로세스인 target_process가 실행된다. - - When processes are not completed, + When processes are not completed, A process whose arrival time has passed \ and has remaining execution time is put into the ready_process. The shortest process in the ready_process, target_process is executed. @@ -75,9 +63,9 @@ def calculate_waitingtime( total_time += burst_time[target_process] completed += 1 remaining_time[target_process] = 0 - waiting_time[target_process] = \ - total_time - arrival_time[target_process]\ - - burst_time[target_process] + waiting_time[target_process] = ( + total_time - arrival_time[target_process] - burst_time[target_process] + ) else: total_time += 1 @@ -85,7 +73,7 @@ def calculate_waitingtime( def calculate_turnaroundtime( - burst_time: list[int], no_of_processes: int, waiting_time: list[int] + burst_time: list[int], no_of_processes: int, waiting_time: list[int] ) -> list[int]: """ Calculate the turnaround time of each Processes @@ -101,8 +89,7 @@ def calculate_turnaroundtime( turn_around_time = [0] * no_of_processes for i in range(no_of_processes): - turn_around_time[i] = burst_time[i] \ - + waiting_time[i] + turn_around_time[i] = burst_time[i] + waiting_time[i] return turn_around_time @@ -113,10 +100,10 @@ def calculate_turnaroundtime( burst_time = [2, 5, 3, 7] arrival_time = [0, 0, 0, 0] processes = list(range(1, 5)) - waiting_time = calculate_waitingtime(arrival_time, - burst_time, no_of_processes) - turn_around_time = calculate_turnaroundtime(burst_time, - no_of_processes, waiting_time) + waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes) + turn_around_time = calculate_turnaroundtime( + burst_time, no_of_processes, waiting_time + ) # Printing the Result print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") From 698c74ddb1f8d43a8b440bb754dddbc725692a22 Mon Sep 17 00:00:00 2001 From: BYN Date: Tue, 31 May 2022 14:18:10 +0900 Subject: [PATCH 5/7] change comments, & to and, type annotation --- .../non_preemptive_shortest_job_first.py | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/scheduling/non_preemptive_shortest_job_first.py b/scheduling/non_preemptive_shortest_job_first.py index e38fe3146fbf..476f11023d85 100644 --- a/scheduling/non_preemptive_shortest_job_first.py +++ b/scheduling/non_preemptive_shortest_job_first.py @@ -1,4 +1,3 @@ -# SJF """ Non-preemptive Shortest Job First Shortest execution time process is chosen for the next execution. @@ -15,7 +14,6 @@ def calculate_waitingtime( arrival_time: list[int], burst_time: list[int], no_of_processes: int ) -> list[int]: - """ Calculate the waiting time of each processes @@ -31,28 +29,26 @@ def calculate_waitingtime( waiting_time = [0] * no_of_processes remaining_time = [0] * no_of_processes - """ - Initialize remaining_time to waiting_time. - """ + # Initialize remaining_time to waiting_time. + for i in range(no_of_processes): remaining_time[i] = burst_time[i] - ready_process = [] + ready_process = [] # ready_process: List[int] completed = 0 total_time = 0 - """ - When processes are not completed, - A process whose arrival time has passed \ - and has remaining execution time is put into the ready_process. - The shortest process in the ready_process, target_process is executed. - """ + # When processes are not completed, + # A process whose arrival time has passed \ + # and has remaining execution time is put into the ready_process. + # The shortest process in the ready_process, target_process is executed. + while completed != no_of_processes: ready_process = [] target_process = -1 for i in range(no_of_processes): - if (arrival_time[i] <= total_time) & (remaining_time[i] > 0): + if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(i) if len(ready_process) > 0: @@ -76,7 +72,7 @@ def calculate_turnaroundtime( burst_time: list[int], no_of_processes: int, waiting_time: list[int] ) -> list[int]: """ - Calculate the turnaround time of each Processes + Calculate the turnaround time of each process. Return: The turnaround time for each process. >>> calculate_turnaroundtime([0,1,2], 3, [0, 10, 15]) @@ -99,7 +95,6 @@ def calculate_turnaroundtime( no_of_processes = 4 burst_time = [2, 5, 3, 7] arrival_time = [0, 0, 0, 0] - processes = list(range(1, 5)) waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes) turn_around_time = calculate_turnaroundtime( burst_time, no_of_processes, waiting_time @@ -107,9 +102,9 @@ def calculate_turnaroundtime( # Printing the Result print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time") - for i, processes in enumerate(processes): + for i, process_ID in enumerate(list(range(1, 5))): print( - f"{processes}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" + f"{process_ID}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t" f"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}" ) print(f"\nAverage waiting time = {mean(waiting_time):.5f}") From 65fdf4cbca6dbef42843af848065e4558e64942f Mon Sep 17 00:00:00 2001 From: BYN Date: Mon, 6 Jun 2022 22:02:38 +0900 Subject: [PATCH 6/7] type annotation --- scheduling/non_preemptive_shortest_job_first.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scheduling/non_preemptive_shortest_job_first.py b/scheduling/non_preemptive_shortest_job_first.py index 476f11023d85..e258dc805fba 100644 --- a/scheduling/non_preemptive_shortest_job_first.py +++ b/scheduling/non_preemptive_shortest_job_first.py @@ -10,6 +10,8 @@ from statistics import mean +"from typing import List" + def calculate_waitingtime( arrival_time: list[int], burst_time: list[int], no_of_processes: int @@ -33,7 +35,7 @@ def calculate_waitingtime( for i in range(no_of_processes): remaining_time[i] = burst_time[i] - ready_process = [] # ready_process: List[int] + ready_process: list[int] = [] completed = 0 total_time = 0 From d18a6cc99a39da073ea4c8f0a5c2acdf112bd19e Mon Sep 17 00:00:00 2001 From: BYN Date: Tue, 7 Jun 2022 00:26:00 +0900 Subject: [PATCH 7/7] delete unnecessary comment --- scheduling/non_preemptive_shortest_job_first.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/scheduling/non_preemptive_shortest_job_first.py b/scheduling/non_preemptive_shortest_job_first.py index e258dc805fba..96e571230ec0 100644 --- a/scheduling/non_preemptive_shortest_job_first.py +++ b/scheduling/non_preemptive_shortest_job_first.py @@ -10,8 +10,6 @@ from statistics import mean -"from typing import List" - def calculate_waitingtime( arrival_time: list[int], burst_time: list[int], no_of_processes: int