This repository has been archived by the owner on Oct 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcfs.py
56 lines (42 loc) · 2.06 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
# first come first serve algorithm
# import sleep function
# this is for mimicking actual waiting time
# (to see how long it takes to finish all the processes)
from time import sleep
# main algorithm function
# variable processes holds a chain (list) of processes (objects) to iterate over
# variable time sets waiting time / time scale of waiting
# variable verbose sets if the algorithm outputs verbose info about what it is doing
def fcfs(processes, time, verbose="n"):
total_wait = 0
# set service times of every process in the chain
for i in range(1, len(processes)):
# service time of the process equals to sum of service and execute time of previous process
processes[i].serviceTime = processes[i - 1].serviceTime + processes[i - 1].executeTime
# iterate over processes from input
for i in processes:
# calculate wait time for current process
# if we assume processes arrive at the same time -> wait time is equal to service time
i.waitTime = i.serviceTime # - i.arrivalTime
total_wait += i.waitTime
# add current process to the end of output file
with open("fcfs_out.csv", "a") as f:
f.write("{0}\t{1}\t{2}\t{3}\n".format(i.arrivalTime, i.executeTime, i.serviceTime, i.waitTime))
# verbosity check and execution if needed
if verbose != "n":
print("Now executing:", i.arrivalTime, i.executeTime, i.serviceTime, i.waitTime, sep='\t')
# mimicking execution of process
sleep(i.executeTime * time)
# calculate average wait time for current chain
avg_wait = total_wait / len(processes)
# add average wait time of current chain to end of output file
with open("fcfs_out.csv", "a") as f:
f.write("\t\t\t{0}\n".format(avg_wait))
# print and return average wait time
print("Avg wait:", avg_wait, sep='\t')
return avg_wait
# function that clears output of the algorithm
def clear_results():
# opens the file in write - only mode which overwrites contents of the file
with open("fcfs_out.csv", "w"):
pass