-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS375_Lab5_Part2.py
102 lines (83 loc) · 2.66 KB
/
CS375_Lab5_Part2.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
import random
# Input: None
# Output: 100 random jobs
def generateJobs():
jobs = []
for i in range(100):
if i == 0:
arrivalTime, length = 0, random.uniform(0.5, 1)
else:
arrivalTime, length = random.random(), random.uniform(0.5, 1)
jobs.append((arrivalTime, length))
return jobs
#Input: jobs
# Output: Schedule jobs based on shortest job first
def shortestFirst(jobs):
s = jobs.copy()
# sort the jobs based on shortest job length
s.sort(key=lambda x: x[1])
schedule = []
schedule.append(s[0])
wt = list(range(100))
st = list(range(100))
wt[0] = schedule[0][0]
st[0] = schedule[0][1]
time = wt[0]
for i in range(1, len(s)):
current = s[i]
time += schedule[0][1]
wt[i] = time - current[0]
st[i] = wt[i] + current[1]
schedule.pop()
schedule.append(current)
print("Shortest first avg wait time: " + str(sum(wt)/100))
print("Shortest first max wait time: " + str(max(wt)))
print("Shortest first avg service time: " + str(sum(st)/100))
print("Shortest first max service time: " + str(max(st)))
def longestFirst(jobs):
s = jobs.copy()
s.sort(key=lambda x: x[1], reverse=True)
schedule = []
schedule.append(s[0])
wt = list(range(100))
st = list(range(100))
wt[0] = schedule[0][0]
st[0] = schedule[0][1]
time = wt[0]
for i in range(1, len(s)):
current = s[i]
time += schedule[0][1]
wt[i] = time - current[0]
st[i] = wt[i] + current[1]
schedule.pop()
schedule.append(current)
print("Longest first avg wait time: " + str(sum(wt)/100))
print("Longest first max wait time: " + str(max(wt)))
print("Longest first avg service time: " + str(sum(st)/100))
print("Longest first max service time: " + str(max(st)))
def FirstCome(jobs):
s = jobs.copy()
s.sort(key=lambda x: x[0])
# print(s)
schedule = []
schedule.append(s[0])
wt = list(range(100))
st = list(range(100))
wt[0] = schedule[0][0]
st[0] = schedule[0][1]
time = schedule[0][0]
for i in range(1, len(s)):
current = s[i]
time += schedule[0][1]
wt[i] = time - current[0]
st[i] = wt[i] + current[1]
schedule.pop()
schedule.append(current)
print("First come first serve avg waiting time: " + str(sum(wt)/100))
print("First come first serve max waiting time: " + str(max(wt)))
print("First come first serve avg service time: " + str(sum(st)/100))
print("First come first serve max service time: " + str(max(st)))
jobs = generateJobs()
shortestFirst(jobs)
longestFirst(jobs)
FirstCome(jobs)