-
Notifications
You must be signed in to change notification settings - Fork 0
/
SJF.py
114 lines (89 loc) · 1.96 KB
/
SJF.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
import sys
def disp(x, s):
sys.stdout.write("\033[;1m") #BOLD headings
print s
sys.stdout.write("\033[0;0m") #RESET to normal(default)
for i in range(count):
print 'P' + str(i+1) + '\t' + str(x[i])
def fileRead(arriv, burst, count):
file = open("sjf.txt", "r")
count = int(file.readline())
for i in range(count):
arriv.insert(i, int(file.readline()))
for i in range(count):
burst.insert(i, int(file.readline()))
return count
def minIndex(l):
if l >= arriv[count-1]:
return count-1
m = count - 1
while True:
if (l < arriv[m]):
m -= 1
else:
break
return m
def minValue(cop, n):
new = []
for i in range(n+1):
new.insert(i, cop[i])
return min(new)
def cal():
n = 0
last = 1
copy = []
line = arriv[0]
start.insert(0, arriv[0])
for i in range(count):
copy.insert(i, burst[i])
line += copy[0]
end.insert(0, line)
copy[0] = 999999;
while True:
n = minIndex(line)
n = minValue(copy, n)
index = copy.index(n)
start.insert(index, line)
line += burst[index]
end.insert(index, line)
copy[index] = 999999;
last += 1
if last == count:
break
def waiting(sum):
sum = 0
for i in range(count):
# wait.insert(i, (turn[i] - burst[i]))
wait.insert(i, (start[i] - arriv[i]))
sum += wait[i]
disp(wait, '\nWaiting Time')
return sum
def turnaround(sum):
sum = 0
for i in range(count):
turn.insert(i, (end[i] - arriv[i]))
sum += turn[i]
disp(turn, '\nTurnaround Time')
return sum
sum = 0
line = 0
count = 0
end = []
wait = []
turn = []
start = []
arriv = []
burst = []
count = fileRead(arriv, burst, count)
disp(arriv, '\nArrival Time')
disp(burst, '\nBurst Time')
cal()
disp(end, '\nCompletion Time')
sum = turnaround(sum)
avg = float(sum)/count
sys.stdout.write("\033[1;33m") #setting text color to YELLOW
print '\nAverage Turnaround Time : ' + str(avg)
sum = waiting(sum)
avg = float(sum)/count
sys.stdout.write("\033[1;33m") #setting text color to YELLOW
print '\nAverage Waiting Time : ' + str(avg)