-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrt.py
425 lines (396 loc) · 13.5 KB
/
srt.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import sys
import os
class Process:
def __init__(self, state, proc_id, arrival_time, cpu_burst_time, num_bursts, io_time):
'''
state will be 'READY' or 'RUNNING' or 'BLOCKED'
'''
self.state = state
self.id = proc_id
self.arrival_time = arrival_time
self.cpu_burst_time = cpu_burst_time
self.num_bursts = num_bursts
self.io_time = io_time
'''
calculated parameters
'''
self.cpu_comp_time = self.cpu_burst_time
self.wait_time = 0
self.turnaround_time = self.cpu_burst_time + self.wait_time
def __cmp__(self, other):
if self.cpu_burst_time > other.cpu_burst_time:
return 1
elif self.cpu_burst_time < other.cpu_burst_time:
return -1
else:
if self.io_time > other.io_time:
return 1
elif self.io_time < other.io_time:
return -1
else:
if self.arrival_time > other.arrival_time:
return 1
elif self.arrival_time < other.arrival_time:
return -1
else:
return cmp(self.proc_id, other.proc_id)
def update_wait_time(self,time):
self.wait_time += time
def update_cpu_completion_time(self,time):
self.cpu_comp_time -= time
def update_num_bursts(self):
self.num_bursts -= 1
def get_cpu_burst_time(self):
return self.cpu_burst_time
def get_cpu_completion_time(self):
return self.cpu_comp_time
def get_wait_time(self):
return self.wait_time
def get_turnaround_time(self):
return self.turnaround_time
def printQueue(queue):
if(queue == []):
str = "[Q <empty>]"
else:
str = "[Q"
for q in queue:
str += " " + q.id
str += "]"
return str
def SRT(p):
t_cs = 8
t = 0
queue = []
current = []
busy = 0
done = []
num = {}
start_time = 0
end_time = 0
time_left=0
cpu_start_t = 0
ioend_time = {}
time_left_arr = []
tmp = None
io_time_beg=0
num_pre=0
num_processes=0
for x in p:
num[x.id] = x.num_bursts
num_processes +=1
print("time %dms: Simulator started for SRT %s" %(t, printQueue(queue)))
if num_processes ==1:
while(1):
if(sum(num.values()) == 0):
break
for x in p:
if(x.arrival_time == t):
# print("arrived", x.id)
if t == 0:
queue.append(x)
print("time " + str(t) + "ms: Process " + x.id + " arrived and added to ready queue " + printQueue(queue))
else:
tmp = current.pop()
if x.cpu_burst_time < tmp.cpu_burst_time- (t-cpu_start_t) :
time_left = tmp.cpu_burst_time- (t-cpu_start_t)
print("time %dms: Process %s arrived and will preempt %s %s" %(t, x.id, tmp.id, printQueue(queue)))
t += t_cs
start_time = t
end_time = start_time + x.cpu_burst_time
ioend_time[x.id] = end_time + 4 + x.io_time
busy = 1
queue.append(tmp)
if time_left > 0:
print("time " + str(t) + "ms: Process " + x.id + " started using the CPU with " + str(time_left) + "ms remaining " + printQueue(queue))
else:
print("time " + str(t) + "ms: Process " + x.id + " started using the CPU " + printQueue(queue))
cpu_start_t = t
current.append(x)
else:
queue.append(x)
print("time " + str(t) + "ms: Process " + x.id + " arrived and added to ready queue " + printQueue(queue))
if(busy == 0):
# print("started using cpu", x.id)
# tmp2 = current.pop()
# print("current", tmp2)
if(queue != []):
t += 4
start_time = t
tmp = queue.pop(0)
end_time = start_time + tmp.cpu_burst_time
ioend_time[tmp.id] = end_time + 4 + tmp.io_time
busy = 1
if time_left > 0:
print("time " + str(t) + "ms: Process " + tmp.id + " started using the CPU with " + str(time_left) + "ms remaining " + printQueue(queue))
else:
print("time " + str(t) + "ms: Process " + tmp.id + " started using the CPU " + printQueue(queue))
current.append(tmp)
cpu_start_t = t
if(time_left+t < t+tmp.arrival_time):
# print("end time change", x.id)
end_time = t+tmp.arrival_time
if(t == end_time):
# print("now is end time", x.id)
busy = 0
if current != []:
tmp = current.pop(0)
# else:
# if q
# tmp = queue.pop(0)
num[tmp.id] -= 1
# print(str(end_time))
# print(str(time_left))
# print(cpu_start_t)
# print("end time"+str(end_time))
if(num[tmp.id] == 0):
# print("time " + str(t) + "ms: FIX-Process " + tmp.id + " terminated " + printQueue(queue))
# current.remove(tmp)
if queue != []:
del queue[0]
t = t+time_left
print("time " + str(t) + "ms: Process " + tmp.id + " terminated " + printQueue(queue))
t +=3
# elif(num[tmp.id] == 1):
elif(num[tmp.id]==1):
print("time " + str(t) + "ms: Process " + tmp.id + " completed a CPU burst; " + str(num[tmp.id]) + " burst to go " + printQueue(queue))
print("time " + str(t) + "ms: Process " + tmp.id + " switching out of CPU; will block on I/O until time " + str(ioend_time[tmp.id]) + "ms " + printQueue(queue))
t += 3
elif(num[tmp.id]>1):
if current != []:
tmp = current.pop()
print("time " + str(t) + "ms: Process " + tmp.id + " completed a CPU burst; " + str(num[tmp.id]) + " bursts to go " + printQueue(queue))
print("time " + str(t) + "ms: Process " + tmp.id + " switching out of CPU; will block on I/O until time " + str(ioend_time[tmp.id]) + "ms " + printQueue(queue))
t += 3
else:
print("done")
for x in p:
if(x.id in ioend_time):
if(t == ioend_time[x.id] and (x not in queue)):
if current != []:
tmp = current.pop()
if x.cpu_burst_time < time_left and t!=end_time :
print("time %dms: Process %s completed I/O and will preempt %s %s" %(t, x.id, tmp.id,printQueue(queue)))
t += t_cs
start_time = t
end_time = start_time + x.cpu_burst_time
ioend_time[x.id] = end_time + 4 + x.io_time
busy = 1
# queue.append(tmp)
queue.insert(0,tmp)
time_left = time_left-(t-cpu_start_t)+t_cs
if time_left > 0:
print("time " + str(t) + "ms: Process " + x.id + " started using the CPU with " + str(time_left) + "ms remaining " + printQueue(queue))
else:
print("time " + str(t) + "ms: Process " + x.id + " started using the CPU " + printQueue(queue))
cpu_start_t = t
current.append(x)
else:
queue.append(x)
print("time " + str(t) + "ms: Process " + x.id + " completed I/O; added to ready queue " + printQueue(queue))
t -= 1
t += 1
else:
while(1):
if(sum(num.values()) == 0):
break
for x in p:
if(t ==x.arrival_time):
if t == 0:
queue.append(x)
print("time " + str(t) + "ms: Process " + x.id + " arrived and added to ready queue " + printQueue(queue))
else:
tmp = current.pop()
if x.cpu_burst_time < tmp.cpu_burst_time- (t-cpu_start_t) :
time_left = tmp.cpu_burst_time- (t-cpu_start_t)
time_left_arr.append(time_left)
print("time %dms: Process %s arrived and will preempt %s %s" %(t, x.id, tmp.id, printQueue(queue)))
num_pre +=1
t += t_cs
start_time = t
end_time = start_time + x.cpu_burst_time
ioend_time[x.id] = end_time + 4 + x.io_time
busy = 1
queue.append(tmp)
print("time " + str(t) + "ms: Process " + x.id + " started using the CPU " + printQueue(queue))
cpu_start_t = t
current.append(x)
else:
queue.append(x)
print("time " + str(t) + "ms: Process " + x.id + " arrived and added to ready queue " + printQueue(queue))
if(busy == 0):
# print("not busy", str(time_left))
if(queue != []):
t += 4
start_time = t
tmp = queue.pop(0)
end_time = start_time + tmp.cpu_burst_time
ioend_time[tmp.id] = end_time + 4 + tmp.io_time
busy = 1
'''errornous one that shows time is here'''
# print("time left", str(time_left))
if time_left_arr != []:
# print("here")
time_left = time_left_arr.pop()
# if current != []:
# current_one = current.pop(0)
# # print("current", current_one.id)
# print("time elft", str(time_left))
if time_left > 0:
print("time " + str(t) + "ms: Process " + tmp.id + " started using the CPU with " + str(time_left) + "ms remaining " + printQueue(queue))
end_time = t+time_left
else:
print("time " + str(t) + "ms: Process " + tmp.id + " started using the CPU " + printQueue(queue))
time_left = tmp.cpu_burst_time
time_left_arr.append(time_left)
current.append(tmp)
cpu_start_t = t
# print(time_left)
# if(time_left+t < t+tmp.arrival_time):
# # print("end time change", x.id)
# end_time = t+tmp.arrival_time
# # print("hi 1?")
# # if(time_left+t < tmp.cpu_burst_time):
# # # end_time = t+time_left
# # end_time = io_time_beg+time_left
# # print("hi?")
# # if(time_left+ t < io_time_beg+)
# if (time_left+t < io_time_beg):
# end_time = t+time_left
if(t == end_time and (x not in done) and num[tmp.id] >= 0) :
# print("end time", str(time_left))
# print("end time", str(end_time))
# print("time left ", str(time_left))
# print("t", str(t))
busy = 0
if current != []:
tmp = current.pop(0)
num[tmp.id] -= 1
# print("tmp", tmp.id)
# print("io end time", str(ioend_time))
# if(x.id in ioend_time):
# if t > ioend_time[x.id]:
# print("hi")
if(num[tmp.id] == 0):
del x
print("time " + str(t) + "ms: Process " + tmp.id + " terminated " + printQueue(queue))
done.append(tmp)
t +=3
time_left = 0
else:
if current != []:
tmp = current.pop()
if num[tmp.id]==1:
print("time " + str(t) + "ms: Process " + tmp.id + " completed a CPU burst; " + str(num[tmp.id]) + " burst to go " + printQueue(queue))
else:
print("time " + str(t) + "ms: Process " + tmp.id + " completed a CPU burst; " + str(num[tmp.id]) + " bursts to go " + printQueue(queue))
ioend_time[x.id] = end_time + 4 + x.io_time
print("time " + str(t) + "ms: Process " + tmp.id + " switching out of CPU; will block on I/O until time " + str(ioend_time[tmp.id]) + "ms " + printQueue(queue))
t += 3
# io_time_beg = t
# end_time = t+time_left
end_time = end_time + 4 + x.io_time
# print("end time io", str(end_time))
# previous.append(tmp)
time_left = 0
''' At each io end time'''
for x in p:
if(x.id in ioend_time):
if(t == ioend_time[x.id] and (x not in queue) and (x not in done)):
# print("iotime", str(time_left))
if current != []:
tmp = current.pop()
if time_left_arr != []:
# print("here")
time_left = time_left_arr.pop()
if time_left+cpu_start_t < ioend_time[x.id]:
busy = 0
num[tmp.id] -= 1
if(num[tmp.id] == 0):
if time_left_arr != []:
# print("here")
time_left = time_left_arr.pop()
t = time_left+cpu_start_t
print("time " + str(t) + "ms: Process " + tmp.id + " terminated " + printQueue(queue))
done.append(tmp)
t +=3
time_left = 0
t +=1
elif(x.cpu_burst_time < time_left and t!=end_time):
print("time %dms: Process %s completed I/O and will preempt %s %s" %(t, x.id, tmp.id,printQueue(queue)))
num_pre +=1
t += t_cs
start_time = t
end_time = start_time + x.cpu_burst_time
ioend_time[x.id] = end_time + 4 + x.io_time
busy = 1
queue.insert(0,tmp)
time_left = time_left-(t-cpu_start_t)+t_cs
time_left_arr.append(time_left)
if time_left >0:
print("time " + str(t) + "ms: Process " + x.id + " started using the CPU " + printQueue(queue))
cpu_start_t = t
current.append(x)
else:
queue.append(x)
print("time " + str(t) + "ms: Process " + x.id + " completed I/O; added to ready queue " + printQueue(queue))
end_time = x.cpu_burst_time+t
t -= 1
t += 1
print("time " + str(t) + "ms: Simulator ended for SRT\n")
return num_pre
if(__name__ == "__main__"):
if((len(sys.argv) != 3) and (len(sys.argv) != 4)):
sys.exit("ERROR: Invalid arguments\nUSAGE: ./a.out <input-file> <stats-output-file> [<rr-add>]")
try:
input_file = open(sys.argv[1], 'r')
except:
sys.exit("ERROR: Invalid input file format")
try:
output_file = open(sys.argv[2], 'w')
except:
sys.exit("ERROR: Invalid output file")
p = []
for line in input_file:
if ((line[0] != '#') and (line[0] != '\n')):
line = line.strip().split('|')
try:
p.append(Process('READY', line[0], int(line[1]), int(line[2]), int(line[3]), int(line[4])))
except:
input_file.close()
sys.exit("ERROR: Invalid input file format")
else:
continue
input_file.close()
# FCFS(p)
num_pre =SRT(p)
# RR(p)
f = open(sys.argv[2], "w+")
cbt = 0.00
wt = 0.00
tt = 0.00
cs = 0
for x in p:
cs += x.num_bursts
cbt += x.cpu_burst_time * x.num_bursts
cbt /= cs
tt = cbt + wt + 8
f.write("Algorithm FCFS\n")
f.write("-- average CPU burst time: %.2f ms\n" %cbt)
f.write("-- average wait time: %.2f ms\n" %wt)
f.write("-- average turnaround time: %.2f ms\n"%tt)
f.write("-- total number of context switches: %d\n" %(cs))
f.write("-- total number of preemptions: 0\n")
# placeholder
f.write("Algorithm SRT\n")
f.write("-- average CPU burst time: %.2f ms\n" %cbt)
f.write("-- average wait time: %.2f ms\n" %wt)
f.write("-- average turnaround time: %.2f ms\n"%tt)
f.write("-- total number of context switches: %d\n" %(cs))
f.write("-- total number of preemptions: %d\n" %(num_pre))
f.write("Algorithm RR\n")
f.write("-- average CPU burst time: %.2f ms\n" %cbt)
f.write("-- average wait time: %.2f ms\n" %wt)
f.write("-- average turnaround time: %.2f ms\n"%tt)
f.write("-- total number of context switches: %d\n" %(cs))
f.write("-- total number of preemptions: %d\n" %(num_pre))
f.close()