-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfcfs_allzero_arrival.py
137 lines (90 loc) · 4.04 KB
/
fcfs_allzero_arrival.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
#COMPLETED VERSION - all zero arrival time
import tabulate
def fcfs (processes):
#this function returns a new dictionary containing new information of the processses
#declare output new dictionary
scheduled_processes = []
#counter is to keep track of the current time that has passed for every loop
counter = 0
#looping around the dictionary
for i in processes:
#find the first process
if i['order'] == 1:
#find burst_time
burst_time = i['burst_time']
id = i['id']
order = i['order']
#default waiting time is equal to zero
waiting_time = 0
turnaround_time = burst_time
counter = turnaround_time
#add attributes to new dictionary
scheduled_processes.append({
'id':id,
'order': order,
'burst time': burst_time,
'waiting time': waiting_time,
'turnaround time': turnaround_time,
})
else:
#declaring initial attributes
order = i['order']
process_id = i['id']
arrival_time = i['arrival_time']
#find burst_time
burst_time = i['burst_time']
waiting_time = counter
turnaround_time = waiting_time + burst_time
counter = turnaround_time
# add attributes to new dictionary
scheduled_processes.append({
'id':process_id,
'order': order,
'burst time': burst_time,
'waiting time': waiting_time,
'turnaround time': turnaround_time
})
return scheduled_processes
#find averaage waiting time
def findAverageWaitingTime(scheduled_processes):
n = len(scheduled_processes)
total_waiting_time = 0
for i in scheduled_processes:
waiting_time = i['waiting time']
total_waiting_time =+ waiting_time
average = (total_waiting_time)/n
return average
# find average turnaround time
def findAverageTurnaroundTime(scheduled_processes):
n = len(scheduled_processes) #finding the number of processes
total_turnaround_time = 0 #declare and intiallise total turnaround time
#loop to sum up all turnaround times for processes
for i in scheduled_processes:
turnaround_time = i['turnaround time']
total_turnaround_time =+ turnaround_time
#calculating average turnaround time
average = (total_turnaround_time)/n
return average
def main():
processes = [
{"order": 1,"id": "P1", "arrival_time": 0, "burst_time": 10},
{"order": 2,"id": "P2", "arrival_time": 0, "burst_time": 5},
{"order": 3,"id": "P3", "arrival_time": 0, "burst_time": 8},
{"order": 4,"id": "P4", "arrival_time": 0, "burst_time": 15},
{"order": 5,"id": "P5", "arrival_time": 0, "burst_time": 25},
{"order": 6,"id": "P6", "arrival_time": 0, "burst_time": 13},
{"order": 7,"id": "P7", "arrival_time": 0, "burst_time": 56},
{"order": 8,"id": "P8", "arrival_time": 0, "burst_time": 2},
{"order": 9,"id": "P9", "arrival_time": 0, "burst_time": 76},
{"order": 10,"id": "P10", "arrival_time": 0, "burst_time": 45},
{"order": 11,"id": "P11", "arrival_time": 0, "burst_time": 12}
]
output = fcfs(processes)
header = output[0].keys()
rows = [x.values() for x in output]
print(tabulate.tabulate(rows, header))
averageWaitingTime = str(round(findAverageWaitingTime(output),2))
averageTurnaroundTime = str(round(findAverageTurnaroundTime(output),2))
print("Average Waiting Time:",averageWaitingTime)
print("Average Turnaround Time:",averageTurnaroundTime)
main()