From 210389d1d4342c7a5e73fc8c69a085c79f3d0c28 Mon Sep 17 00:00:00 2001 From: pawanbuddy2000 Date: Mon, 29 Jun 2020 18:06:55 +0530 Subject: [PATCH 01/15] round_robin and priority cpu scheduling algorithms --- scheduling/priority_cpu_scheduling.py | 43 ++++++++++++++++++ scheduling/round_robin_algorithm.py | 64 +++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100755 scheduling/priority_cpu_scheduling.py create mode 100755 scheduling/round_robin_algorithm.py diff --git a/scheduling/priority_cpu_scheduling.py b/scheduling/priority_cpu_scheduling.py new file mode 100755 index 000000000000..1736a419c4c9 --- /dev/null +++ b/scheduling/priority_cpu_scheduling.py @@ -0,0 +1,43 @@ +# Priority Scheduling algorithm implementation using python +# Priority Scheduling is one of the most common scheduling algorithm in batch systems +def findWaitingTime(processes, n, wt): + wt[0] = 0 + for i in range(1, n): + wt[i] = processes[i - 1][1] + wt[i - 1] +def findTurnAroundTime(processes, n, wt, tat): + for i in range(n): + tat[i] = processes[i][1] + wt[i] + +def findavgTime(processes, n): + wt = [0] * n + tat = [0] * n + findWaitingTime(processes, n, wt) + findTurnAroundTime(processes, n, wt, tat) + + print("\nProcesses Burst Time Waiting Time Turn-Around Time") + total_wt = 0 + total_tat = 0 + for i in range(n): + + total_wt = total_wt + wt[i] + total_tat = total_tat + tat[i] + print(" ", processes[i][0], "\t\t", processes[i][1], "\t\t", wt[i], "\t\t", tat[i]) + + print("\nAverage waiting time = %.5f "%(total_wt /n)) + print("Average turn around time = ", total_tat / n) + +def priorityScheduling(proc, n): + + proc = sorted(proc, key = lambda proc:proc[2], reverse = True); + + print("Order in which processes gets executed") + for i in proc: + print(i[0], end = " ") + findavgTime(proc, n) + + +if __name__ =="__main__": + + proc = [[1, 10, 1], [2, 5, 0], [3, 8, 1]] + n = 3 + priorityScheduling(proc, n) diff --git a/scheduling/round_robin_algorithm.py b/scheduling/round_robin_algorithm.py new file mode 100755 index 000000000000..d4591ab4bf9f --- /dev/null +++ b/scheduling/round_robin_algorithm.py @@ -0,0 +1,64 @@ +# Round Robin Scheduling implementation using python +# Round Robin i a CPU scheduling algorithm where each process is assigned a fixed time slot in a cyclic way. +def findWaitingTime(processes, n, bt, wt, quantum): + rem_bt = [0] * n + + for i in range(n): + rem_bt[i] = bt[i] + t = 0 + + while(1): + done = True + + for i in range(n): + + if (rem_bt[i] > 0) : + done = False + + if (rem_bt[i] > quantum) : + t += quantum + + rem_bt[i] -= quantum + + else: + + t = t + rem_bt[i] + wt[i] = t - bt[i] + rem_bt[i] = 0 + + if (done == True): + break + +def findTurnAroundTime(processes, n, bt, wt, tat): + for i in range(n): + tat[i] = bt[i] + wt[i] + + +def findavgTime(processes, n, bt, quantum): + wt = [0] * n + tat = [0] * n + + findWaitingTime(processes, n, bt, wt, quantum) + + findTurnAroundTime(processes, n, bt, wt, tat) + + print("Processes Burst Time Waitint Time Turn-Around Time") + total_wt = 0 + total_tat = 0 + for i in range(n): + + total_wt = total_wt + wt[i] + total_tat = total_tat + tat[i] + print(" ", i + 1, "\t\t", bt[i], "\t\t", wt[i], "\t\t", tat[i]) + + print("\nAverage waiting time = %.5f "%(total_wt /n) ) + print("Average turn around time = %.5f "%(total_tat / n)) + +if __name__ =="__main__": + + proc = [1, 2, 3] + n = 3 + burst_time = [8, 6, 12] + quantum = 2; + findavgTime(proc, n, burst_time, quantum) + From ad82bcf0ac55fd3e4229d3136dfec0f398b190f6 Mon Sep 17 00:00:00 2001 From: Pawan Sundargiri <67560186+pawanbuddy2000@users.noreply.github.com> Date: Mon, 29 Jun 2020 18:10:06 +0530 Subject: [PATCH 02/15] Delete priority_cpu_scheduling.py --- scheduling/priority_cpu_scheduling.py | 43 --------------------------- 1 file changed, 43 deletions(-) delete mode 100755 scheduling/priority_cpu_scheduling.py diff --git a/scheduling/priority_cpu_scheduling.py b/scheduling/priority_cpu_scheduling.py deleted file mode 100755 index 1736a419c4c9..000000000000 --- a/scheduling/priority_cpu_scheduling.py +++ /dev/null @@ -1,43 +0,0 @@ -# Priority Scheduling algorithm implementation using python -# Priority Scheduling is one of the most common scheduling algorithm in batch systems -def findWaitingTime(processes, n, wt): - wt[0] = 0 - for i in range(1, n): - wt[i] = processes[i - 1][1] + wt[i - 1] -def findTurnAroundTime(processes, n, wt, tat): - for i in range(n): - tat[i] = processes[i][1] + wt[i] - -def findavgTime(processes, n): - wt = [0] * n - tat = [0] * n - findWaitingTime(processes, n, wt) - findTurnAroundTime(processes, n, wt, tat) - - print("\nProcesses Burst Time Waiting Time Turn-Around Time") - total_wt = 0 - total_tat = 0 - for i in range(n): - - total_wt = total_wt + wt[i] - total_tat = total_tat + tat[i] - print(" ", processes[i][0], "\t\t", processes[i][1], "\t\t", wt[i], "\t\t", tat[i]) - - print("\nAverage waiting time = %.5f "%(total_wt /n)) - print("Average turn around time = ", total_tat / n) - -def priorityScheduling(proc, n): - - proc = sorted(proc, key = lambda proc:proc[2], reverse = True); - - print("Order in which processes gets executed") - for i in proc: - print(i[0], end = " ") - findavgTime(proc, n) - - -if __name__ =="__main__": - - proc = [[1, 10, 1], [2, 5, 0], [3, 8, 1]] - n = 3 - priorityScheduling(proc, n) From d26a0adc485d682169e4b646c548b3d3dea59957 Mon Sep 17 00:00:00 2001 From: Pawan Sundargiri <67560186+pawanbuddy2000@users.noreply.github.com> Date: Mon, 29 Jun 2020 18:10:17 +0530 Subject: [PATCH 03/15] Delete round_robin_algorithm.py --- scheduling/round_robin_algorithm.py | 64 ----------------------------- 1 file changed, 64 deletions(-) delete mode 100755 scheduling/round_robin_algorithm.py diff --git a/scheduling/round_robin_algorithm.py b/scheduling/round_robin_algorithm.py deleted file mode 100755 index d4591ab4bf9f..000000000000 --- a/scheduling/round_robin_algorithm.py +++ /dev/null @@ -1,64 +0,0 @@ -# Round Robin Scheduling implementation using python -# Round Robin i a CPU scheduling algorithm where each process is assigned a fixed time slot in a cyclic way. -def findWaitingTime(processes, n, bt, wt, quantum): - rem_bt = [0] * n - - for i in range(n): - rem_bt[i] = bt[i] - t = 0 - - while(1): - done = True - - for i in range(n): - - if (rem_bt[i] > 0) : - done = False - - if (rem_bt[i] > quantum) : - t += quantum - - rem_bt[i] -= quantum - - else: - - t = t + rem_bt[i] - wt[i] = t - bt[i] - rem_bt[i] = 0 - - if (done == True): - break - -def findTurnAroundTime(processes, n, bt, wt, tat): - for i in range(n): - tat[i] = bt[i] + wt[i] - - -def findavgTime(processes, n, bt, quantum): - wt = [0] * n - tat = [0] * n - - findWaitingTime(processes, n, bt, wt, quantum) - - findTurnAroundTime(processes, n, bt, wt, tat) - - print("Processes Burst Time Waitint Time Turn-Around Time") - total_wt = 0 - total_tat = 0 - for i in range(n): - - total_wt = total_wt + wt[i] - total_tat = total_tat + tat[i] - print(" ", i + 1, "\t\t", bt[i], "\t\t", wt[i], "\t\t", tat[i]) - - print("\nAverage waiting time = %.5f "%(total_wt /n) ) - print("Average turn around time = %.5f "%(total_tat / n)) - -if __name__ =="__main__": - - proc = [1, 2, 3] - n = 3 - burst_time = [8, 6, 12] - quantum = 2; - findavgTime(proc, n, burst_time, quantum) - From 7db666db0dd8f563abc2c201503532406befa1ff Mon Sep 17 00:00:00 2001 From: pawanbuddy2000 Date: Mon, 29 Jun 2020 18:33:58 +0530 Subject: [PATCH 04/15] [add] cpu_scheduling_algorithms --- scheduling/priority_cpu_scheduling.py | 41 ++++++++++++++++++ scheduling/round_robin.py | 62 +++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100755 scheduling/priority_cpu_scheduling.py create mode 100755 scheduling/round_robin.py diff --git a/scheduling/priority_cpu_scheduling.py b/scheduling/priority_cpu_scheduling.py new file mode 100755 index 000000000000..64f3a0307c33 --- /dev/null +++ b/scheduling/priority_cpu_scheduling.py @@ -0,0 +1,41 @@ +def findWaitingTime(processes, n, wt): + wt[0] = 0 + for i in range(1, n): + wt[i] = processes[i - 1][1] + wt[i - 1] +def findTurnAroundTime(processes, n, wt, tat): + for i in range(n): + tat[i] = processes[i][1] + wt[i] + +def findavgTime(processes, n): + wt = [0] * n + tat = [0] * n + findWaitingTime(processes, n, wt) + findTurnAroundTime(processes, n, wt, tat) + + print("\nProcesses Burst Time Waiting Time Turn-Around Time") + total_wt = 0 + total_tat = 0 + for i in range(n): + + total_wt = total_wt + wt[i] + total_tat = total_tat + tat[i] + print(" ", processes[i][0], "\t\t", processes[i][1], "\t\t", wt[i], "\t\t", tat[i]) + + print("\nAverage waiting time = %.5f "%(total_wt /n)) + print("Average turn around time = ", total_tat / n) + +def priorityScheduling(proc, n): + + proc = sorted(proc, key = lambda proc:proc[2], reverse = True); + + print("Order in which processes gets executed") + for i in proc: + print(i[0], end = " ") + findavgTime(proc, n) + + +if __name__ =="__main__": + + proc = [[1, 10, 1], [2, 5, 0], [3, 8, 1]] + n = 3 + priorityScheduling(proc, n) diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py new file mode 100755 index 000000000000..b47f4bdccf20 --- /dev/null +++ b/scheduling/round_robin.py @@ -0,0 +1,62 @@ +def findWaitingTime(processes, n, bt, wt, quantum): + rem_bt = [0] * n + + for i in range(n): + rem_bt[i] = bt[i] + t = 0 + + while(1): + done = True + + for i in range(n): + + if (rem_bt[i] > 0) : + done = False + + if (rem_bt[i] > quantum) : + t += quantum + + rem_bt[i] -= quantum + + else: + + t = t + rem_bt[i] + wt[i] = t - bt[i] + rem_bt[i] = 0 + + if (done == True): + break + +def findTurnAroundTime(processes, n, bt, wt, tat): + for i in range(n): + tat[i] = bt[i] + wt[i] + + +def findavgTime(processes, n, bt, quantum): + wt = [0] * n + tat = [0] * n + + findWaitingTime(processes, n, bt, wt, quantum) + + findTurnAroundTime(processes, n, bt, wt, tat) + + print("Processes Burst Time Waitint Time Turn-Around Time") + total_wt = 0 + total_tat = 0 + for i in range(n): + + total_wt = total_wt + wt[i] + total_tat = total_tat + tat[i] + print(" ", i + 1, "\t\t", bt[i], "\t\t", wt[i], "\t\t", tat[i]) + + print("\nAverage waiting time = %.5f "%(total_wt /n) ) + print("Average turn around time = %.5f "%(total_tat / n)) + +if __name__ =="__main__": + + proc = [1, 2, 3] + n = 3 + burst_time = [8, 6, 12] + quantum = 2; + findavgTime(proc, n, burst_time, quantum) + From b585f94b7f248ec7d85ae46e77e2c34ca19e7ffe Mon Sep 17 00:00:00 2001 From: pawanbuddy <46370996+pawanbuddy@users.noreply.github.com> Date: Thu, 2 Jul 2020 08:01:48 +0530 Subject: [PATCH 05/15] [add] Round robin cpu scheduling algorithm --- scheduling/priority_cpu_scheduling.py | 41 ------- scheduling/round_robin.py | 62 ----------- .../round_robin_scheduling_algorithm.py | 102 ++++++++++++++++++ 3 files changed, 102 insertions(+), 103 deletions(-) delete mode 100755 scheduling/priority_cpu_scheduling.py delete mode 100755 scheduling/round_robin.py create mode 100644 scheduling/round_robin_scheduling_algorithm.py diff --git a/scheduling/priority_cpu_scheduling.py b/scheduling/priority_cpu_scheduling.py deleted file mode 100755 index 64f3a0307c33..000000000000 --- a/scheduling/priority_cpu_scheduling.py +++ /dev/null @@ -1,41 +0,0 @@ -def findWaitingTime(processes, n, wt): - wt[0] = 0 - for i in range(1, n): - wt[i] = processes[i - 1][1] + wt[i - 1] -def findTurnAroundTime(processes, n, wt, tat): - for i in range(n): - tat[i] = processes[i][1] + wt[i] - -def findavgTime(processes, n): - wt = [0] * n - tat = [0] * n - findWaitingTime(processes, n, wt) - findTurnAroundTime(processes, n, wt, tat) - - print("\nProcesses Burst Time Waiting Time Turn-Around Time") - total_wt = 0 - total_tat = 0 - for i in range(n): - - total_wt = total_wt + wt[i] - total_tat = total_tat + tat[i] - print(" ", processes[i][0], "\t\t", processes[i][1], "\t\t", wt[i], "\t\t", tat[i]) - - print("\nAverage waiting time = %.5f "%(total_wt /n)) - print("Average turn around time = ", total_tat / n) - -def priorityScheduling(proc, n): - - proc = sorted(proc, key = lambda proc:proc[2], reverse = True); - - print("Order in which processes gets executed") - for i in proc: - print(i[0], end = " ") - findavgTime(proc, n) - - -if __name__ =="__main__": - - proc = [[1, 10, 1], [2, 5, 0], [3, 8, 1]] - n = 3 - priorityScheduling(proc, n) diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py deleted file mode 100755 index b47f4bdccf20..000000000000 --- a/scheduling/round_robin.py +++ /dev/null @@ -1,62 +0,0 @@ -def findWaitingTime(processes, n, bt, wt, quantum): - rem_bt = [0] * n - - for i in range(n): - rem_bt[i] = bt[i] - t = 0 - - while(1): - done = True - - for i in range(n): - - if (rem_bt[i] > 0) : - done = False - - if (rem_bt[i] > quantum) : - t += quantum - - rem_bt[i] -= quantum - - else: - - t = t + rem_bt[i] - wt[i] = t - bt[i] - rem_bt[i] = 0 - - if (done == True): - break - -def findTurnAroundTime(processes, n, bt, wt, tat): - for i in range(n): - tat[i] = bt[i] + wt[i] - - -def findavgTime(processes, n, bt, quantum): - wt = [0] * n - tat = [0] * n - - findWaitingTime(processes, n, bt, wt, quantum) - - findTurnAroundTime(processes, n, bt, wt, tat) - - print("Processes Burst Time Waitint Time Turn-Around Time") - total_wt = 0 - total_tat = 0 - for i in range(n): - - total_wt = total_wt + wt[i] - total_tat = total_tat + tat[i] - print(" ", i + 1, "\t\t", bt[i], "\t\t", wt[i], "\t\t", tat[i]) - - print("\nAverage waiting time = %.5f "%(total_wt /n) ) - print("Average turn around time = %.5f "%(total_tat / n)) - -if __name__ =="__main__": - - proc = [1, 2, 3] - n = 3 - burst_time = [8, 6, 12] - quantum = 2; - findavgTime(proc, n, burst_time, quantum) - diff --git a/scheduling/round_robin_scheduling_algorithm.py b/scheduling/round_robin_scheduling_algorithm.py new file mode 100644 index 000000000000..1afaaddcb1ad --- /dev/null +++ b/scheduling/round_robin_scheduling_algorithm.py @@ -0,0 +1,102 @@ +""" +Round Robin is a CPU scheduling algorithm. +In Round Robin each process is assigned a fixed time slot in a cyclic way. +https://en.wikipedia.org/wiki/Round-robin_scheduling +""" + + +def calculate_waiting_time(processes, n, burst_time, waiting_time, quantum): + """ + 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]) + [0, 5, 15] + >>> calculate_waiting_times([1, 2, 3, 4, 5]) + [0, 1, 3, 6, 10] + >>> calculate_waiting_times([10, 3]) + [0, 10] + """ + rem_burst_time = [0] * n + for i in range(n): + rem_burst_time[i] = burst_time[i] + t = 0 + while 1: + done = True + for i in range(n): + if rem_burst_time[i] > 0: + done = False + if rem_burst_time[i] > quantum: + t += quantum + rem_burst_time[i] -= quantum + else: + t = t + rem_burst_time[i] + waiting_time[i] = t - burst_time[i] + rem_burst_time[i] = 0 + if done is True: + break + + +def calculate_turn_around_time( + processes, n, burst_time, waiting_time, turn_around_time +): + + """ + 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_times([5, 10, 15], [0, 5, 15]) + [5, 15, 30] + >>> calculate_turnaround_times([1, 2, 3, 4, 5], [0, 1, 3, 6, 10]) + [1, 3, 6, 10, 15] + >>> calculate_turnaround_times([10, 3], [0, 10]) + [10, 13] + """ + + for i in range(n): + turn_around_time[i] = burst_time[i] + waiting_time[i] + + +def calculate_avg_time(processes, n, burst_time, quantum): + """ + This function calculates the average of the waiting times + Return: The average of the waiting times. + >>> calculate_average_waiting_time([0, 5, 16]) + 7.0 + >>> calculate_average_waiting_time([1, 5, 8, 12]) + 6.5 + >>> calculate_average_waiting_time([10, 24]) + 17.0 + """ + waiting_time = [0] * n + turn_around_time = [0] * n + calculate_waiting_time(processes, n, burst_time, waiting_time, quantum) + calculate_turn_around_time(processes, n, burst_time, waiting_time, turn_around_time) + print("Processes Burst Time Waiting Time Turn-Around Time") + total_waiting_time = 0 + total_turn_around_time = 0 + 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( + " ", + i + 1, + "\t\t", + burst_time[i], + "\t\t", + waiting_time[i], + "\t\t", + turn_around_time[i], + ) + print("\nAverage waiting time = %.5f " % (total_waiting_time / n)) + print("Average turn around time = %.5f " % (total_turn_around_time / n)) + + +if __name__ == "__main__": + # Process id + processes = [1, 2, 3] + n = 3 + burst_time = [10, 5, 8] + quantum = 2 + calculate_avg_time(processes, n, burst_time, quantum) From 2151f4a0a2a36ae1b27b12d7a5981a91119e8d6b Mon Sep 17 00:00:00 2001 From: Pawan Sundargiri <67560186+pawanbuddy2000@users.noreply.github.com> Date: Fri, 3 Jul 2020 03:05:30 +0530 Subject: [PATCH 06/15] Update scheduling/round_robin_scheduling_algorithm.py Co-authored-by: Christian Clauss --- scheduling/round_robin_scheduling_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduling/round_robin_scheduling_algorithm.py b/scheduling/round_robin_scheduling_algorithm.py index 1afaaddcb1ad..e9f298fb6b26 100644 --- a/scheduling/round_robin_scheduling_algorithm.py +++ b/scheduling/round_robin_scheduling_algorithm.py @@ -7,7 +7,7 @@ def calculate_waiting_time(processes, n, burst_time, waiting_time, quantum): """ - This function calculates the waiting time of some processes that have a + Calculate 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]) From 0e156d691c443ae0eaf05691bf94e21831891611 Mon Sep 17 00:00:00 2001 From: Pawan Sundargiri <67560186+pawanbuddy2000@users.noreply.github.com> Date: Fri, 3 Jul 2020 03:21:57 +0530 Subject: [PATCH 07/15] Update scheduling/round_robin.py Co-authored-by: Christian Clauss --- scheduling/round_robin_scheduling_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduling/round_robin_scheduling_algorithm.py b/scheduling/round_robin_scheduling_algorithm.py index e9f298fb6b26..ffcf07b2019e 100644 --- a/scheduling/round_robin_scheduling_algorithm.py +++ b/scheduling/round_robin_scheduling_algorithm.py @@ -10,7 +10,7 @@ def calculate_waiting_time(processes, n, burst_time, waiting_time, quantum): Calculate 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_time([5, 10, 15]) [0, 5, 15] >>> calculate_waiting_times([1, 2, 3, 4, 5]) [0, 1, 3, 6, 10] From 8df2f10a644c1d95ba953b9c1a6db36d6b3386cf Mon Sep 17 00:00:00 2001 From: Pawan Sundargiri <67560186+pawanbuddy2000@users.noreply.github.com> Date: Fri, 3 Jul 2020 03:52:08 +0530 Subject: [PATCH 08/15] Update scheduling/round_robin_scheduling.py Co-authored-by: Christian Clauss --- scheduling/round_robin_scheduling_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduling/round_robin_scheduling_algorithm.py b/scheduling/round_robin_scheduling_algorithm.py index ffcf07b2019e..38bcd46bd2bb 100644 --- a/scheduling/round_robin_scheduling_algorithm.py +++ b/scheduling/round_robin_scheduling_algorithm.py @@ -12,7 +12,7 @@ def calculate_waiting_time(processes, n, burst_time, waiting_time, quantum): Return: The waiting time for each process. >>> calculate_waiting_time([5, 10, 15]) [0, 5, 15] - >>> calculate_waiting_times([1, 2, 3, 4, 5]) + >>> calculate_waiting_time([1, 2, 3, 4, 5]) [0, 1, 3, 6, 10] >>> calculate_waiting_times([10, 3]) [0, 10] From 5620bd9e8303e077d694d38a1695a7edf8c8b7d3 Mon Sep 17 00:00:00 2001 From: Pawan Sundargiri <67560186+pawanbuddy2000@users.noreply.github.com> Date: Fri, 3 Jul 2020 03:52:21 +0530 Subject: [PATCH 09/15] Update scheduling/round_robin_scheduling.py Co-authored-by: Christian Clauss --- scheduling/round_robin_scheduling_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduling/round_robin_scheduling_algorithm.py b/scheduling/round_robin_scheduling_algorithm.py index 38bcd46bd2bb..2597581b6a9c 100644 --- a/scheduling/round_robin_scheduling_algorithm.py +++ b/scheduling/round_robin_scheduling_algorithm.py @@ -14,7 +14,7 @@ def calculate_waiting_time(processes, n, burst_time, waiting_time, quantum): [0, 5, 15] >>> calculate_waiting_time([1, 2, 3, 4, 5]) [0, 1, 3, 6, 10] - >>> calculate_waiting_times([10, 3]) + >>> calculate_waiting_time([10, 3]) [0, 10] """ rem_burst_time = [0] * n From 2aa35ddf3c4a7daa2cd9ce26241f5ddc70ac7c9c Mon Sep 17 00:00:00 2001 From: Pawan Sundargiri <67560186+pawanbuddy2000@users.noreply.github.com> Date: Fri, 3 Jul 2020 03:52:37 +0530 Subject: [PATCH 10/15] Update scheduling/round_robin.py Co-authored-by: Christian Clauss --- scheduling/round_robin_scheduling_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduling/round_robin_scheduling_algorithm.py b/scheduling/round_robin_scheduling_algorithm.py index 2597581b6a9c..0cbd015b7d44 100644 --- a/scheduling/round_robin_scheduling_algorithm.py +++ b/scheduling/round_robin_scheduling_algorithm.py @@ -42,7 +42,7 @@ def calculate_turn_around_time( ): """ - This function calculates the turnaround time of some processes. + Calculate the turnaround time of some processes. Return: The time difference between the completion time and the arrival time. Practically waiting_time + duration_time From e91cbfb3c6bd3b35daad3c172d34cb6a28920be3 Mon Sep 17 00:00:00 2001 From: pawanbuddy2000 Date: Fri, 3 Jul 2020 09:23:36 +0530 Subject: [PATCH 11/15] Round_Robin --- scheduling/round_robin.py | 117 ++++++++++++++++++ .../round_robin_scheduling_algorithm.py | 102 --------------- 2 files changed, 117 insertions(+), 102 deletions(-) create mode 100755 scheduling/round_robin.py delete mode 100644 scheduling/round_robin_scheduling_algorithm.py diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py new file mode 100755 index 000000000000..a12e5e3badb6 --- /dev/null +++ b/scheduling/round_robin.py @@ -0,0 +1,117 @@ +""" +Round Robin is a CPU scheduling algorithm. +In Round Robin each process is assigned a fixed time slot in a cyclic way. +https://en.wikipedia.org/wiki/Round-robin_scheduling +""" + + +def calculate_waiting_time(burst_time: int) -> int: + """ + Calculate the waiting time of some processes that have a + specified duration time. + Return: The waiting time for each process. + >>> calculate_waiting_time([10, 5, 8]) + [13, 10, 13] + >>> calculate_waiting_time([4, 6, 3, 1]) + [4, 7, 8] + >>> calculate_waiting_time([12, 2, 10]) + [12, 2, 12] + """ + n = 3 + waiting_time = [0] * n + quantum = 2 + rem_burst_time = [0] * n + for i in range(n): + rem_burst_time[i] = burst_time[i] + t = 0 + while 1: + done = True + for i in range(n): + if rem_burst_time[i] > 0: + done = False + if rem_burst_time[i] > quantum: + t += quantum + rem_burst_time[i] -= quantum + else: + t = t + rem_burst_time[i] + waiting_time[i] = t - burst_time[i] + rem_burst_time[i] = 0 + if done is True: + return waiting_time + + +def calculate_turn_around_time(burst_time: int, waiting_time: int) -> int: + + """ + Calculate the turnaround time of some processes. + Return: The time difference between the completion time and the + arrival time. + Practically waiting_time + duration_time + >>> calculate_turn_around_time([5, 10, 15], [8, 13, 15]) + [13, 23, 30] + >>> calculate_turn_around_time([1, 2, 3, 4], [0, 1, 3]) + [1, 3, 6] + >>> calculate_turn_around_time([10, 3, 7], [10, 6, 11]) + [20, 9, 18] + """ + n = 3 + turn_around_time = [0] * n + for i in range(n): + turn_around_time[i] = burst_time[i] + waiting_time[i] + return turn_around_time + + +def calculate_avg_waiting_time(waiting_time: int) -> float: + """ + This function calculates the average of the waiting times + Return: The average of the waiting times. + >>> calculate_avg_waiting_time([10, 6, 11]) + 9.0 + >>> calculate_avg_waiting_time([12, 2, 10]) + 8.0 + >>> calculate_avg_waiting_time([4, 7, 8]) + 6.333333333333333 + """ + return sum(waiting_time) / len(waiting_time) + + +def calculate_avg_turnaround_time(turn_around_time: int) -> float: + """ + This function calculates the average of the waiting times + Return: The average of the waiting times. + >>> calculate_avg_turnaround_time([25, 41, 23]) + 29.666666666666668 + >>> calculate_avg_turnaround_time([10, 3, 7]) + 6.666666666666667 + >>> calculate_avg_turnaround_time([18, 21, 12]) + 17.0 + """ + return sum(turn_around_time) / len(turn_around_time) + + +if __name__ == "__main__": + # Process id + processes = [1, 2, 3] + n = 3 + burst_time = [3, 5, 7] + quantum = 2 + waiting_time = calculate_waiting_time(burst_time) + turn_around_time = calculate_turn_around_time(burst_time, waiting_time) + avg_wating_time = 0 + avg_turn_around_time = 0 + avg_waiting_time = calculate_avg_waiting_time(waiting_time) + avg_turn_around_time = calculate_avg_turnaround_time(turn_around_time) + print("Process ID \tBurst Time \tWaiting Time \tTurnaround Time") + for i in range(n): + print( + " ", + i + 1, + "\t\t", + burst_time[i], + "\t\t", + waiting_time[i], + "\t\t", + turn_around_time[i], + ) + print("\nAverage waiting time = %.5f " % (avg_waiting_time)) + print("Average turn around time = %.5f " % (avg_turn_around_time)) diff --git a/scheduling/round_robin_scheduling_algorithm.py b/scheduling/round_robin_scheduling_algorithm.py deleted file mode 100644 index 0cbd015b7d44..000000000000 --- a/scheduling/round_robin_scheduling_algorithm.py +++ /dev/null @@ -1,102 +0,0 @@ -""" -Round Robin is a CPU scheduling algorithm. -In Round Robin each process is assigned a fixed time slot in a cyclic way. -https://en.wikipedia.org/wiki/Round-robin_scheduling -""" - - -def calculate_waiting_time(processes, n, burst_time, waiting_time, quantum): - """ - Calculate the waiting time of some processes that have a - specified duration time. - Return: The waiting time for each process. - >>> calculate_waiting_time([5, 10, 15]) - [0, 5, 15] - >>> calculate_waiting_time([1, 2, 3, 4, 5]) - [0, 1, 3, 6, 10] - >>> calculate_waiting_time([10, 3]) - [0, 10] - """ - rem_burst_time = [0] * n - for i in range(n): - rem_burst_time[i] = burst_time[i] - t = 0 - while 1: - done = True - for i in range(n): - if rem_burst_time[i] > 0: - done = False - if rem_burst_time[i] > quantum: - t += quantum - rem_burst_time[i] -= quantum - else: - t = t + rem_burst_time[i] - waiting_time[i] = t - burst_time[i] - rem_burst_time[i] = 0 - if done is True: - break - - -def calculate_turn_around_time( - processes, n, burst_time, waiting_time, turn_around_time -): - - """ - Calculate 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_times([5, 10, 15], [0, 5, 15]) - [5, 15, 30] - >>> calculate_turnaround_times([1, 2, 3, 4, 5], [0, 1, 3, 6, 10]) - [1, 3, 6, 10, 15] - >>> calculate_turnaround_times([10, 3], [0, 10]) - [10, 13] - """ - - for i in range(n): - turn_around_time[i] = burst_time[i] + waiting_time[i] - - -def calculate_avg_time(processes, n, burst_time, quantum): - """ - This function calculates the average of the waiting times - Return: The average of the waiting times. - >>> calculate_average_waiting_time([0, 5, 16]) - 7.0 - >>> calculate_average_waiting_time([1, 5, 8, 12]) - 6.5 - >>> calculate_average_waiting_time([10, 24]) - 17.0 - """ - waiting_time = [0] * n - turn_around_time = [0] * n - calculate_waiting_time(processes, n, burst_time, waiting_time, quantum) - calculate_turn_around_time(processes, n, burst_time, waiting_time, turn_around_time) - print("Processes Burst Time Waiting Time Turn-Around Time") - total_waiting_time = 0 - total_turn_around_time = 0 - 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( - " ", - i + 1, - "\t\t", - burst_time[i], - "\t\t", - waiting_time[i], - "\t\t", - turn_around_time[i], - ) - print("\nAverage waiting time = %.5f " % (total_waiting_time / n)) - print("Average turn around time = %.5f " % (total_turn_around_time / n)) - - -if __name__ == "__main__": - # Process id - processes = [1, 2, 3] - n = 3 - burst_time = [10, 5, 8] - quantum = 2 - calculate_avg_time(processes, n, burst_time, quantum) From 39d017f4cf17921d534b6e3ba5fc63bd2469cdb2 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 3 Jul 2020 14:26:52 +0200 Subject: [PATCH 12/15] Update round_robin.py --- scheduling/round_robin.py | 121 ++++++++++---------------------------- 1 file changed, 32 insertions(+), 89 deletions(-) diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py index a12e5e3badb6..d1bb784f6c60 100755 --- a/scheduling/round_robin.py +++ b/scheduling/round_robin.py @@ -1,117 +1,60 @@ """ -Round Robin is a CPU scheduling algorithm. +Round Robin is a scheduling algorithm. In Round Robin each process is assigned a fixed time slot in a cyclic way. https://en.wikipedia.org/wiki/Round-robin_scheduling """ +from statistics import mean +from typing import List -def calculate_waiting_time(burst_time: int) -> int: +def calculate_waiting_times(burst_times: List[int]) -> List[int]: """ - Calculate the waiting time of some processes that have a - specified duration time. - Return: The waiting time for each process. - >>> calculate_waiting_time([10, 5, 8]) + Calculate the waiting times of a list of processes that have a specified duration. + + Return: The waiting time for each process. + >>> calculate_waiting_times([10, 5, 8]) [13, 10, 13] - >>> calculate_waiting_time([4, 6, 3, 1]) - [4, 7, 8] - >>> calculate_waiting_time([12, 2, 10]) + >>> calculate_waiting_times([4, 6, 3, 1]) + [5, 8, 9, 6] + >>> calculate_waiting_times([12, 2, 10]) [12, 2, 12] """ - n = 3 - waiting_time = [0] * n quantum = 2 - rem_burst_time = [0] * n - for i in range(n): - rem_burst_time[i] = burst_time[i] + rem_burst_times = [i for i in burst_times] + waiting_times = [0] * len(burst_times) t = 0 while 1: done = True - for i in range(n): - if rem_burst_time[i] > 0: + for i, burst_time in enumerate(burst_times): + if rem_burst_times[i] > 0: done = False - if rem_burst_time[i] > quantum: + if rem_burst_times[i] > quantum: t += quantum - rem_burst_time[i] -= quantum + rem_burst_times[i] -= quantum else: - t = t + rem_burst_time[i] - waiting_time[i] = t - burst_time[i] - rem_burst_time[i] = 0 + t += rem_burst_times[i] + waiting_times[i] = t - burst_times[i] + rem_burst_times[i] = 0 if done is True: - return waiting_time + return waiting_times -def calculate_turn_around_time(burst_time: int, waiting_time: int) -> int: - +def calculate_turn_around_times(burst_times: List[int], waiting_times: List[int]) -> List[int]: """ - Calculate the turnaround time of some processes. - Return: The time difference between the completion time and the - arrival time. - Practically waiting_time + duration_time - >>> calculate_turn_around_time([5, 10, 15], [8, 13, 15]) - [13, 23, 30] - >>> calculate_turn_around_time([1, 2, 3, 4], [0, 1, 3]) + >>> calculate_turn_around_times([1, 2, 3, 4], [0, 1, 3]) [1, 3, 6] - >>> calculate_turn_around_time([10, 3, 7], [10, 6, 11]) + >>> calculate_turn_around_times([10, 3, 7], [10, 6, 11]) [20, 9, 18] """ - n = 3 - turn_around_time = [0] * n - for i in range(n): - turn_around_time[i] = burst_time[i] + waiting_time[i] - return turn_around_time - - -def calculate_avg_waiting_time(waiting_time: int) -> float: - """ - This function calculates the average of the waiting times - Return: The average of the waiting times. - >>> calculate_avg_waiting_time([10, 6, 11]) - 9.0 - >>> calculate_avg_waiting_time([12, 2, 10]) - 8.0 - >>> calculate_avg_waiting_time([4, 7, 8]) - 6.333333333333333 - """ - return sum(waiting_time) / len(waiting_time) - - -def calculate_avg_turnaround_time(turn_around_time: int) -> float: - """ - This function calculates the average of the waiting times - Return: The average of the waiting times. - >>> calculate_avg_turnaround_time([25, 41, 23]) - 29.666666666666668 - >>> calculate_avg_turnaround_time([10, 3, 7]) - 6.666666666666667 - >>> calculate_avg_turnaround_time([18, 21, 12]) - 17.0 - """ - return sum(turn_around_time) / len(turn_around_time) + return [burst + waiting for burst, waiting in zip(burst_times, waiting_times)] if __name__ == "__main__": - # Process id - processes = [1, 2, 3] - n = 3 - burst_time = [3, 5, 7] - quantum = 2 - waiting_time = calculate_waiting_time(burst_time) - turn_around_time = calculate_turn_around_time(burst_time, waiting_time) - avg_wating_time = 0 - avg_turn_around_time = 0 - avg_waiting_time = calculate_avg_waiting_time(waiting_time) - avg_turn_around_time = calculate_avg_turnaround_time(turn_around_time) + burst_times = [3, 5, 7] + waiting_times = calculate_waiting_times(burst_times) + turn_around_times = calculate_turn_around_times(burst_times, waiting_times) print("Process ID \tBurst Time \tWaiting Time \tTurnaround Time") - for i in range(n): - print( - " ", - i + 1, - "\t\t", - burst_time[i], - "\t\t", - waiting_time[i], - "\t\t", - turn_around_time[i], - ) - print("\nAverage waiting time = %.5f " % (avg_waiting_time)) - print("Average turn around time = %.5f " % (avg_turn_around_time)) + for i, burst_time in enumerate(burst_times): + print(f" {i + 1}\t\t{burst_time}\t\t{waiting_times[i]}\t\t{turn_around_times[i]}") + print(f"\nAverage waiting time = {mean(waiting_times):.5f}") + print(f"Average turn around time = {mean(turn_around_times):.5f}") From a4244bc6217d03ed02f70fe096df12fec0ad8c30 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 3 Jul 2020 14:38:29 +0200 Subject: [PATCH 13/15] Update round_robin.py --- scheduling/round_robin.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py index d1bb784f6c60..fa768e041b8a 100755 --- a/scheduling/round_robin.py +++ b/scheduling/round_robin.py @@ -10,7 +10,7 @@ def calculate_waiting_times(burst_times: List[int]) -> List[int]: """ Calculate the waiting times of a list of processes that have a specified duration. - + Return: The waiting time for each process. >>> calculate_waiting_times([10, 5, 8]) [13, 10, 13] @@ -20,7 +20,7 @@ def calculate_waiting_times(burst_times: List[int]) -> List[int]: [12, 2, 12] """ quantum = 2 - rem_burst_times = [i for i in burst_times] + rem_burst_times = list(burst_times) waiting_times = [0] * len(burst_times) t = 0 while 1: @@ -39,7 +39,9 @@ def calculate_waiting_times(burst_times: List[int]) -> List[int]: return waiting_times -def calculate_turn_around_times(burst_times: List[int], waiting_times: List[int]) -> List[int]: +def calculate_turn_around_times( + burst_times: List[int], waiting_times: List[int] +) -> List[int]: """ >>> calculate_turn_around_times([1, 2, 3, 4], [0, 1, 3]) [1, 3, 6] @@ -55,6 +57,8 @@ def calculate_turn_around_times(burst_times: List[int], waiting_times: List[int] turn_around_times = calculate_turn_around_times(burst_times, waiting_times) print("Process ID \tBurst Time \tWaiting Time \tTurnaround Time") for i, burst_time in enumerate(burst_times): - print(f" {i + 1}\t\t{burst_time}\t\t{waiting_times[i]}\t\t{turn_around_times[i]}") + print( + f" {i + 1}\t\t{burst_time}\t\t{waiting_times[i]}\t\t{turn_around_times[i]}" + ) print(f"\nAverage waiting time = {mean(waiting_times):.5f}") print(f"Average turn around time = {mean(turn_around_times):.5f}") From 277c209fdcf0b983011bf17209f1dc878b718122 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 3 Jul 2020 14:43:00 +0200 Subject: [PATCH 14/15] Update round_robin.py --- scheduling/round_robin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py index fa768e041b8a..9aadf39f65a8 100755 --- a/scheduling/round_robin.py +++ b/scheduling/round_robin.py @@ -58,7 +58,7 @@ def calculate_turn_around_times( print("Process ID \tBurst Time \tWaiting Time \tTurnaround Time") for i, burst_time in enumerate(burst_times): print( - f" {i + 1}\t\t{burst_time}\t\t{waiting_times[i]}\t\t{turn_around_times[i]}" + f" {i + 1}\t\t {burst_time}\t\t {waiting_times[i]}\t\t {turn_around_times[i]}" ) print(f"\nAverage waiting time = {mean(waiting_times):.5f}") print(f"Average turn around time = {mean(turn_around_times):.5f}") From 5581e58778fefe66ef7b25c49011b743e9d293d6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 3 Jul 2020 14:53:21 +0200 Subject: [PATCH 15/15] Update round_robin.py --- scheduling/round_robin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py index 9aadf39f65a8..10aa3ecab31f 100755 --- a/scheduling/round_robin.py +++ b/scheduling/round_robin.py @@ -58,7 +58,8 @@ def calculate_turn_around_times( print("Process ID \tBurst Time \tWaiting Time \tTurnaround Time") for i, burst_time in enumerate(burst_times): print( - f" {i + 1}\t\t {burst_time}\t\t {waiting_times[i]}\t\t {turn_around_times[i]}" + f" {i + 1}\t\t {burst_time}\t\t {waiting_times[i]}\t\t " + f"{turn_around_times[i]}" ) print(f"\nAverage waiting time = {mean(waiting_times):.5f}") print(f"Average turn around time = {mean(turn_around_times):.5f}")