-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcfs.py
243 lines (236 loc) · 7.69 KB
/
fcfs.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
'''
Zhiying Jiang, jiangz6
Tianyi Zhang, zhangt17
Angela Su, sua2
'''
import sys
import Queue
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', type can be 'arrive' or 'fio' (finish io)
'''
self.state = state
self.id = proc_id
self.arrival_time = int(arrival_time)
self.cpu_burst_time = int(cpu_burst_time)
self.num_bursts = int(num_bursts)
self.io_time = int(io_time)
self.io = False
self.type = 'arrive'
'''
calculated parameters
'''
self.cpu_start_time = 0
self.cpu_comp_time = self.cpu_burst_time
self.single_wait_time = 0
self.wait_time = 0
self.io_start_time = 0
self.io_finish_time = 0
self.finished_time = 0
self.turnaround_time = 0
self.burst_time = 0
self.running_time = 0
self.first = True
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_turnaround_time(self,time):
self.turnaround_time = time
def update_wait_time(self,time):
self.wait_time += time
def update_turnaround_time(self,time):
self.turnaround_time += time
def update_cpu_completion_time(self,time):
self.cpu_comp_time -= time
def update_num_bursts(self):
self.num_bursts -= 1
def update_burst_time(self):
self.burst_time += self.cpu_burst_time
def update_finished_time(self,time):
self.finished_time = time
def set_cpu_completion_time(self,time):
self.cpu_comp_time = time
def set_wait_time(self,time):
self.wait_time = time
def get_cpu_burst_time(self):
return self.cpu_burst_time
def get_cpu_completion_time(self):
return self.cpu_burst_time - self.running_time
def get_wait_time(self):
return self.wait_time
def get_turnaround_time(self):
return self.turnaround_time
def get_io_time(self):
return self.io_time
def add_wait_time(queue,p,time):
new_queue = []
for process in queue:
if process.id!=p.id:
process.update_wait_time(time)
new_queue.append(process)
else:
new_queue.append(process)
return new_queue
def printQueue(queue):
if(queue == []):
str = "[Q <empty>]"
else:
str = "[Q"
for q in queue:
str += " " + q.id
str += "]"
return str
def FCFS(f,queue, t_cs = 8):
time = 0
print "time 0ms: Simulator started for FCFS [Q <empty>]"
num_process = len(queue)
ready_queue = []
context_switch = False
using_cpu = -1
num_preemption = 0
num_context_switch = 0
num_burst = 0
switch_start = 0
switch_end = 0
stop_cpu_time = 0
slice_now = 0
while True:
io = -1
for i in range(num_process):
if queue[i].io:
io = i
if using_cpu<0 and io<0 and time > sys.maxint and ready_queue==[]:
break
elif using_cpu<0 and ready_queue!=[] and time >= sys.maxint+t_cs/2:
context_switch = True
if switch_start == 0:
switch_start = time-1
if ready_queue!=[]:
if context_switch and (time == sys.maxint+t_cs/2 or (time-switch_start) == t_cs):
num_context_switch+=1
process = ready_queue.pop(0)
using_cpu = queue.index(process)
print "time %dms: Process %s started using the CPU %s"%(time, queue[using_cpu].id, printQueue(ready_queue))
context_switch = False
queue[using_cpu].update_wait_time(queue[using_cpu].single_wait_time)
queue[using_cpu].update_turnaround_time(queue[using_cpu].single_wait_time)
queue[using_cpu].cpu_start_time = time
queue[using_cpu].single_wait_time = 0
slice_now = 0
queue[using_cpu].wait_time -= t_cs/2
switch_start = 0
p = using_cpu
incomplete = True
if using_cpu>=0:
if queue[p].running_time-queue[p].cpu_start_time+time == queue[p].get_cpu_burst_time():
incomplete = False
stop_cpu_time = time
num_burst += 1
queue[p].update_turnaround_time(queue[p].get_cpu_burst_time()+t_cs/2)
queue[p].update_burst_time()
queue[p].update_num_bursts()
queue[p].cpu_start_time = 0
queue[p].running_time = 0
using_cpu = -1
if queue[p].num_bursts == 0:
print "time %dms: Process %s terminated %s"%(time, queue[p].id, printQueue(ready_queue))
else:
if queue[p].num_bursts > 1:
print "time %dms: Process %s completed a CPU burst; %d bursts to go %s"%(time, queue[p].id, queue[p].num_bursts, printQueue(ready_queue))
else:
print "time %dms: Process %s completed a CPU burst; %d burst to go %s"%(time, queue[p].id, queue[p].num_bursts, printQueue(ready_queue))
if queue[p].io_time>0:
queue[p].io = True
queue[p].io_finish_time = time + queue[p].io_time + t_cs/2
print "time %dms: Process %s switching out of CPU; will block on I/O until time %dms %s"%(time, queue[p].id, queue[p].io_finish_time,printQueue(ready_queue))
else:
ready_queue.append(queue[p])
for i in range(num_process):
if queue[i].io and time==queue[i].io_finish_time:
queue[i].io_finish_time = 0
if ready_queue==[] and queue[i].num_bursts>0 and using_cpu==-1:
context_switch=True
if time - stop_cpu_time <= t_cs/2:
switch_start = time
else:
switch_start = time - t_cs/2
ready_queue.append(queue[i])
print "time %dms: Process %s completed I/O; added to ready queue %s"%(time,queue[i].id,printQueue(ready_queue))
queue[i].io = False
for k in range(num_process):
if queue[k].arrival_time<=time and queue[k].first:
p = queue[k]
ready_queue.append(queue[k])
queue[k].single_wait_time=0
queue[k].first=False
print "time %dms: Process %s arrived and added to ready queue %s"%(p.arrival_time,p.id,printQueue(ready_queue))
for j in range(num_process):
if using_cpu != j and (not queue[j].io):
queue[j].single_wait_time+=1
time+=1
if ready_queue==[] and context_switch:
context_switch = False
switch_start = time
print "time %dms: Simulator ended for FCFS"%(time+t_cs/2-1)
wait_time = 0
burst_time = 0
turnaround_time = 0
for i in range(num_process):
wait_time += queue[i].wait_time
burst_time += queue[i].burst_time
turnaround_time += queue[i].turnaround_time
wait_time -= num_preemption*(t_cs/2)
if num_burst!=0:
wait_time /= float(num_burst)
burst_time /= float(num_burst)
turnaround_time /= float(num_burst)
else:
wait_time /= float(num_process)
burst_time /= float(num_process)
turnaround_time /= float(num_process)
write_result(f,'FCFS',burst_time,wait_time,turnaround_time,num_context_switch,num_preemption)
def write_result(f,alg,bt,wt,tt,nc,np):
f.write("Algorithm %s\n-- average CPU burst time: %.2f ms\n-- average wait time: %.2f ms\n-- average turnaround time: %.2f ms\n-- total number of context switches: %d\n-- total number of preemptions: %d\n"%(alg,bt,wt,tt,nc,np))
if __name__ == "__main__":
if len(sys.argv)!=4 and len(sys.argv)!=3:
sys.exit("ERROR: Invalid arguments\nUsage: ./a.out <input-file> <stats-output-file> [<rr-add>]")
input_file = os.getcwd()+'/'+sys.argv[1]
output_file = sys.argv[2]
out = open(output_file,'w')
if len(sys.argv)==4:
rr_add = sys.argv[3]
else:
rr_add = 'END'
queue = Queue.PriorityQueue()
rr_queue = []
try:
f = open(input_file)
for line in f:
line = line.strip()
if line and not line.startswith('#'):
ele = line.split('|')
proc_id,arrival_time,cpu_burst_time,num_bursts,io_time = ele
if int(arrival_time)<sys.maxint:
sys.maxint = int(arrival_time)
proc = Process('READY', proc_id, arrival_time, cpu_burst_time, num_bursts, io_time)
queue.put(proc)
rr_queue.append(proc)
FCFS(out,rr_queue)
except ValueError as e:
sys.exit("ERROR: Invalid input file format")