-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_3.py
53 lines (49 loc) · 1.68 KB
/
5_3.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
#SCAN
import matplotlib.pyplot as plt
class Scheduler:
def __init__(self,name):
self.name = name
self.head = None
self.requests = []
def __repr__(self):
return(str(self.name))
def seek(requests,head):
time = 0
served = []
start = requests.index(head)
for i in range(start,len(requests)-1):
st = abs((requests[i+1]-requests[i]))
print(f"From {requests[i]} to {requests[i+1]}, seektime:{st}")
served += [requests[i]]
time += st
remaining = [i for i in requests if i not in served]
remaining.sort(reverse=True)
for i in range(len(remaining)-1):
st = abs((remaining[i+1]-remaining[i]))
print(f"From {remaining[i]} to {remaining[i+1]}, seektime:{st}")
served += [remaining[i]]
time += st
served.append(remaining[i+1])
return ((f" Seektime: {time}\n Average Time: {time/len(requests)}"),served)
def plot(requestaxis,timeaxis,time):
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
fig, ax = plt.subplots()
ax.xaxis.set_label_position('top')
ax.tick_params(labelbottom=False,labeltop=True)
ax.plot(requestaxis,timeaxis)
ax.set_title('SCAN Disk Scheduling')
ax.invert_yaxis()
plt.xlabel("Disk block")
plt.ylabel("Time")
plt.show()
scheduler = Scheduler("SCAN")
print("Enter the order of requests separated by comma:")
scheduler.requests += map(int,input().split(','))
scheduler.head = int(input("Current position of head: "))
timeaxis = [i for i in range(len(scheduler.requests)+1)]
requestaxis = [scheduler.head] + scheduler.requests
requestaxis.sort()
time,served = seek(requestaxis,scheduler.head)
print(time)
plot(served,timeaxis,time)