From 0612f08d76b4473def97f85f1d62d6a689997f0d Mon Sep 17 00:00:00 2001 From: Ale3androsS Date: Fri, 31 Jan 2020 17:01:05 +0200 Subject: [PATCH 1/6] Added FCFS --- scheduling/fcfs.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 scheduling/fcfs.py diff --git a/scheduling/fcfs.py b/scheduling/fcfs.py new file mode 100644 index 000000000000..a039cf0486a0 --- /dev/null +++ b/scheduling/fcfs.py @@ -0,0 +1,83 @@ +# Implementation of First Come First Served CPU sheduling Algorithm +# In this Algorithm we just care about the order that the processes arrived +# without carring about their duration time + +# Function to calculate the waiting time of each process + + +def findWaitingTime(processes, n, duration_time, waiting_time): + + # Initialising the first prosecces' waiting time with 0 + waiting_time[0] = 0 + + # calculating waiting time + for i in range(1, n): + waiting_time[i] = duration_time[i - 1] + waiting_time[i - 1] + + +# Function to calculate the turn-around time of each process + + +def findTurnAroundTime(processes, n, duration_time, waiting_time, turn_around_time): + + # calculating turnaround time by + # adding duration_time[i] + waiting_time[i] + + for i in range(n): + turn_around_time[i] = duration_time[i] + waiting_time[i] + + +# Function to calculate the average time of each process + + +def findavgTime(processes, n, duration_time): + + waiting_time = [0] * n + turn_around_time = [0] * n + total_waiting_time = 0 + total_turn_around_time = 0 + + # Function to find waiting time of each process + + findWaitingTime(processes, n, duration_time, waiting_time) + + # Function to find turn-around time of each process + + findTurnAroundTime(processes, n, duration_time, waiting_time, turn_around_time) + + # Display processes along their details + + print("Processes Burst time " + " Waiting time " + " Turn around time") + + # Calculate total waiting time + # and total turn around time + + for i in range(n): + total_waiting_time = total_waiting_time + waiting_time[i] + total_turn_around_time = total_turn_around_time + turn_around_time[i] + print(f" {i+1}\t\t{duration_time[i]}\t {waiting_time[i]}\t\t {turn_around_time[i]}") + print(f"Average waiting time = {total_waiting_time / n}") + print(f"Average turn around time = {total_turn_around_time / n}") + + +# Driver code +if __name__ == "__main__": + + # process id's + processes = [1, 2, 3] + + # ensure that we actually have prosecces + if len(processes) == 0: + print("Zero amount of processes") + exit() + n = len(processes) + + # duration time of all processes + duration_time = [19, 8, 9] + + # ensure we can match each id to a duration time + if len(duration_time) != len(processes): + print("Unable to match all id's with their duration time") + exit() + + findavgTime(processes, n, duration_time) From 9b3d5f8b1da123978979ed0f8a32345fdd001860 Mon Sep 17 00:00:00 2001 From: Ale3androsS Date: Fri, 31 Jan 2020 17:25:57 +0200 Subject: [PATCH 2/6] Fixed spelling error --- scheduling/fcfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduling/fcfs.py b/scheduling/fcfs.py index a039cf0486a0..aac130ecd4b6 100644 --- a/scheduling/fcfs.py +++ b/scheduling/fcfs.py @@ -1,4 +1,4 @@ -# Implementation of First Come First Served CPU sheduling Algorithm +# Implementation of First Come First Served CPU scheduling Algorithm # In this Algorithm we just care about the order that the processes arrived # without carring about their duration time From d6ba127d6ef784fddf297f9372f95942ddfd4087 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 31 Jan 2020 17:03:30 +0100 Subject: [PATCH 3/6] Rename fcfs.py to first_come_first_served.py --- scheduling/{fcfs.py => first_come_first_served.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename scheduling/{fcfs.py => first_come_first_served.py} (100%) diff --git a/scheduling/fcfs.py b/scheduling/first_come_first_served.py similarity index 100% rename from scheduling/fcfs.py rename to scheduling/first_come_first_served.py From 6768c420e747634f21956bee468b71468bd12ece Mon Sep 17 00:00:00 2001 From: Ale3androsS Date: Wed, 5 Feb 2020 20:21:21 +0200 Subject: [PATCH 4/6] Fixed FCFS and added tests. --- scheduling/first_come_first_served.py | 153 ++++++++++++++++---------- 1 file changed, 94 insertions(+), 59 deletions(-) diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index aac130ecd4b6..a44e6734b439 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -1,66 +1,83 @@ # Implementation of First Come First Served CPU scheduling Algorithm # In this Algorithm we just care about the order that the processes arrived # without carring about their duration time - -# Function to calculate the waiting time of each process - - -def findWaitingTime(processes, n, duration_time, waiting_time): - - # Initialising the first prosecces' waiting time with 0 - waiting_time[0] = 0 - - # calculating waiting time - for i in range(1, n): +# https://en.wikipedia.org/wiki/Scheduling_(computing)#First_come,_first_served +from typing import List +from functools import reduce + + +def calculate_waiting_time( + number_of_processes: int, duration_time: List[int] +) -> List[int]: + """ + This function calculates the waiting time of some processes that have a specified duration time. + Return: The waiting time for each process. + >>> calculate_waiting_time(3, [5,10,15]) + [0, 5, 15] + >>> calculate_waiting_time(5, [1,2,3,4,5]) + [0, 1, 3, 6, 10] + >>> calculate_waiting_time(2, [10, 3]) + [0, 10] + """ + # Initialize the result of the waiting time to all zeroes + waiting_time: List[int] = [0] * number_of_processes + # calculating waiting time. First item should always be 0 so the iteration starts from index 1 + for i in range(1, number_of_processes): waiting_time[i] = duration_time[i - 1] + waiting_time[i - 1] + return waiting_time + + +def calculate_turnaround_time( + number_of_processes: int, duration_time: List[int], waiting_time: List[int] +) -> List[int]: + """ + This function calculates the turnaround time of some processes. + Return: The time difference between the completion time and the arrival time. + Practically waiting_time + duration_time + >>> calculate_turnaround_time(3, [5, 10, 15], [0, 5, 15]) + [5, 15, 30] + >>> calculate_turnaround_time(5, [1, 2, 3, 4, 5], [0, 1, 3, 6, 10]) + [1, 3, 6, 10, 15] + >>> calculate_turnaround_time(2, [10, 3], [0, 10]) + [10, 13] + """ + # Initialize the result of the waiting time to all zeroes + turnaround_time: List[int] = [0] * number_of_processes + for i in range(number_of_processes): + turnaround_time[i] = duration_time[i] + waiting_time[i] + return turnaround_time + + +def calculate_average_turnaround_time(number_of_processes, turnaround_time) -> float: + """ + This function calculates the average of the turnaround times + Return: The average of the turnaround times. + >>> calculate_average_turnaround_time(3, [0, 5, 16]) + 7.0 + >>> calculate_average_turnaround_time(4, [1, 5, 8, 12]) + 6.5 + >>> calculate_average_turnaround_time(2, [10, 24]) + 17.0 + """ + total_turnaround_time: List[int] = reduce(lambda x, y: x + y, turnaround_time, 0) + return total_turnaround_time / number_of_processes + + +def calculate_average_waiting_time(number_of_processes, waiting_time) -> float: + """ + This function calculates the average of the waiting times + Return: The average of the waiting times. + >>> calculate_average_waiting_time(3, [0, 5, 16]) + 7.0 + >>> calculate_average_waiting_time(4, [1, 5, 8, 12]) + 6.5 + >>> calculate_average_waiting_time(2, [10, 24]) + 17.0 + """ + total_waiting_time: List[int] = reduce(lambda x, y: x + y, waiting_time, 0) + return total_waiting_time / number_of_processes -# Function to calculate the turn-around time of each process - - -def findTurnAroundTime(processes, n, duration_time, waiting_time, turn_around_time): - - # calculating turnaround time by - # adding duration_time[i] + waiting_time[i] - - for i in range(n): - turn_around_time[i] = duration_time[i] + waiting_time[i] - - -# Function to calculate the average time of each process - - -def findavgTime(processes, n, duration_time): - - waiting_time = [0] * n - turn_around_time = [0] * n - total_waiting_time = 0 - total_turn_around_time = 0 - - # Function to find waiting time of each process - - findWaitingTime(processes, n, duration_time, waiting_time) - - # Function to find turn-around time of each process - - findTurnAroundTime(processes, n, duration_time, waiting_time, turn_around_time) - - # Display processes along their details - - print("Processes Burst time " + " Waiting time " + " Turn around time") - - # Calculate total waiting time - # and total turn around time - - for i in range(n): - total_waiting_time = total_waiting_time + waiting_time[i] - total_turn_around_time = total_turn_around_time + turn_around_time[i] - print(f" {i+1}\t\t{duration_time[i]}\t {waiting_time[i]}\t\t {turn_around_time[i]}") - print(f"Average waiting time = {total_waiting_time / n}") - print(f"Average turn around time = {total_turn_around_time / n}") - - -# Driver code if __name__ == "__main__": # process id's @@ -70,7 +87,6 @@ def findavgTime(processes, n, duration_time): if len(processes) == 0: print("Zero amount of processes") exit() - n = len(processes) # duration time of all processes duration_time = [19, 8, 9] @@ -80,4 +96,23 @@ def findavgTime(processes, n, duration_time): print("Unable to match all id's with their duration time") exit() - findavgTime(processes, n, duration_time) + # get the waiting times and the turnaround times + waiting_time = calculate_waiting_time(len(processes), duration_time) + turnaround_time = calculate_turnaround_time( + len(processes), duration_time, waiting_time + ) + + # get the average times + average_waiting_time = calculate_average_waiting_time(len(processes), waiting_time) + average_turnaround_time = calculate_average_turnaround_time( + len(processes), turnaround_time + ) + + # print all the results + print("Process ID\tDuration Time\tWaiting Time\tTurnaround Time") + for i in range(len(processes)): + print( + f"{processes[i]}\t\t{duration_time[i]}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}" + ) + print(f"Average waiting time = {average_waiting_time}") + print(f"Average turn around time = {average_turnaround_time}") \ No newline at end of file From 0bf18782c606d162cec28dbbc93e02299ea422c9 Mon Sep 17 00:00:00 2001 From: Ale3androsS Date: Thu, 6 Feb 2020 20:36:44 +0200 Subject: [PATCH 5/6] Made changes requested --- scheduling/first_come_first_served.py | 85 ++++++++++++--------------- 1 file changed, 36 insertions(+), 49 deletions(-) diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index a44e6734b439..56a5cff30930 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -3,79 +3,70 @@ # without carring about their duration time # https://en.wikipedia.org/wiki/Scheduling_(computing)#First_come,_first_served from typing import List -from functools import reduce -def calculate_waiting_time( - number_of_processes: int, duration_time: List[int] -) -> List[int]: +def calculate_waiting_times(duration_times: List[int]) -> List[int]: """ - This function calculates the waiting time of some processes that have a specified duration time. + This function calculates the waiting time of some processes that have a + specified duration time. Return: The waiting time for each process. - >>> calculate_waiting_time(3, [5,10,15]) + >>> calculate_waiting_times([5,10,15]) [0, 5, 15] - >>> calculate_waiting_time(5, [1,2,3,4,5]) + >>> calculate_waiting_times([1,2,3,4,5]) [0, 1, 3, 6, 10] - >>> calculate_waiting_time(2, [10, 3]) + >>> calculate_waiting_times([10, 3]) [0, 10] """ - # Initialize the result of the waiting time to all zeroes - waiting_time: List[int] = [0] * number_of_processes - # calculating waiting time. First item should always be 0 so the iteration starts from index 1 - for i in range(1, number_of_processes): - waiting_time[i] = duration_time[i - 1] + waiting_time[i - 1] - return waiting_time + waiting_times = [0] * len(duration_times) + for i in range(1, len(duration_times)): + waiting_times[i] = duration_times[i - 1] + waiting_times[i - 1] + return waiting_times -def calculate_turnaround_time( - number_of_processes: int, duration_time: List[int], waiting_time: List[int] +def calculate_turnaround_times( + duration_times: List[int], waiting_times: List[int] ) -> List[int]: """ This function calculates the turnaround time of some processes. - Return: The time difference between the completion time and the arrival time. + Return: The time difference between the completion time and the + arrival time. Practically waiting_time + duration_time - >>> calculate_turnaround_time(3, [5, 10, 15], [0, 5, 15]) + >>> calculate_turnaround_times([5, 10, 15], [0, 5, 15]) [5, 15, 30] - >>> calculate_turnaround_time(5, [1, 2, 3, 4, 5], [0, 1, 3, 6, 10]) + >>> calculate_turnaround_times([1, 2, 3, 4, 5], [0, 1, 3, 6, 10]) [1, 3, 6, 10, 15] - >>> calculate_turnaround_time(2, [10, 3], [0, 10]) + >>> calculate_turnaround_times([10, 3], [0, 10]) [10, 13] """ - # Initialize the result of the waiting time to all zeroes - turnaround_time: List[int] = [0] * number_of_processes - for i in range(number_of_processes): - turnaround_time[i] = duration_time[i] + waiting_time[i] - return turnaround_time + return [duration_times[i] + waiting_times[i] for i in range(len(duration_times))] -def calculate_average_turnaround_time(number_of_processes, turnaround_time) -> float: +def calculate_average_turnaround_time(turnaround_times: List[int]) -> float: """ This function calculates the average of the turnaround times Return: The average of the turnaround times. - >>> calculate_average_turnaround_time(3, [0, 5, 16]) + >>> calculate_average_turnaround_time([0, 5, 16]) 7.0 - >>> calculate_average_turnaround_time(4, [1, 5, 8, 12]) + >>> calculate_average_turnaround_time([1, 5, 8, 12]) 6.5 - >>> calculate_average_turnaround_time(2, [10, 24]) + >>> calculate_average_turnaround_time([10, 24]) 17.0 """ - total_turnaround_time: List[int] = reduce(lambda x, y: x + y, turnaround_time, 0) - return total_turnaround_time / number_of_processes + return sum(turnaround_times) / len(turnaround_times) -def calculate_average_waiting_time(number_of_processes, waiting_time) -> float: +def calculate_average_waiting_time(waiting_times: List[int]) -> float: """ This function calculates the average of the waiting times Return: The average of the waiting times. - >>> calculate_average_waiting_time(3, [0, 5, 16]) + >>> calculate_average_waiting_time([0, 5, 16]) 7.0 - >>> calculate_average_waiting_time(4, [1, 5, 8, 12]) + >>> calculate_average_waiting_time([1, 5, 8, 12]) 6.5 - >>> calculate_average_waiting_time(2, [10, 24]) + >>> calculate_average_waiting_time([10, 24]) 17.0 """ - total_waiting_time: List[int] = reduce(lambda x, y: x + y, waiting_time, 0) - return total_waiting_time / number_of_processes + return sum(waiting_times) / len(waiting_times) if __name__ == "__main__": @@ -89,30 +80,26 @@ def calculate_average_waiting_time(number_of_processes, waiting_time) -> float: exit() # duration time of all processes - duration_time = [19, 8, 9] + duration_times = [19, 8, 9] # ensure we can match each id to a duration time - if len(duration_time) != len(processes): + if len(duration_times) != len(processes): print("Unable to match all id's with their duration time") exit() # get the waiting times and the turnaround times - waiting_time = calculate_waiting_time(len(processes), duration_time) - turnaround_time = calculate_turnaround_time( - len(processes), duration_time, waiting_time - ) + waiting_times = calculate_waiting_times(duration_times) + turnaround_times = calculate_turnaround_times(duration_times, waiting_times) # get the average times - average_waiting_time = calculate_average_waiting_time(len(processes), waiting_time) - average_turnaround_time = calculate_average_turnaround_time( - len(processes), turnaround_time - ) + average_waiting_time = calculate_average_waiting_time(waiting_times) + average_turnaround_time = calculate_average_turnaround_time(turnaround_times) # print all the results print("Process ID\tDuration Time\tWaiting Time\tTurnaround Time") for i in range(len(processes)): print( - f"{processes[i]}\t\t{duration_time[i]}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}" + f"{processes[i]}\t\t{duration_times[i]}\t\t{waiting_times[i]}\t\t{turnaround_times[i]}" ) print(f"Average waiting time = {average_waiting_time}") - print(f"Average turn around time = {average_turnaround_time}") \ No newline at end of file + print(f"Average turn around time = {average_turnaround_time}") From 8539b87cc529394184d537c92d82c12f9f680df5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 6 Feb 2020 21:39:26 +0200 Subject: [PATCH 6/6] Use enumerate() instead of range(len()) --- scheduling/first_come_first_served.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index 56a5cff30930..d339273fe741 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -1,4 +1,4 @@ -# Implementation of First Come First Served CPU scheduling Algorithm +# Implementation of First Come First Served scheduling algorithm # In this Algorithm we just care about the order that the processes arrived # without carring about their duration time # https://en.wikipedia.org/wiki/Scheduling_(computing)#First_come,_first_served @@ -10,9 +10,9 @@ def calculate_waiting_times(duration_times: List[int]) -> List[int]: This function calculates the waiting time of some processes that have a specified duration time. Return: The waiting time for each process. - >>> calculate_waiting_times([5,10,15]) + >>> calculate_waiting_times([5, 10, 15]) [0, 5, 15] - >>> calculate_waiting_times([1,2,3,4,5]) + >>> calculate_waiting_times([1, 2, 3, 4, 5]) [0, 1, 3, 6, 10] >>> calculate_waiting_times([10, 3]) [0, 10] @@ -38,7 +38,7 @@ def calculate_turnaround_times( >>> calculate_turnaround_times([10, 3], [0, 10]) [10, 13] """ - return [duration_times[i] + waiting_times[i] for i in range(len(duration_times))] + return [duration_time + waiting_times[i] for i, duration_time in enumerate(duration_times)] def calculate_average_turnaround_time(turnaround_times: List[int]) -> float: @@ -70,11 +70,10 @@ def calculate_average_waiting_time(waiting_times: List[int]) -> float: if __name__ == "__main__": - # process id's processes = [1, 2, 3] - # ensure that we actually have prosecces + # ensure that we actually have processes if len(processes) == 0: print("Zero amount of processes") exit() @@ -97,9 +96,9 @@ def calculate_average_waiting_time(waiting_times: List[int]) -> float: # print all the results print("Process ID\tDuration Time\tWaiting Time\tTurnaround Time") - for i in range(len(processes)): + for i, process in enumerate(processes): print( - f"{processes[i]}\t\t{duration_times[i]}\t\t{waiting_times[i]}\t\t{turnaround_times[i]}" + f"{process}\t\t{duration_times[i]}\t\t{waiting_times[i]}\t\t{turnaround_times[i]}" ) print(f"Average waiting time = {average_waiting_time}") print(f"Average turn around time = {average_turnaround_time}")