-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundRobin.py
executable file
·267 lines (230 loc) · 11.8 KB
/
RoundRobin.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import random
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTableWidgetItem
from matplotlib import pyplot as plt
from matplotlib.backends.backend_template import FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
class RoundRobin:
def processData(self, mainWindow):
process_data = []
global originalWindow
originalWindow = mainWindow
for i in range(0, mainWindow.processTable.rowCount()):
temporary = []
process_id = i
arrival_time = mainWindow.processTable.item(i, 1).text()
burst_time = mainWindow.processTable.item(i, 2).text()
temporary.extend([int(process_id), int(arrival_time), int(burst_time), 0, int(burst_time)])
'''
'0' is the state of the process. 0 means not executed and 1 means execution complete
'''
process_data.append(temporary)
time_slice = 3
RoundRobin.schedulingProcess(self, process_data, time_slice)
def schedulingProcess(self, process_data, time_slice):
start_time = []
exit_time = []
executed_process = []
ready_queue = []
s_time = 0
process_data.sort(key=lambda x: x[1])
'''
Sort processes according to the Arrival Time
'''
while 1:
normal_queue = []
temp = []
for i in range(len(process_data)):
if process_data[i][1] <= s_time and process_data[i][3] == 0:
present = 0
if len(ready_queue) != 0:
for k in range(len(ready_queue)):
if process_data[i][0] == ready_queue[k][0]:
present = 1
'''
The above if loop checks that the next process is not a part of ready_queue
'''
if present == 0:
temp.extend([process_data[i][0], process_data[i][1], process_data[i][2], process_data[i][4]])
ready_queue.append(temp)
temp = []
'''
The above if loop adds a process to the ready_queue only if it is not already present in it
'''
if len(ready_queue) != 0 and len(executed_process) != 0:
for k in range(len(ready_queue)):
if ready_queue[k][0] == executed_process[len(executed_process) - 1]:
ready_queue.insert((len(ready_queue) - 1), ready_queue.pop(k))
'''
The above if loop makes sure that the recently executed process is appended at the end of ready_queue
'''
elif process_data[i][3] == 0:
temp.extend([process_data[i][0], process_data[i][1], process_data[i][2], process_data[i][4]])
normal_queue.append(temp)
temp = []
if len(ready_queue) == 0 and len(normal_queue) == 0:
break
if len(ready_queue) != 0:
if ready_queue[0][2] > time_slice:
'''
If process has remaining burst time greater than the time slice, it will execute for a time period equal to time slice and then switch
'''
start_time.append(s_time)
s_time = s_time + time_slice
e_time = s_time
exit_time.append(e_time)
executed_process.append(ready_queue[0][0])
for j in range(len(process_data)):
if process_data[j][0] == ready_queue[0][0]:
break
process_data[j][2] = process_data[j][2] - time_slice
ready_queue.pop(0)
elif ready_queue[0][2] <= time_slice:
'''
If a process has a remaining burst time less than or equal to time slice, it will complete its execution
'''
start_time.append(s_time)
s_time = s_time + ready_queue[0][2]
e_time = s_time
exit_time.append(e_time)
executed_process.append(ready_queue[0][0])
for j in range(len(process_data)):
if process_data[j][0] == ready_queue[0][0]:
break
process_data[j][2] = 0
process_data[j][3] = 1
process_data[j].append(e_time)
ready_queue.pop(0)
elif len(ready_queue) == 0:
if s_time < normal_queue[0][1]:
s_time = normal_queue[0][1]
if normal_queue[0][2] > time_slice:
'''
If process has remaining burst time greater than the time slice, it will execute for a time period equal to time slice and then switch
'''
start_time.append(s_time)
s_time = s_time + time_slice
e_time = s_time
exit_time.append(e_time)
executed_process.append(normal_queue[0][0])
for j in range(len(process_data)):
if process_data[j][0] == normal_queue[0][0]:
break
process_data[j][2] = process_data[j][2] - time_slice
elif normal_queue[0][2] <= time_slice:
'''
If a process has a remaining burst time less than or equal to time slice, it will complete its execution
'''
start_time.append(s_time)
s_time = s_time + normal_queue[0][2]
e_time = s_time
exit_time.append(e_time)
executed_process.append(normal_queue[0][0])
for j in range(len(process_data)):
if process_data[j][0] == normal_queue[0][0]:
break
process_data[j][2] = 0
process_data[j][3] = 1
process_data[j].append(e_time)
t_time = RoundRobin.calculateTurnaroundTime(self, process_data)
w_time = RoundRobin.calculateWaitingTime(self, process_data)
RoundRobin.printData(self, process_data, t_time, w_time, executed_process)
def calculateTurnaroundTime(self, process_data):
total_turnaround_time = 0
for i in range(len(process_data)):
turnaround_time = process_data[i][5] - process_data[i][1]
'''
turnaround_time = completion_time - arrival_time
'''
total_turnaround_time = total_turnaround_time + turnaround_time
process_data[i].append(turnaround_time)
average_turnaround_time = total_turnaround_time / len(process_data)
'''
average_turnaround_time = total_turnaround_time / no_of_processes
'''
return average_turnaround_time
def calculateWaitingTime(self, process_data):
total_waiting_time = 0
for i in range(len(process_data)):
waiting_time = process_data[i][6] - process_data[i][4]
'''
waiting_time = turnaround_time - burst_time
'''
total_waiting_time = total_waiting_time + waiting_time
process_data[i].append(waiting_time)
average_waiting_time = total_waiting_time / len(process_data)
'''
average_waiting_time = total_waiting_time / no_of_processes
'''
return average_waiting_time
def printData(self, process_data, average_turnaround_time, average_waiting_time, executed_process):
process_data.sort(key=lambda x: x[0])
'''
Sort processes according to the Process ID
'''
'''print(
"Process_ID Arrival_Time Rem_Burst_Time Completed Original_Burst_Time Completion_Time Turnaround_Time Waiting_Time")'''
processIDs = [0] * originalWindow.processTable.rowCount()
originalWindow.gnt.cla()
originalWindow.gnt.grid(True)
for i in range(len(process_data)):
for j in range(len(process_data[i])):
#print(process_data[i][j], end=" ")
if (j == 0):
processIDs[i] = process_data[i][j]
self.getTimeSequence(executed_process, process_data, i)
if (j == 5):
item2 = QTableWidgetItem()
item2.setTextAlignment(Qt.AlignCenter)
item2.setData(Qt.EditRole, process_data[i][j])
originalWindow.processTable.setItem(i, 3, item2)
if (j == 6):
item2 = QTableWidgetItem()
item2.setTextAlignment(Qt.AlignCenter)
item2.setData(Qt.EditRole, process_data[i][j])
originalWindow.processTable.setItem(i, 4, item2)
if (j == 7):
item2 = QTableWidgetItem()
item2.setTextAlignment(Qt.AlignCenter)
item2.setData(Qt.EditRole, process_data[i][j])
originalWindow.processTable.setItem(i, 5, item2)
print()
print(f'Average Turnaround Time: {average_turnaround_time}')
print(f'Average Waiting Time: {average_waiting_time}')
#print(f'Sequence of Processes: {executed_process}')
originalWindow.drawChartAverage(average_turnaround_time, average_waiting_time)
yTicksArray = [i * 10 for i in processIDs]
originalWindow.gnt.set_yticks(yTicksArray)
processIDsIncreased = [i + 1 for i in processIDs]
originalWindow.gnt.set_yticklabels(processIDsIncreased)
originalWindow.canvas = FigureCanvas(originalWindow.figure)
originalWindow.toolbar = NavigationToolbar(originalWindow.canvas, originalWindow)
for i in reversed(range(originalWindow.plotBox.count())):
originalWindow.plotBox.itemAt(i).widget().setParent(None)
originalWindow.plotBox.addWidget(originalWindow.toolbar)
originalWindow.plotBox.addWidget(originalWindow.canvas)
def getTimeSequence(self, sequence_of_process, process_data, PID):
doOnce = False
for i in range(len(sequence_of_process)):
if (sequence_of_process[i] == PID):
if (doOnce == False):
startIn = process_data[0][1] + i
# print("Start In: ", startIn)
doOnce = True
if (doOnce == True and i == len(sequence_of_process) - 1):
endIn = process_data[0][1] + i + 1
# print(" End In: ", endIn)
originalWindow.gnt.broken_barh([(startIn, endIn - startIn)],
(process_data[PID][0] * 10, 10),
facecolors=('tab:' + originalWindow.colorsChart[
random.randrange(len(originalWindow.colorsChart))]))
else:
if (doOnce == True):
endIn = process_data[0][1] + i
# print(" End In: ", endIn)
doOnce = False
originalWindow.gnt.broken_barh([(startIn, endIn - startIn)],
(process_data[PID][0] * 10, 10),
facecolors=('tab:' + originalWindow.colorsChart[
random.randrange(len(originalWindow.colorsChart))]))