-
Notifications
You must be signed in to change notification settings - Fork 3
/
diskScheduling.py
150 lines (130 loc) · 3.36 KB
/
diskScheduling.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
from heapq import *
from handleFile import *
def SSTF(hp, reqs):
requests = reqs.copy()
time = 0
position = hp
heap = []
distance = []
distance.append(hp)
while len(requests) > 0:
for r in requests:
heappush(heap, (abs(position-r), r))
x = heappop(heap)[1]
time += abs(position-x)
position = x
distance.append(x)
requests.remove(x)
heap = []
# calculate average seek time
save('total', time, 'distance', distance)
def SCAN(hp, reqs):
requests = reqs.copy()
pos = hp
time = 0
end = 200
start = 0
distance = []
distance.append(hp)
for i in range(pos, end+1):
if i in requests:
time += abs(pos-i)
pos = i
distance.append(i)
requests.remove(i)
time += abs(pos-end)
pos = end
for i in range(end, start-1, -1):
if i in requests:
time += abs(pos-i)
pos = i
distance.append(i)
requests.remove(i)
save('total', time, 'distance', distance)
def FCFS(hp, requests):
time = 0
pos = hp
distance = []
distance.append(hp)
for request in requests:
time += abs(request-pos)
pos = request
distance.append(pos)
# calculate average seek time
save('total', time, 'distance', distance)
def C_SCAN(hp, reqs):
requests = reqs.copy()
pos = hp
time = 0
end = 200
start = 0
distance = []
distance.append(hp)
# 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
distance.append(i)
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
distance.append(i)
requests.remove(i)
# calculate average seek time
save('total', time, 'distance', distance)
def LOOK(hp, reqs):
requests = reqs.copy()
pos = hp
time = 0
end = max(requests)
start = min(requests)
distance = []
distance.append(hp)
# 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
distance.append(i)
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
distance.append(i)
requests.remove(i)
# calculate average seek time
save('total', time, 'distance', distance)
def C_LOOK(hp, reqs):
requests = reqs.copy()
pos = hp
time = 0
end = max(requests)
start = min(requests)
distance = []
distance.append(hp)
# 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
distance.append(i)
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
distance.append(i)
requests.remove(i)
# calculate average seek time
save('total', time, 'distance', distance)