-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLFQ.py
411 lines (349 loc) · 15 KB
/
MLFQ.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
import random
import struct
import time
import statistics
class Process:
def __init__(self,name,arrival_time,burst_time,priority=0):
self.name=name
self.arrival_time=arrival_time
self.burst_time=burst_time
self.priority=priority
self.remaining_time=burst_time
self.start_time= None
self.end_time=None
self.waiting_time=None
self.turnaround_time=None
self.response_time=None
self.is_completed=False
self.is_started = False
self.state = 'not_started' # not_started, ready, running, waiting, completed
def complete(self,t):
self.end_time=t
self.turnaround_time=self.end_time-self.arrival_time
self.waiting_time=self.turnaround_time-self.burst_time
self.is_completed=True
class Queue:
def __init__(self,MLFQ,max_burst_time,priority):
self.max_burst_time = max_burst_time
self.priority = priority
self.processes = []
self.next_queue = None
self.last_queue = None
self.is_completed = True
self.MLFQ = MLFQ
self.last = False
def add_process(self,process):
self.processes.append(process)
self.is_completed = False
def remove_process(self):
self.processes.pop(0)
if len(self.processes) == 0:
self.is_completed = True
def get_process(self):
return self.processes[0]
def complete_process(self):
process = self.get_process()
process.complete(self.MLFQ.time)
self.remove_process()
class MLFQ: #Multilevel Feedback Queue Scheduling
def __init__(self,queues,processes,end,i,plot= True):
self.name = ''
self.i = i
for q in queues:
self.name += str(q)
self.name += '-'
self.init_processes(processes)
self.init_queues(queues)
self.time = 0
self.complete_processes = []
self.n = len(self.processes)
self.end = end
self.do_plot = plot
#-------
self.arrival_times = [0 for _ in range(end)]
for p in processes:
self.arrival_times[p[0]] = p[1]
self.queues_history = [[] for _ in range(len(queues))]
self.complete_processes_history = []
self.avarage_waiting_time = []
self.avarage_turnaround_time = []
self.avarage_response_time = []
self.avarage_burst_time = []
def init_queues(self,queues):
self.queues = []
for q in queues:
self.add_queue(Queue(self,q,0))
def add_queue(self,queue):
if len(self.queues) == 0:
self.queues.append(queue)
self.queues[0].last = True
else:
self.queues.append(queue)
self.queues[-1].last = True
self.queues[-2].last = False
self.queues[-2].next_queue = queue
def init_processes(self,processes):
self.processes = []
for p in range(len(processes)):
process = Process(p, processes[p][0],processes[p][1],0)
self.processes.append(process)
def check_processes(self):
if len(self.processes) > 0:
if self.time >= self.processes[0].arrival_time:
self.processes[0].state = 'ready'
self.processes[0].waiting_time = 0
self.processes[0].response_time = 0
self.processes[0].turnaround_time = 0
self.queues[0].add_process(self.processes[0])
self.processes.pop(0)
def get_highest_priority_queue(self):
for queue in self.queues:
if len(queue.processes) > 0:
return queue
return None
def update_processes_times(self):
for queue in self.queues:
for process in queue.processes:
#not started , ready, running, waiting, completed
if process.state == 'not_started':
pass
elif process.state == 'ready':
process.waiting_time += 1
process.response_time += 1
process.turnaround_time += 1
elif process.state == 'running':
process.remaining_time -= 1
process.turnaround_time += 1
elif process.state == 'waiting':
process.waiting_time += 1
process.turnaround_time += 1
elif process.state == 'completed':
pass
def run(self):
self.current_queue = self.queues[0]
self.current_process = None
self.cpu_state = 'free'
while len(self.complete_processes) != self.n:
self.check_processes()
self.current_queue = self.get_highest_priority_queue()
if self.current_queue is not None:
self.cpu_state = 'busy'
turn_time = 0
if self.current_queue.last:
self.current_process = self.current_queue.get_process()
self.current_process.state = 'running'
while self.current_process.remaining_time != 0:
self.update_processes_times()
self.time += 1
self.record()
self.current_process.state = 'completed'
self.current_queue.complete_process()
self.complete_processes.append(self.current_process)
else:
self.current_process = self.current_queue.get_process()
self.current_process.state = 'running'
while turn_time < self.current_queue.max_burst_time:
if not self.current_process.is_started:
self.current_process.start_time = self.time
self.current_process.response_time = self.time - self.current_process.arrival_time
self.current_process.is_started = True
self.update_processes_times()
self.time += 1
self.record()
if self.current_process.remaining_time == 0:
self.current_queue.complete_process()
self.current_process.state = 'completed'
self.complete_processes.append(self.current_process)
self.cpu_state = 'free'
break
turn_time += 1
if not self.current_process.is_completed:
self.current_process.state = 'waiting'
self.current_queue.next_queue.add_process(self.current_process)
self.current_queue.remove_process()
else:
self.update_processes_times()
self.time += 1
self.record()
if self.do_plot: self.plot()
return self.avarage_waiting_time[-1],self.avarage_turnaround_time[-1],self.avarage_response_time[-1],self.avarage_burst_time[-1]
def record(self):
avarage_waiting_time = 0
avarage_waiting_time_n = 0
avarage_turnaround_time = 0
avarage_turnaround_time_n = 0
avarage_response_time = 0
avarage_response_time_n = 0
avarage_burst_time = 0
avarage_burst_time_n = 0
complete_processes_history = len(self.complete_processes)
for i,queue in enumerate(self.queues):
self.queues_history[i].append(len(queue.processes))
self.complete_processes_history.append(len(self.complete_processes))
for queue in self.queues:
for process in queue.processes:
if process.waiting_time != None:
avarage_waiting_time += process.waiting_time
avarage_waiting_time_n += 1
if process.turnaround_time != None:
avarage_turnaround_time += process.turnaround_time
avarage_turnaround_time_n += 1
if process.response_time != None:
avarage_response_time += process.response_time
avarage_response_time_n += 1
if process.burst_time != None:
avarage_burst_time += process.burst_time
avarage_burst_time_n += 1
for process in self.complete_processes:
if process.waiting_time != None:
avarage_waiting_time += process.waiting_time
avarage_waiting_time_n += 1
if process.turnaround_time != None:
avarage_turnaround_time += process.turnaround_time
avarage_turnaround_time_n += 1
if process.response_time != None:
avarage_response_time += process.response_time
avarage_response_time_n += 1
if process.burst_time != None:
avarage_burst_time += process.burst_time
avarage_burst_time_n += 1
if avarage_waiting_time_n != 0:
self.avarage_waiting_time.append(avarage_waiting_time/avarage_waiting_time_n)
else:
self.avarage_waiting_time.append(0)
if avarage_turnaround_time_n != 0:
self.avarage_turnaround_time.append(avarage_turnaround_time/avarage_turnaround_time_n)
else:
self.avarage_turnaround_time.append(0)
if avarage_response_time_n != 0:
self.avarage_response_time.append(avarage_response_time/avarage_response_time_n)
else:
self.avarage_response_time.append(0)
if avarage_burst_time_n != 0:
self.avarage_burst_time.append(avarage_burst_time/avarage_burst_time_n)
else:
self.avarage_burst_time.append(0)
def plot(self):
print('avarage waiting_time:',self.avarage_waiting_time[-1])
print('avarage turnaround_time:',self.avarage_turnaround_time[-1])
print('avarage response_time:',self.avarage_response_time[-1])
print('avarage burst_time:',self.avarage_burst_time[-1])
plt.figure(figsize=(12,8))
plt.subplot(4,1,1)
plt.plot(self.arrival_times,label='burst times')
plt.legend()
plt.ylabel('burst time')
plt.xlabel('time')
plt.subplot(4,1,2)
for q in range(len(self.queues)):
plt.plot(self.queues_history[q],label='Queue '+str(q))
plt.legend()
plt.xlabel('Time')
plt.ylabel('Queue length')
plt.title('Queue length')
plt.subplot(4,1,3)
plt.plot(self.complete_processes_history,label='Processes')
plt.legend()
plt.xlabel('Time')
plt.ylabel('Complete Processes')
plt.title('Complete Processes')
plt.subplot(4,1,4)
plt.plot(self.avarage_waiting_time,label='Waiting time')
plt.plot(self.avarage_turnaround_time,label='Turnaround time')
plt.plot(self.avarage_response_time,label='Response time')
plt.plot(self.avarage_burst_time,label='Burst time')
plt.legend()
plt.xlabel('Time')
plt.ylabel('Time')
plt.title('Processes')
plt.savefig('plots/{}{}.png'.format(self.name,self.i))
plt.show()
class Random:
def __init__(self):
pass
def lastbit(sef,f):
return struct.pack('!f', f)[-1] & 1
def getrandbits(self,k):
"Return k random bits using a relative drift of two clocks."
# assume time.sleep() and time.clock() use different clocks
# though it might work even if they use the same clock
#XXX it does not produce "good" random bits, see below for details
result = 0
for _ in range(k):
time.sleep(0)
result <<= 1
result |= self.lastbit(time.perf_counter()%100)
return result
def randint(self,a, b, n):
"Return random integer in range [a, b], including both end points."
res = []
for _ in range(n):
res.append(a + self.randbelow(b - a + 1))
return np.asarray(res)
def randbelow(self,n):
"Return a random int in the range [0,n). Raises ValueError if n<=0."
# from Lib/random.py
if n <= 0:
raise ValueError
k = n.bit_length() # don't use (n-1) here because n can be 1
r = self.getrandbits(k) # 0 <= r < 2**k
while r >= n: # avoid skew
r = self.getrandbits(k)
return r
def generate_process(start,end,number_of_processes,max_burst_time):
rand = Random()
arrival_times = np.random.randint(start,end,number_of_processes)
burst_times = np.random.randint(1,max_burst_time,number_of_processes)
arrival_times = np.sort(arrival_times)
process = np.array(list((zip(arrival_times,burst_times)))).reshape(number_of_processes,2)
return process
def test_random_generator(l, l_median):
runs, n1, n2 = 0, 0, 0
for i in range(len(l)):
if (l[i] >= l_median and l[i-1] < l_median) or (l[i] < l_median and l[i-1] >= l_median):
runs += 1
if(l[i]) >= l_median:
n1 += 1
else:
n2 += 1
runs_exp = ((2*n1*n2)/(n1+n2))+1
stan_dev = math.sqrt((2*n1*n2*(2*n1*n2-n1-n2))/(((n1+n2)**2)*(n1+n2-1)))
z = (runs-runs_exp)/stan_dev
return z
def main(queues,start,end,number_of_processes,max_burst_time,itteration=1,plot=True):
toltal_scores = np.asarray([0.0,0.0,0.0,0.0])
waiting_hist = []
turnaround_hist = []
response_hist = []
burst_hist = []
for itter in range(itteration):
processes = generate_process(start,end,number_of_processes,max_burst_time)
mlfq = MLFQ(queues,processes,end,itter,plot)
temp = np.asarray(mlfq.run())
waiting_hist.append(temp[0])
turnaround_hist.append(temp[1])
response_hist.append(temp[2])
burst_hist.append(temp[3])
toltal_scores += temp
toltal_scores = toltal_scores/itteration
print('avarage waiting_time:',toltal_scores[0])
print('standard deviation waiting_time:',np.std(waiting_hist))
print('avarage turnaround_time:',toltal_scores[1])
print('standard deviation turnaround_time:',np.std(turnaround_hist))
print('avarage response_time:',toltal_scores[2])
print('standard deviation response_time:',np.std(response_hist))
print('avarage burst_time:',toltal_scores[3])
print('standard deviation burst_time:',np.std(burst_hist))
if __name__ == '__main__':
queues = [int(x) for x in input('queues: ').split(' ')]
start = int(input('start: '))
end = int(input('end: '))
number_of_processes = int(input('number of processes: '))
max_burst_time = int(input('max burst time: '))
itteration = int(input('itteration: '))
plot = int(input('draw plot? (1/0): '))
main(queues,start,end,number_of_processes,max_burst_time,itteration,plot)