-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskScheduler.go
575 lines (460 loc) · 17 KB
/
DiskScheduler.go
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Created by Isaias Perez Vega
// Disk - Scheduling Algorithms
// =================================================
// Program reads an input file with cylinder requests
// File specifies scheduling algorithm, program outputs
// The optimal schedule based on algorithm.
package main
import (
"bufio"
"fmt"
"strings"
"os"
"math"
"strconv"
)
// ------------ //
// Struct //
type cylinder struct {
location int
cost int
}
// --------------------------- //
// Streamlining error checking //
func checkErr(e error) {
if e != nil {
fmt.Println("Error ocurred when trying to open file.\n")
panic(e)
}
}
// ---------------------------- //
// Search index of word in list //
func find(item string, list []string) (index int) {
count := 0
index = -1
for _, word:= range list {
if word == item {
index = count
}
count = count + 1
}
// Before returning check if the word was found
if index == -1 {
//fmt.Println("Did not find ", item, " in memory..")
}
return index
}
// ------------------------------------------------ //
// Looks for a word in array and converts it to int //
func lookConvert(word string, lines []string) (int) {
index := find(word, lines)
num, err := strconv.Atoi(lines[index + 1])
// Type conversion error handling
if err != nil {
//fmt.Println("Conversion of var ", word, " failed!\n")
}
return num
}
// ----------------------------------- //
// Read Input File parameter in arg[1] //
func readInputFile()(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int) {
// Fetch name and read file
fileName := os.Args[1]
input, err := os.Open(fileName)
checkErr(err)
defer input.Close()
// Scanning each line of file
scanner := bufio.NewScanner(input)
scanner.Split(bufio.ScanLines)
// Temporary storage
var lines []string
var words []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
// Extracting info for each line
for i := 0; i < len(lines); i = i + 1 {
// Each line in file gets split into a string array
words = strings.Split(lines[i], " ")
// Get algo type, cylinder bounds, and start location info
// Avoids processing comments '#'
switch words[0] {
case "use":
algorithm = words[1]
case "lowerCYL":
lowerCYL = lookConvert("lowerCYL", words)
case "upperCYL":
upperCYL = lookConvert("upperCYL", words)
case "initCYL":
initCYL = lookConvert("initCYL", words)
case "cylreq":
cylReqs = append(cylReqs, lookConvert("cylreq", words))
}
}
return algorithm, lowerCYL, upperCYL, initCYL, cylReqs
}
// ----------------------------------------------------- //
// Print formatted setup info and list cylinder requests //
func printReadInput(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int)() {
fmt.Printf("Seek algorithm: %s\n", strings.ToUpper(algorithm))
fmt.Printf("\tLower cylinder: %5d\n", lowerCYL)
fmt.Printf("\tUpper cylinder: %5d\n", upperCYL)
fmt.Printf("\tInit cylinder: %5d\n", initCYL)
fmt.Printf("\tCylinder requests:\n")
for i := 0; i < len(cylReqs); i = i + 1 {
fmt.Printf("\t\tCylinder %5d\n", cylReqs[i])
}
}
// --------------------------------------- //
// Check if cylinder is within disk bounds //
func checkBounds(lowerCYL, upperCYL, cylinder int)(bool) {
inBounds := false
if cylinder > lowerCYL && cylinder < upperCYL {
inBounds = true
}
return inBounds
}
//-------------------------------- //
// Perform an Inversed Bubble sort //
func sort(cylReqs []cylinder, sortBy string)([]cylinder) {
for x := 0; x < len(cylReqs); x = x + 1 {
for i := 0; i < len(cylReqs) - 1; i = i + 1 {
// Sort the structs by cost
if sortBy == "cost" {
if cylReqs[i].cost < cylReqs[i + 1].cost {
cylReqs[i], cylReqs[i + 1] = cylReqs[i + 1], cylReqs[i]
}
} else {
// Sort the structs by location
if cylReqs[i].location < cylReqs[i + 1].location {
cylReqs[i], cylReqs[i + 1] = cylReqs[i + 1], cylReqs[i]
}
}
}
}
return cylReqs
}
// ------------------------------------------------------------------------ //
// Execute the First-come First-Served disk scheduling algorithm will print //
// cylinder number as it processes them and calculate total seek distance //
func runFCFS(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int)() {
nextDistance := float64(cylReqs[0] - initCYL)
seekDistance := int(nextDistance)
fmt.Printf("Servicing %5d\n", cylReqs[0])
for i := 1; i < len(cylReqs); i = i + 1 {
fmt.Printf("Servicing %5d\n", cylReqs[i])
if checkBounds(lowerCYL, upperCYL, cylReqs[i]) {
nextDistance = math.Abs(float64(cylReqs[i] - cylReqs[i - 1]))
seekDistance = seekDistance + int(nextDistance)
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
fmt.Printf("%s traversal count = %3d\n", strings.ToUpper(algorithm), seekDistance)
}
// ------------------------------------------------------------------------- //
// Execute the Shortest seek time first disk scheduling algorithm will print //
// cylinder number as it processes them and calculate total seek distance //
func runSSTF(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int)() {
// Create array of struct cylinders with respective locations
// and initial costs
var requests = make([]cylinder, len(cylReqs))
var cyl cylinder
current := initCYL
for i := 0; i < len(cylReqs); i = i + 1 {
cyl.location = cylReqs[i]
cyl.cost = int(math.Abs(float64(cylReqs[i] - initCYL)))
requests[i] = cyl
}
// Traversal Distance
seekDistance := 0
// For every requests, calculate costs and service the
// one with the shortest seek time
for i := 0; i < len(cylReqs); i = i + 1 {
requests = sort(requests, "cost")
fmt.Printf("Servicing %d\n", requests[len(requests)-1].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[len(requests)-1].location < upperCYL && requests[len(requests)-1].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[len(requests)-1].location - current)))
current = requests[len(requests)-1].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
for i := 0; i < len(requests); i = i + 1 {
//fmt.Println("cyl.loc: ", requests[i].location, " cyl.cost: ", requests[i].cost)
requests[i].cost = int(math.Abs(float64(requests[i].location - current)))
}
requests = requests[:len(requests)-1]
}
fmt.Printf("%s traversal count = %3d\n", strings.ToUpper(algorithm), seekDistance)
}
// -------------------------------------------------------------------- //
// Execute the SCAN scheduling algorithm will print cylinder number as //
// it processes them and calculate total seek distance //
func runSCAN(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int)() {
var requests = make([]cylinder, len(cylReqs))
var cyl cylinder
current := initCYL
startIndex := 0
turnHead := false
// Create array of struct cylinders with respective locations
// and initial costs
for i := 0; i < len(cylReqs); i = i + 1 {
cyl.location = cylReqs[i]
cyl.cost = int(math.Abs(float64(cylReqs[i] - initCYL)))
requests[i] = cyl
}
seekDistance := 0
// Start by sorting request by location
requests = sort(requests, "location")
// Find First cylinder location to visit
for i := 0; i < len(requests); i = i + 1 {
if initCYL <= requests[i].location {
startIndex = i
}
// Check if head must turn
if requests[i].location < initCYL {
turnHead = true
}
}
// Service cylinders to the right(up) of the disk
for i := startIndex; i >= 0; i = i - 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
// Update distance, account for head to reach end of disk
// and traverse back to and servicing requests on its way back, if it must go back
if turnHead {
seekDistance = seekDistance + (upperCYL - current)
current = upperCYL
}
// Service cylinders to the left(down) of the disk
for i := startIndex + 1; i < len(requests); i = i + 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
fmt.Printf("%s traversal count = %3d\n", strings.ToUpper(algorithm), seekDistance)
}
// ---------------------------------------------------------------------- //
// Execute the C-SCAN scheduling algorithm will print cylinder number as //
// it processes them and calculate total seek distance //
func runCSCAN(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int)() {
var requests = make([]cylinder, len(cylReqs))
var cyl cylinder
current := initCYL
startIndex := 0
seekDistance := 0
turnHead := false
// Create array of struct cylinders with respective locations
// and initial costs
for i := 0; i < len(cylReqs); i = i + 1 {
cyl.location = cylReqs[i]
cyl.cost = int(math.Abs(float64(cylReqs[i] - initCYL)))
requests[i] = cyl
}
// Start by sorting request by location
requests = sort(requests, "location")
// Find First cylinder location to visit
for i := 0; i < len(requests); i = i + 1 {
if initCYL <= requests[i].location {
startIndex = i
}
// Check if head must turn
if requests[i].location < initCYL {
turnHead = true
}
}
// Service cylinders to the right(up) of the disk
for i := startIndex; i >= 0; i = i - 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
// Update distance, account for head to reach end of disk
// and traverse back to otherside.
if turnHead {
seekDistance = seekDistance + (upperCYL - current) + (upperCYL - lowerCYL)
current = 0
}
// Service cylinders to the left(down) of the disk
for i := len(requests) - 1; i > startIndex; i = i - 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
fmt.Printf("%s traversal count = %3d\n", strings.ToUpper(algorithm), seekDistance)
}
// -------------------------------------------------------------------- //
// Execute the LOOK scheduling algorithm will print cylinder number as //
// it processes them and calculate total seek distance //
func runLOOK(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int)() {
var requests = make([]cylinder, len(cylReqs))
var cyl cylinder
current := initCYL
startIndex := 0
seekDistance := 0
turnHead := false
// Create array of struct cylinders with respective locations
// and initial costs
for i := 0; i < len(cylReqs); i = i + 1 {
cyl.location = cylReqs[i]
cyl.cost = int(math.Abs(float64(cylReqs[i] - initCYL)))
requests[i] = cyl
}
// Start by sorting request by location
requests = sort(requests, "location")
// Find First cylinder location to visit
for i := 0; i < len(requests); i = i + 1 {
if initCYL <= requests[i].location {
startIndex = i
}
// Check if head must turn
if requests[i].location < initCYL {
turnHead = true
}
}
// Service cylinders to the right(up) of the disk
for i := startIndex; i >= 0; i = i - 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
// Update distance, account for head to reach end of disk
// and traverse back to otherside, only if it has to
if turnHead {
lastCYL := current
current = requests[startIndex + 1].location
seekDistance = seekDistance + (lastCYL - current)
// Service cylinders to the left(down) of the disk
for i := startIndex + 1; i < len(requests); i = i + 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
}
fmt.Printf("%s traversal count = %3d\n", strings.ToUpper(algorithm), seekDistance)
}
// -------------------------------------------------------------------- //
// Execute the LOOK scheduling algorithm will print cylinder number as //
// it processes them and calculate total seek distance //
func runCLOOK(algorithm string, lowerCYL, upperCYL, initCYL int, cylReqs []int)() {
var requests = make([]cylinder, len(cylReqs))
var cyl cylinder
current := initCYL
startIndex := 0
seekDistance := 0
turnHead := false
// Create array of struct cylinders with respective locations
// and initial costs
for i := 0; i < len(cylReqs); i = i + 1 {
cyl.location = cylReqs[i]
cyl.cost = int(math.Abs(float64(cylReqs[i] - initCYL)))
requests[i] = cyl
}
// Start by sorting request by location
requests = sort(requests, "location")
// Find First cylinder location to visit
for i := 0; i < len(requests); i = i + 1 {
if initCYL <= requests[i].location {
startIndex = i
}
// Check if head must turn
if requests[i].location < initCYL {
turnHead = true
}
}
// Service cylinders to the right(up) of the disk
for i := startIndex; i >= 0; i = i - 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
// Update distance, account for head to reach last cylinder
// and traverse back to otherside from smallest cylinder, only if it has to
if turnHead {
lastCYL := current
current = requests[len(requests) - 1].location
seekDistance = seekDistance + (lastCYL - current)
// Service cylinders to the left(down) of the disk
for i := len(requests) - 1; i > startIndex; i = i - 1 {
fmt.Printf("Servicing %d\n", requests[i].location)
// Recalculate costs without current cylinder
// and update traversal distance
if requests[i].location < upperCYL && requests[i].location > lowerCYL {
seekDistance = seekDistance + int(math.Abs(float64(requests[i].location - current)))
current = requests[i].location
} else {
fmt.Printf("Cylinder is out of bounds\n")
}
}
}
fmt.Printf("%s traversal count = %3d\n", strings.ToUpper(algorithm), seekDistance)
}
// ---------------- //
// Initiate Program //
func main() {
// Read input file and structure data
algorithm, lowerCYL, upperCYL, initCYL, cylReqs := readInputFile()
// Print to STDOUT structured data
printReadInput(algorithm, lowerCYL, upperCYL, initCYL, cylReqs)
// Execute chosen scheduling algorithm
switch algorithm {
case "fcfs":
runFCFS(algorithm, lowerCYL, upperCYL, initCYL, cylReqs)
case "sstf":
runSSTF(algorithm, lowerCYL, upperCYL, initCYL, cylReqs)
case "scan":
runSCAN(algorithm, lowerCYL, upperCYL, initCYL, cylReqs)
case "c-scan":
runCSCAN(algorithm, lowerCYL, upperCYL, initCYL, cylReqs)
case "look":
runLOOK(algorithm, lowerCYL, upperCYL, initCYL, cylReqs)
case "c-look":
runCLOOK(algorithm, lowerCYL, upperCYL, initCYL, cylReqs)
}
}