Sub topic 8
Choosing a scheduling algorithm
This chapter assumes you can trace FCFS and SSTF, and that you know SCAN, C-SCAN, LOOK and C-LOOK from the previous chapter. It also assumes you know how seek time and rotational latency make up disk access time. Here you compare the algorithms on throughput, fairness and starvation, and you learn which one fits which workload.
The criteria
Every disk scheduler trades off the same three things.
- Throughput is requests served per second. It improves when the head moves less per request, so total head movement is a proxy for it.
- Fairness is how evenly waiting time is spread over requests, including requests at the edges of the disk.
- Starvation means a request waits indefinitely because new, more attractive requests keep arriving.
Rotational latency matters too. Most textbook algorithms look only at cylinders, so they minimize seek time and ignore the sector position inside the cylinder.
One queue, six algorithms
Use the classic queue 98, 183, 37, 122, 14, 124, 65, 67. The head starts at cylinder 53, and the disk has cylinders 0 to 199. These results use two conventions you should keep consistent in GATE answers.
- SCAN and C-SCAN travel to the physical edge (0 or 199). LOOK and C-LOOK stop at the last request in the direction of travel.
- For the circular variants, the return jump is counted in the total, as in the code below. Some questions exclude it, so read the question statement.
| Algorithm | Direction first | Total head movement |
|---|---|---|
| FCFS | n/a | 640 |
| SSTF | n/a | 236 |
| SCAN | toward 0 | 236 |
| SCAN | toward 199 | 331 |
| LOOK | toward 0 | 208 |
| LOOK | toward 199 | 299 |
| C-SCAN | toward 199 | 382 |
| C-LOOK | toward 199 | 322 |
The C-SCAN figure is 146 (53 to 199), plus 199 (return to 0), plus 37 (0 to 37). The C-LOOK figure is 130 (53 to 183), plus 169 (jump to 14), plus 23 (14 to 37).
This code computes every row:
def fcfs(head, reqs):
total = 0
for r in reqs:
total += abs(r - head)
head = r
return total
def sstf(head, reqs):
pending, total = list(reqs), 0
while pending:
nxt = min(pending, key=lambda r: abs(r - head))
total += abs(nxt - head)
head = nxt
pending.remove(nxt)
return total
def split(head, reqs):
lower = [r for r in reqs if r < head]
upper = [r for r in reqs if r > head]
return lower, upper
def scan_down(head, reqs):
lower, upper = split(head, reqs)
return head + (max(upper) if upper else 0)
def scan_up(head, reqs, max_cyl=199):
lower, upper = split(head, reqs)
return (max_cyl - head) + (max_cyl - min(lower) if lower else 0)
def look_down(head, reqs):
lower, upper = split(head, reqs)
total, pos = 0, head
if lower:
total += head - min(lower)
pos = min(lower)
if upper:
total += max(upper) - pos
return total
def look_up(head, reqs):
lower, upper = split(head, reqs)
total, pos = 0, head
if upper:
total += max(upper) - head
pos = max(upper)
if lower:
total += pos - min(lower)
return total
def cscan_up(head, reqs, max_cyl=199):
lower, upper = split(head, reqs)
total = max_cyl - head
if lower:
total += max_cyl + max(lower) # return jump to 0, then sweep up
return total
def clook_up(head, reqs):
lower, upper = split(head, reqs)
total, pos = 0, head
if upper:
total += max(upper) - head
pos = max(upper)
if lower:
total += (pos - min(lower)) + (max(lower) - min(lower))
return total
queue = [98, 183, 37, 122, 14, 124, 65, 67]
for f in (fcfs, sstf, scan_down, scan_up, look_down, look_up,
cscan_up, clook_up):
print(f.__name__, f(53, queue))It prints 640, 236, 236, 331, 208, 299, 382 and 322.
Starvation and fairness in detail
FCFS is perfectly fair, because it serves requests in arrival order. It has the worst throughput on random workloads, since the head zigzags across the disk.
SSTF is greedy. It often gives low total movement, but it is not optimal overall. It can starve requests far from the head. Suppose the head sits near cylinder 100 and new requests keep arriving around cylinders 98 to 102. A request at cylinder 190 never becomes the nearest one. SSTF also tends to keep the head in the middle of a busy region.
SCAN bounds the wait. Every request is served within about two sweeps, so it cannot starve anything. It is biased, though. Right after the head reverses at one edge, the cylinders just behind it were served moments ago, so they wait almost a full round trip. Cylinders in the middle are visited twice per cycle, so they see shorter average waits than the edges.
C-SCAN removes that bias. It serves requests in one direction only, then returns without serving. Every cylinder is visited once per cycle, so waiting times are more uniform. The price is the return trip, which does no useful work.
LOOK and C-LOOK are the practical versions of SCAN and C-SCAN. They reverse or jump at the last request instead of the disk edge, so they never travel to cylinders nobody asked for. They keep the same starvation guarantee.
A subtle point: SCAN-type algorithms can still delay requests if a process keeps hammering the cylinder under the head. The head stays in place and serves them indefinitely. Real implementations limit this, for example by freezing the queue during a sweep (N-step SCAN).
Matching the algorithm to the load
- With a short queue there is almost nothing to reorder, so every algorithm behaves like FCFS. The simplest one wins.
- Under heavy load, SCAN-family algorithms perform best, because many requests give them many chances to batch work in one sweep.
- Workloads dominated by large contiguous files, such as streaming, produce few seeks. Allocation policy then matters more than the scheduler.
Real kernels often mix ideas. Linux's deadline-style schedulers sort requests by sector like LOOK, but attach an expiry time to each request so none waits too long. The scheduler can also merge adjacent requests into one larger one.
When scheduling matters less
On an SSD there is no head and no rotation, so cylinder distance means nothing. Simple schedulers, sometimes just a FIFO queue, are common there. The goal shifts to merging requests and keeping the device's internal parallelism busy.
Exercise
Using the queue 98, 183, 37, 122, 14, 124, 65, 67 with the head at cylinder 53 moving toward cylinder 0, what is the total head movement under LOOK? Why is it smaller than SCAN's 236?
Show answer
LOOK goes 53 to 14 (39 cylinders), reverses at the last lower request, then goes 14 to 183 (169 cylinders). The total is 39 + 169 = 208. SCAN is 236 because it also travels from 14 to cylinder 0 (14 extra cylinders) and then from 0 to 14 again (14 more). That is 28 cylinders of movement that serve no request.
Exercise
A server's disk queue is always long, and users complain that some requests far from the busy region take very long. Name the algorithm that most likely causes this, explain why, and suggest a replacement.
Show answer
SSTF is the likely cause. It always picks the nearest request, so a steady stream of requests near the head can starve distant ones. Replace it with C-LOOK (or LOOK). C-LOOK visits every cylinder once per cycle, so the wait is bounded and roughly uniform, and it does not travel to the disk edges needlessly.