-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCFS.py
98 lines (80 loc) · 2.61 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
import heapq
import matplotlib.pyplot as plt
import numpy as np
def solve():
print("Enter number of processes ", end='')
process = int(input())
arrival = []
burst = []
procompleted = [0] * process
finish = []
p = [] # priority queue arrival number
for i in range(process):
print("Enter arrival time ", end='')
arrival.append(int(input()))
print("Enter burst time ", end='')
burst.append(int(input()))
heapq.heappush(p, [arrival[i], i])
completed = 0
v = [] * process # Answer
time = 0
while True:
if p[0][1] <= time:
running_process = heapq.heappop(p)
print(running_process)
total = burst[running_process[1]] + time
# print(running_process[0] , " " , running_process[1] , " " , running_process[2])
while time < total:
temp = []
for i in range(process):
if i == running_process[1]:
temp.append(1)
else:
temp.append(0)
v.append(temp)
time += 1
completed += 1
if completed == process:
break
time -= 1
else:
temp = []
for i in range(process):
# print(i)
temp.append(0)
v.append(temp)
time += 1
# print(v)
for i in range(process):
last_ind = 0
for j in range(len(v)):
print(v[j][i], end='')
if v[j][i] == 1:
last_ind = j
finish.append(last_ind + 1)
print()
matrix_vec = np.array(v)
nonzero = np.argwhere(matrix_vec == 1)
for i, j in nonzero:
plt.fill([i, i + 1, i + 1, i], [len(matrix_vec) - j - 1, len(matrix_vec) - j - 1, len(matrix_vec) - j, len(matrix_vec) - j], 'blue')
name = 'A'
avg_wt = 0
avg_tat = 0
max_yticks = 0
for i in range(process):
print(chr(ord(name) + i), " ", arrival[i], " ", burst[i], " ", finish[i], " ", finish[i] - arrival[i], " ", finish[i] - arrival[i] - burst[i])
max_yticks = max(max_yticks, finish[i])
avg_wt += finish[i] - arrival[i] - burst[i]
avg_tat += finish[i] - arrival[i]
print("Avg of TAT ", avg_tat / process)
print("Avg of WT ", avg_wt / process)
plt.xticks(range(max_yticks + 1))
plt.grid(True)
plt.title('FCFS')
plt.show()
if __name__ == "__main__":
n = 1
# n = int(input())
while n > 0:
solve()
n -= 1