-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskscheduler.py
274 lines (258 loc) · 9.18 KB
/
diskscheduler.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#Colton Hines
#Problem Set 4
#COSC 335
#Simulating Disk Scheduling Algorithms
import random
import math
bestAlgorithm = math.inf #Integer to determine algorithm with the least amount of read head movement
bestTotal = math.inf # Best current total number of cylinders traversed per algorithm
# Switch-statement to output the best algorithm.
def best(ba):
switcher = {
1: "FCFS is best.",
2: "SSTF is best.",
3: "Scan is best.",
4: "Look is best.",
5: "C-Scan is best.",
6: "C-Look is best.",
}
return switcher.get(ba, "N/A")
# Function to generate a list of 1000 random integers between 0 and 4999 inclusive.
def generateRequests():
randomRequests = random.sample(range(0,5000), 1000)
return randomRequests
# First-come first-serve algorithm. Adds the distance between the current cylinder and its neighbor in order of appearance in the work queue.
def FCFS(n,requests):
total = 0
seekTime = 0
for x in requests:
total = total + int(abs(n - x))
n = x
seekTime = int(0.76 + (0.24 * math.sqrt(total)))
global bestAlgorithm
global bestTotal
if(total < bestTotal):
bestAlgorithm = 1
bestTotal = total
print("FCFS")
print("=================================")
print("Total cylinders traversed: ", total)
print("Total seek time: ", seekTime, "milliseconds")
print("=================================")
print()
# Shortest seek time first algorithm. Adds the distance between the current cylinder and its closest neighbor in the work queue to the total distance.
def SSTF(n,requests):
total = 0
seekTime = 0
while(len(requests) > 0):
nearest = math.inf
for x in requests:
if(int(abs(n-x)) < (abs(n-nearest))):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekTime = int(0.76 + (0.24 * math.sqrt(total)))
global bestAlgorithm
global bestTotal
if(total < bestTotal):
bestAlgorithm = 2
bestTotal = total
print("SSTF")
print("=================================")
print("Total cylinders traversed: ", total)
print("Total seek time: ", seekTime, "milliseconds")
print("=================================")
print()
# Scan algorithm. Similar to the SSTF algorithm except the direction of the read head is descending (extra condition is added to the if statement to make sure the next cylinder considered lower on the disk than the current cylinder. When the head reaches the lowest cylinder in the work queue, the current cylinder and the next closest cylinder are added to total since the read head travels all the way to 0 before changing direction. Read head position is tracked by a variable (seekDownCount) containing the number of cylinders lower than the original read head position.
def Scan(n,requests): #Read head direction descending
total = 0
seekTime = 0
seekDownCount = 0
for x in requests:
if(x<n):
seekDownCount = seekDownCount + 1
while(len(requests) > 0):
nearest = math.inf
if(seekDownCount > 0):
for x in requests:
if(((int(abs(n-x))) < (abs(n-nearest))) and (x < n)):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
if(seekDownCount == 0):
nearest = math.inf
for x in requests:
if((int(abs(n-x))) < (abs(n-nearest))):
nearest = x
total = total + n + nearest
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
else:
nearest = math.inf
for x in requests:
if((int(abs(n-x))) < (abs(n-nearest))):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
seekTime = int(0.76 + (0.24 * math.sqrt(total)))
global bestAlgorithm
global bestTotal
if(total < bestTotal):
bestAlgorithm = 3
bestTotal = total
print("Scan")
print("=================================")
print("Total cylinders traversed: ", total)
print("Total seek time: ", seekTime, "milliseconds")
print("=================================")
print()
# Look algorithm. Similar to Scan, except when the read head switches direction only the distance to the nearest cylinder is added to the total. Read head direction is ascending to match the in-class assignment for testing purposes (although the direction shouldn't matter for total distance traveled).
def Look(n,requests): # Read head direction ascending.
total = 0
seekTime = 0
seekDownCount = 0
for x in requests:
if(x>n):
seekDownCount = seekDownCount + 1
while(len(requests) > 0):
nearest = math.inf
if(seekDownCount > 0):
for x in requests:
if(((int(abs(n-x))) < (abs(n-nearest))) and (x > n)):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
if(seekDownCount <= 0):
nearest = math.inf
for x in requests:
if(((int(abs(n-x))) < (abs(n-nearest)))):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
seekTime = int(0.76 + (0.24 * math.sqrt(total)))
global bestAlgorithm
global bestTotal
if(total < bestAlgorithm):
bestAlgorithm = 4
bestTotal = total
print("Look")
print("=================================")
print("Total cylinders traversed: ", total)
print("Total seek time: ", seekTime, "milliseconds")
print("=================================")
print()
# C-Scan algorithm. Same as Scan except the read head jumps to the end of the disk. The distance between the top and bottom cylinders and the maximum and minimum cylinders are added to total because the read head seeks to the edge of the disk before jumping. The jump is not included in read head traversal.
def CScan(n,requests): # Read head direction descending.
total = 0
seekTime = 0
seekDownCount = 0
for x in requests:
if(x<n):
seekDownCount = seekDownCount + 1
while(len(requests) > 0):
nearest = math.inf
if(seekDownCount > 0):
for x in requests:
if(((int(abs(n-x))) < (abs(n-nearest))) and (x < n)):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
if(seekDownCount == 0):
farthest = 0
for x in requests:
if((int(abs(n-x))) > farthest):
farthest = x
total = total + n + int(abs(4999 - farthest))
n = farthest
requests.remove(farthest)
seekDownCount = seekDownCount - 1
if(seekDownCount < 0):
nearest = math.inf
for x in requests:
if((int(abs(n-x))) < (abs(n-nearest))):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
seekTime = int(0.76 + (0.24 * math.sqrt(total)))
global bestAlgorithm
global bestTotal
if(total < bestTotal):
bestAlgorithm = 5;
bestTotal = total
print("C-Scan")
print("=================================")
print("Total cylinders traversed: ", total)
print("Total seek time: ", seekTime, "milliseconds")
print("=================================")
print()
# C-Look algorithm. Similar to the look algorithm, except that when it reaches the lowest value in the work queue it immediately jumps to the highest value in the work queue. Once again the jump is not counted as read head movement making this algorithm extremely efficient.
def CLook(n,requests):# Read head direction descending
total = 0
seekTime = 0
seekDownCount = 0
for x in requests:
if(x<n):
seekDownCount = seekDownCount + 1
while(len(requests) > 0):
nearest = math.inf
if(seekDownCount > 0):
for x in requests:
if(((int(abs(n-x))) < (abs(n-nearest))) and (x < n)):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
if(seekDownCount == 0):
farthest = 0
for x in requests:
if((int(abs(n-x))) > farthest):
farthest = x
n = farthest
requests.remove(farthest)
seekDownCount = seekDownCount - 1
if(seekDownCount < 0):
nearest = math.inf
for x in requests:
if((int(abs(n-x))) < (abs(n-nearest))):
nearest = x
total = total + int(abs(n-nearest))
n = nearest
requests.remove(nearest)
seekDownCount = seekDownCount - 1
seekTime = int(0.76 + (0.24 * math.sqrt(total)))
global bestAlgorithm
global bestTotal
if(total < bestTotal):
bestAlgorithm = 6
bestTotal = total
print("C-Look")
print("=================================")
print("Total cylinders traversed: ", total)
print("Total seek time: ", seekTime, "milliseconds")
print("=================================")
print(best(bestAlgorithm))
r = generateRequests()
head = -1
while((head < 0) or (head > 4999)):
head = int(input("Please enter a head cylinder between 0 and 4999 for the disk scheduling algorithm computatons: "))
FCFS(head, r[:])
SSTF(head, r[:])
Scan(head, r[:])
Look(head, r[:])
CScan(head, r[:])
CLook(head, r[:])