|
1 |
| - |
2 |
| - |
3 |
| -# Shortest job remainig first |
4 |
| -# PLease note that arriaval time and burst |
5 |
| -#time are inputted as space separated |
| 1 | +""" |
| 2 | +Shortest job remainig first |
| 3 | +Please note arrival time and burst |
| 4 | +Please use spaces to separate times entered. |
| 5 | +""" |
6 | 6 |
|
7 | 7 | import pandas as pd
|
8 | 8 | from typing import List
|
9 |
| - |
| 9 | + |
10 | 10 |
|
11 | 11 | def calculate_waitingtime(
|
12 |
| - arrival_time: List[int],burst_time: List[int],no_of_processes: int |
| 12 | + arrival_time: List[int], burst_time: List[int], no_of_processes: int |
13 | 13 | ) -> List[int]:
|
14 |
| - |
| 14 | + |
15 | 15 | """
|
16 |
| - This function calculates the Waiting Times of each Processes |
17 |
| - Return: list of Waiting Time. |
| 16 | + Calculate the waiting time of each processes |
| 17 | + Return: list of waiting times. |
18 | 18 | >>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4)
|
19 | 19 | [0, 3, 5, 0]
|
20 | 20 | >>> calculate_waitingtime([1,2,3],[2,5,1],3)
|
21 | 21 | [0, 2, 0]
|
22 | 22 | >>> calculate_waitingtime([2,3],[5,1],2)
|
23 | 23 | [1, 0]
|
24 | 24 | """
|
25 |
| - remaining_time=[0]*no_of_processes |
26 |
| - waiting_time=[0]*no_of_processes |
27 |
| - # Copy the burst time into remaining_time[] |
28 |
| - for i in range(no_of_processes): |
29 |
| - remaining_time[i] =burst_time[i] |
30 |
| - |
| 25 | + remaining_time = [0] * no_of_processes |
| 26 | + waiting_time = [0] * no_of_processes |
| 27 | + # Copy the burst time into remaining_time[] |
| 28 | + for i in range(no_of_processes): |
| 29 | + remaining_time[i] = burst_time[i] |
| 30 | + |
31 | 31 | complete = 0
|
32 | 32 | increment_time = 0
|
33 | 33 | minm = 999999999
|
34 | 34 | short = 0
|
35 | 35 | check = False
|
36 | 36 |
|
37 |
| - # Process until all processes gets |
38 |
| - # completed |
| 37 | + # Process until all processes are completed |
39 | 38 | while complete != no_of_processes:
|
40 | 39 | for j in range(no_of_processes):
|
41 |
| - if arrival_time[j]<=increment_time: |
42 |
| - if remaining_time[j]>0: |
43 |
| - if remaining_time[j]<minm: |
44 |
| - minm=remaining_time[j] |
45 |
| - short=j |
46 |
| - check=True |
47 |
| - |
48 |
| - if check == False: |
| 40 | + if arrival_time[j] <= increment_time: |
| 41 | + if remaining_time[j] > 0: |
| 42 | + if remaining_time[j] < minm: |
| 43 | + minm = remaining_time[j] |
| 44 | + short = j |
| 45 | + check = True |
| 46 | + |
| 47 | + if not check: |
49 | 48 | increment_time += 1
|
50 | 49 | continue
|
51 |
| - remaining_time[short]-=1 |
52 |
| - |
53 |
| - minm = remaining_time[short] |
54 |
| - if minm == 0: |
| 50 | + remaining_time[short] -= 1 |
| 51 | + |
| 52 | + minm = remaining_time[short] |
| 53 | + if minm == 0: |
55 | 54 | minm = 999999999
|
56 |
| - |
57 |
| - if remaining_time[short] == 0: |
| 55 | + |
| 56 | + if remaining_time[short] == 0: |
58 | 57 | complete += 1
|
59 | 58 | check = False
|
60 | 59 |
|
61 |
| - # Find finish time of current |
62 |
| - # process |
| 60 | + # Find finish time of current process |
63 | 61 | finish_time = increment_time + 1
|
64 | 62 |
|
65 |
| - # Calculate waiting time |
66 |
| - finar=finish_time - arrival_time[short] |
67 |
| - waiting_time[short] = (finar - burst_time[short]) |
| 63 | + # Calculate waiting time |
| 64 | + finar = finish_time - arrival_time[short] |
| 65 | + waiting_time[short] = finar - burst_time[short] |
68 | 66 |
|
69 |
| - if waiting_time[short] < 0: |
| 67 | + if waiting_time[short] < 0: |
70 | 68 | waiting_time[short] = 0
|
71 |
| - |
72 |
| - # Increment time |
| 69 | + |
| 70 | + # Increment time |
73 | 71 | increment_time += 1
|
74 | 72 | return waiting_time
|
| 73 | + |
| 74 | + |
75 | 75 | def calculate_turnaroundtime(
|
76 | 76 | burst_time: List[int], no_of_processes: int, waiting_time: List[int]
|
77 |
| -) -> List[int]: |
| 77 | +) -> List[int]: |
78 | 78 | """
|
79 |
| - This function calculates the Turn Around Times of each Processes |
80 |
| - Return: list of Turn Around Time. |
| 79 | + Calculate the turn around time of each Processes |
| 80 | + Return: list of turn around times. |
81 | 81 | >>> calculate_turnaroundtime([3,3,5,1], 4, [0,3,5,0])
|
82 | 82 | [3, 6, 10, 1]
|
83 | 83 | >>> calculate_turnaroundtime([3,3], 2, [0,3])
|
84 | 84 | [3, 6]
|
85 | 85 | >>> calculate_turnaroundtime([8,10,1], 3, [1,0,3])
|
86 | 86 | [9, 10, 4]
|
87 | 87 | """
|
88 |
| - turn_around_time=[0]*no_of_processes |
89 |
| - for i in range(no_of_processes): |
90 |
| - turn_around_time[i] = burst_time[i] + waiting_time[i] |
| 88 | + turn_around_time = [0] * no_of_processes |
| 89 | + for i in range(no_of_processes): |
| 90 | + turn_around_time[i] = burst_time[i] + waiting_time[i] |
91 | 91 | return turn_around_time
|
| 92 | + |
| 93 | + |
92 | 94 | def calculate_average_times(
|
93 |
| - waiting_time: List[int],turn_around_time: List[int], no_of_processes: int |
94 |
| -): |
| 95 | + waiting_time: List[int], turn_around_time: List[int], no_of_processes: int |
| 96 | +): |
95 | 97 | """
|
96 | 98 | This function calculates the average of the waiting & turnaround times
|
97 | 99 | Prints: Average Waiting time & Average Turn Around Time
|
98 | 100 | >>> calculate_average_times([0,3,5,0],[3,6,10,1],4)
|
99 |
| - Average waiting time = 2.00000 |
100 |
| - Average turn around time = 5.0 |
| 101 | + Average waiting time = 2.00000 |
| 102 | + Average turn around time = 5.0 |
101 | 103 | >>> calculate_average_times([2,3],[3,6],2)
|
102 |
| - Average waiting time = 2.50000 |
103 |
| - Average turn around time = 4.5 |
| 104 | + Average waiting time = 2.50000 |
| 105 | + Average turn around time = 4.5 |
104 | 106 | >>> calculate_average_times([10,4,3],[2,7,6],3)
|
105 |
| - Average waiting time = 5.66667 |
106 |
| - Average turn around time = 5.0 |
| 107 | + Average waiting time = 5.66667 |
| 108 | + Average turn around time = 5.0 |
107 | 109 | """
|
108 | 110 | total_waiting_time = 0
|
109 | 111 | total_turn_around_time = 0
|
110 |
| - for i in range(no_of_processes): |
111 |
| - total_waiting_time = total_waiting_time + waiting_time[i] |
112 |
| - total_turn_around_time = total_turn_around_time + turn_around_time[i] |
113 |
| - print("Average waiting time = %.5f "%( |
114 |
| - total_waiting_time /no_of_processes) ) |
115 |
| - print("Average turn around time = ", |
116 |
| - total_turn_around_time / no_of_processes) |
117 |
| - |
| 112 | + for i in range(no_of_processes): |
| 113 | + total_waiting_time = total_waiting_time + waiting_time[i] |
| 114 | + total_turn_around_time = total_turn_around_time + turn_around_time[i] |
| 115 | + print("Average waiting time = %.5f" % (total_waiting_time / no_of_processes)) |
| 116 | + print("Average turn around time =", total_turn_around_time / no_of_processes) |
| 117 | + |
| 118 | + |
118 | 119 | if __name__ == "__main__":
|
119 |
| - |
120 |
| - print("Enter How Many Process You want To Enter") |
121 |
| - no_of_processes=int(input()) |
122 |
| - burst_time=[0]*no_of_processes |
123 |
| - arrival_time=[0]*no_of_processes |
124 |
| - processes=list(range(1,no_of_processes+1)) |
| 120 | + print("Enter how many process you want to analyze") |
| 121 | + no_of_processes = int(input()) |
| 122 | + burst_time = [0] * no_of_processes |
| 123 | + arrival_time = [0] * no_of_processes |
| 124 | + processes = list(range(1, no_of_processes + 1)) |
125 | 125 |
|
126 | 126 | for i in range(no_of_processes):
|
127 |
| - print("Enter The Arrival time and Brust time for Process:--"+str(i+1)) |
128 |
| - arrival_time[i], burst_time[i]=map(int,input().split()) |
129 |
| - waiting_time=calculate_waitingtime(arrival_time,burst_time,no_of_processes) |
130 |
| - bt=burst_time |
131 |
| - n=no_of_processes |
132 |
| - wt=waiting_time |
133 |
| - turn_around_time=calculate_turnaroundtime(bt,n,wt) |
134 |
| - calculate_average_times(waiting_time,turn_around_time, no_of_processes) |
135 |
| - processes=list(range(1,no_of_processes+1)) |
136 |
| - fcfs=pd.DataFrame(list(zip(processes,burst_time, |
137 |
| - arrival_time,waiting_time,turn_around_time)), |
138 |
| - columns=['Process','BurstTime','ArrivalTime','WaitingTime','TurnAroundTime']) |
139 |
| - |
140 |
| - # Printing the dataFrame |
141 |
| - pd.set_option('display.max_rows', fcfs.shape[0]+1) |
142 |
| - print(fcfs) |
| 127 | + print("Enter the arrival time and brust time for process:--" + str(i + 1)) |
| 128 | + arrival_time[i], burst_time[i] = map(int, input().split()) |
| 129 | + waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes) |
| 130 | + bt = burst_time |
| 131 | + n = no_of_processes |
| 132 | + wt = waiting_time |
| 133 | + turn_around_time = calculate_turnaroundtime(bt, n, wt) |
| 134 | + calculate_average_times(waiting_time, turn_around_time, no_of_processes) |
| 135 | + processes = list(range(1, no_of_processes + 1)) |
| 136 | + fcfs = pd.DataFrame( |
| 137 | + list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), |
| 138 | + columns=[ |
| 139 | + "Process", |
| 140 | + "BurstTime", |
| 141 | + "ArrivalTime", |
| 142 | + "WaitingTime", |
| 143 | + "TurnAroundTime", |
| 144 | + ], |
| 145 | + ) |
| 146 | + |
| 147 | + # Printing the dataFrame |
| 148 | + pd.set_option("display.max_rows", fcfs.shape[0] + 1) |
| 149 | + print(fcfs) |
0 commit comments