-
Notifications
You must be signed in to change notification settings - Fork 8
/
disk_scheduling.py
186 lines (171 loc) · 4.01 KB
/
disk_scheduling.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
from heapq import *
# hp is initial head position
# and requests is the list of requests
# no of cylinders is 200
def FCFS(hp,requests):
time = 0
n = len(requests)
pos = hp
for request in requests:
time += abs(request-pos)
pos = request
print(" ",pos," seeked")
# calculate average seek time
avg_seek_time = time / n
return avg_seek_time
# Shortest Seek Time First
def SSTF(hp,reqs):
requests = reqs.copy()
time = 0
position = hp
n = len(requests)
heap=[]
while len(requests)>0:
for r in requests:
heappush(heap,(abs(position-r),r))
x=heappop(heap)[1]
time+=abs(position-x)
position=x
print(" ",x," seeked")
requests.remove(x)
heap=[]
# calculate average seek time
avg_seek_time = time/n
return avg_seek_time
def SCAN(hp,reqs):
requests = reqs.copy()
pos = hp
time = 0
end=200
start=0
#seek from curr_pos to end which is 200
for i in range(pos,end+1):
if i in requests:
time+=abs(pos-i)
pos=i
print(" ",i," seeked")
requests.remove(i)
time+=abs(pos-end)
pos=end
#seek back to start
for i in range(end,start-1,-1):
if i in requests:
time+=abs(pos-i)
# print(time)
pos=i
print(" ",i," seeked")
requests.remove(i)
print(time)
# calculate average seek time
avg_seek_time = time/n
return avg_seek_time
def C_SCAN(hp,reqs):
requests = reqs.copy()
pos = hp
time = 0
end=200
start=0
#seek from curr_pos to end which is 200
for i in range(pos,end+1):
if i in requests:
time+=abs(pos-i)
pos=i
print(" ",i," seeked")
requests.remove(i)
time+=abs(pos-end)
pos=end
#seek to hp from start
for i in range(start,hp+1):
if i in requests:
time+=abs(pos-i)
pos=i
print(" ",i," seeked")
requests.remove(i)
# calculate average seek time
avg_seek_time = time/n
return avg_seek_time
def LOOK(hp,reqs):
requests = reqs.copy()
pos = hp
time = 0
end=max(requests)
start=min(requests)
#seek from curr_pos to end which is 200
for i in range(pos,end+1):
if i in requests:
time+=abs(pos-i)
pos=i
print(" ",i," seeked")
requests.remove(i)
#seek back to start
for i in range(end,start-1,-1):
if i in requests:
time+=abs(pos-i)
pos=i
print(" ",i," seeked")
requests.remove(i)
print(time)
# calculate average seek time
avg_seek_time = time/n
return avg_seek_time
def C_LOOK(hp,reqs):
requests = reqs.copy()
pos = hp
time = 0
end=max(requests)
start=min(requests)
#seek from curr_pos to max of list
for i in range(pos,end+1):
if i in requests:
time+=abs(pos-i)
pos=i
print(" ",i," seeked")
requests.remove(i)
time+=abs(pos-start)
pos=start
#seek to hp from start
for i in range(start,hp+1):
if i in requests:
time+=abs(pos-i)
pos=i
print(" ",i," seeked")
requests.remove(i)
# calculate average seek time
avg_seek_time = time/n
return avg_seek_time
if __name__=='__main__':
print("DISK SCHEDULING:")
print("Provide number of I/O requests")
#n is the number of I/O requests
n = int(input())
print("Provide initial position of disc arm (total cylinders=200)")
hp = int(input())
while hp>200:
print("!!! INVALID !!! try again")
hp = int(input())
print("Provide positions to visit : max is 200")
requests = []
for i in range(n):
req = int(input())
requests.append(req)
print(requests)
#calling the functions
print(" ********** FCFS *********")
print("Avg seek time for fcfs was ",
FCFS(hp,requests))
print(" ********** SSTF *********")
print("Avg seek time for sstf was ",
SSTF(hp,requests))
print(" ********** SCAN *********")
print("Avg seek time for scan was ",
SCAN(hp,requests))
print(" ********** C-SCAN *********")
print("Avg seek time for C-scan was ",
C_SCAN(hp,requests))
print(" ********** LOOK *********")
print("Avg seek time for look was ",
LOOK(hp,requests))
print(" ********** C-LOOK *********")
print("Avg seek time for C-look was ",
C_LOOK(hp,requests))
print(" ********** Thanks *********")