-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskscheduler.py
More file actions
339 lines (311 loc) · 9.08 KB
/
Copy pathdiskscheduler.py
File metadata and controls
339 lines (311 loc) · 9.08 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from tqdm.auto import tqdm
from time import sleep
from os import system, name
import sys
#these functions are created for design purpose
def clear():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
def load():
print("\n"*2)
print(" "*4,"LOADING.....")
print()
for i in tqdm(range(80)):
print("", end="\r")
sleep(0.025)
def drawline():
print("_"*168)
#home is the main screen of application
def home():
clear()
drawline()
print()
drawline()
print("\n"*5)
print(" "*74,"WELCOME")
print()
print(" "*77,"TO")
print()
print(" "*70,"DISK SCHEDULER")
print()
print(" "*50,"(AN OPERATING SYSTEM PROJECT)" )
print("\n"*5)
drawline()
print()
drawline()
print()
print(" type 'start() to start the application in prompt below")
print()
x=input(" >>>>")
if(x=="start()"):
print()
load()
menu()
else:
print(" INVALID COMMAND")
home()
L,U=int(),int()
#menu takes care of initialization of sequence and initial head and choosing algo
def menu():
sequence=list()
clear()
drawline()
print()
drawline()
print()
print(" the following process will initialize values")
print(" 1.sequence of requests.")
print(" 2.initial head position.")
print(" type 'initialize()' in prompt to continue ")
print()
c=input(" >>>>")
if c=='initialize()':
load()
clear()
drawline()
print()
drawline()
print()
print(" type 'exit()' to exit in sequence prompt")
print(" Enter request sequence:")
while True:
print()
value=input(" >>>>")
if value=="exit()":
break
try:
sequence.append(int(value))
except:
pass
while len(sequence)==0:
print(" there are no track request.type '/a' to do it again")
a=input(" >>>>")
if a == "/a":
menu()
else:
print(" INVALID COMMAND")
print(" Sequence entered.......")
ipos=int(input(" Enter initial head position:"))
print(" head position entered....")
global L
L=int(input(" Enter Lower Track position:"))
print(" lower track position entered....")
global U
U=int(input(" Enter Upper Track position:"))
print(" upper track position entered....")
drawline()
print()
drawline()
print()
amenu(sequence,ipos)
else:
print(" INVALID COMMAND")
menu()
#amenu takes care of choosing disk scheduling algorithm
def amenu(sequence,ipos):
load()
clear()
drawline()
print()
drawline()
print()
print(" Type the following command for following algorithm :")
print()
print(" 1.First come first serve -> 'cd fcfs'")
print(" 2.Shortest seek time first -> 'cd sstf'")
print(" 3.Elevator algorithm(SCAN) -> 'cd scan'")
print(" 4.Look algorithm -> 'cd look'")
print(" 5.C-Scan algorithm -> 'cd cscan'")
print(" 6.C-Look algorithm -> 'cd clook'")
print()
print(" type your command:")
command=input(" >>>>")
print()
if command=="cd fcfs":
fcfs(sequence,ipos)
elif command=="cd sstf":
sstf(sequence,ipos)
elif command=="cd scan":
scan(sequence,ipos)
elif command=="cd look":
look(sequence,ipos)
elif command=="cd cscan":
cscan(sequence,ipos)
elif command=="cd clook":
clook(sequence,ipos)
else:
print(" INVALID COMMAND")
amenu(sequence,ipos)
drawline()
print()
drawline()
navigate(sequence,ipos)
def navigate(sequence,ipos):
print(" IF WANTED TO CHECK OTHER METHODS type 'cd algomethods' \n type 'close()' to exit application")
q=input(" >>>>")
if( q=="cd algomethods"):
amenu(sequence,ipos)
elif (q=="close()"):
clear()
sys.exit(0)
else:
print(" Wrong command")
navigate(sequence,ipos)
#these function are disk scheduling algorithms
def fcfs(sequence,head):
seek_count = 0;
distance, cur_track = 0, 0;
for i in sequence:
cur_track = i;
# calculate absolute distance
distance = abs(cur_track - head);
# increase the total count
seek_count += distance;
# accessed track is now new head
head = cur_track;
print(" Seek Sequence is");
print(" >>>>>",end=" ")
for i in sequence:
print(i,end="--->");
print()
print(" Total number of seek operations = ",seek_count);
print()
def sstf(sequence,head):
sequence1=sequence.copy()
pos=head
mov=0
print(" Seek Sequence is");
print(" >>>>>",end=" ")
while sequence1:
closest=abs(pos-sequence1[0])
index=0
for j in range(1,len(sequence1)):
if(abs(pos-sequence1[j])<closest):
closest=abs(pos-sequence1[j])
index=j
mov+=abs(pos-sequence1[index])
pos=sequence1[index]
print(pos,end="--->")
sequence1.remove(pos)
print(" Total number of seek operations = ",mov);
print()
def scan(sequence,head):
print(" Enter Direction:")
direction=input(" >>>>")
sequence1=sequence.copy()
pos=head
mov=0
print(" Seek Sequence is");
print(" >>>>>",end=" ")
while sequence1:
if pos in sequence1:
print(pos,end="--->")
sequence1.remove(pos)
if not sequence1:
break
if direction=="left" and pos> L:
pos-=1
if direction=="right"and pos<= U:
pos+=1
mov+=1
if pos == 0:
direction="right"
if pos==U:
direction="left"
print(" Total number of seek operations = ",mov);
print()
#
def cscan(sequence,head):
print(" Enter Direction:")
direction=input(" >>>>")
sequence1=sequence.copy()
pos=head
mov=0
print(" Seek Sequence is");
print(" >>>>>",end=" ")
while sequence1:
if direction=="right":
if pos in sequence1:
print(pos,end="--->")
sequence1.remove(pos)
if not sequence1:
break
mov+=1
pos+=1
if pos == U:
pos=0
mov+=U
if direction=="left":
if pos in sequence1:
print(pos,end="--->")
sequence1.remove(pos)
if not sequence1:
break
pos-=1
mov+=1
if pos == 0:
pos=U
mov+=U
print(" Total number of seek operations = ",mov);
print()
def look(sequence,head):
print(" Enter Direction:")
direction=input(" >>>>")
sequence1=sequence.copy()
pos=head
mov=0
print(" Seek Sequence is");
print(" >>>>>",end=" ")
while sequence1:
if pos in sequence1:
print(pos,end="--->")
sequence1.remove(pos)
if not sequence1:
break
if direction=="left" and pos> L:
pos-=1
if direction=="right"and pos< U:
pos+=1
mov+=1
if pos == min(sequence):
direction="right"
if pos==max(sequence):
direction="left"
print(" Total number of seek operations = ",mov);
print()
def clook(sequence,head):
print(" Enter Direction:")
direction=input(" >>>>")
sequence1=sequence.copy()
pos=head
mov=0
print(" Seek Sequence is");
print(" >>>>>",end=" ")
while sequence1:
if direction=="right":
if pos in sequence1:
print(pos,end="--->")
sequence1.remove(pos)
if not sequence1:
break
mov+=1
pos+=1
if pos == max(sequence)+1:
pos=min(sequence)
mov+=(abs(max(sequence)-min(sequence))-1)
if direction=="left":
if pos in sequence1:
print(pos,end="--->")
sequence1.remove(pos)
if not sequence1:
break
pos-=1
mov+=1
if pos == min(sequence)-1:
pos=max(sequence)
mov+=(abs(max(sequence)-min(sequence))-1)
print(" Total number of seek operations = ",mov);
print()
home()