Sub topic 7
SCAN, C-SCAN, LOOK and C-LOOK
This chapter assumes you can trace FCFS and SSTF, and that you know seek time dominates disk access time. Both come from earlier chapters. FCFS and SSTF have a weakness. FCFS ignores head position, and SSTF can starve distant requests. The SCAN family fixes this by making the head sweep in one direction, serving every request on the way.
The elevator idea
SCAN is also called the elevator algorithm. The head moves in one direction and serves each pending request on the way. At the end of the disk it reverses and sweeps back. A request can wait at most about two sweeps, so no request starves.
The four algorithms differ in two choices:
- How far the head travels before turning. SCAN and C-SCAN go to the physical edge of the disk. LOOK and C-LOOK go only as far as the last request in that direction.
- Whether the head serves requests on the return trip. SCAN and LOOK serve in both directions. C-SCAN and C-LOOK serve in one direction only. They jump back to the start without serving anything and then sweep the same way again.
| Algorithm | Turns at | Serves on return | Waiting time |
|---|---|---|---|
| SCAN | Disk edge | Yes | Uneven, favors the middle |
| C-SCAN | Disk edge, then jumps to the other edge | No | More uniform |
| LOOK | Last request | Yes | Uneven, favors the middle |
| C-LOOK | Last request, then jumps to the first request | No | More uniform |
Worked example
Use the same queue as before: 98, 183, 37, 122, 14, 124, 65, 67. The head starts at cylinder 53, and the disk has cylinders 0 to 199.
Requests below the head are 14 and 37. Requests above it are 65, 67, 98, 122, 124 and 183.
SCAN, moving toward 0 first. The head goes 53 → 37 → 14 → 0, then reverses and goes up to 183. The distance is 53 + 183 = 236.
SCAN, moving up first. The head goes 53 → 183 → 199, then back down to 14. The distance is 146 + 185 = 331. The initial direction matters, and GATE questions always state it.
LOOK, moving toward 0 first. The head goes 53 → 14, then up to 183. The distance is 39 + 169 = 208. It saves 28 cylinders against SCAN because it never visits cylinder 0.
C-LOOK, moving up. The head goes 53 → 183, which is 130. It then jumps to 14, which is 169, and sweeps up to 37, which is 23. The total is 322 if you count the jump and 153 if you do not.
C-SCAN, moving up. The head goes 53 → 183 → 199, which is 146. It then jumps to 0, which is 199, and sweeps up to 37, which is 37. The total is 382 with the jump and 183 without.
The dashed arrow is the jump. It serves nothing, so it is the part that exam conventions disagree about.
Computing it in code
The functions below compute the examples above. They build the list of stops the head visits and sum the distances.
def cost(stops):
return sum(abs(b - a) for a, b in zip(stops, stops[1:]))
def split(head, reqs):
lower = sorted(r for r in reqs if r < head)
upper = sorted(r for r in reqs if r >= head)
return lower, upper
def scan(head, reqs, max_cyl, up=True):
lower, upper = split(head, reqs)
if up:
return cost([head] + upper + [max_cyl] + lower[::-1])
return cost([head] + lower[::-1] + [0] + upper)
def look(head, reqs, up=True):
lower, upper = split(head, reqs)
if up:
return cost([head] + upper + lower[::-1])
return cost([head] + lower[::-1] + upper)
def c_scan(head, reqs, max_cyl, count_jump=True):
lower, upper = split(head, reqs)
sweep = cost([head] + upper + [max_cyl])
second = cost([0] + lower)
return sweep + (max_cyl if count_jump else 0) + second
def c_look(head, reqs, count_jump=True):
lower, upper = split(head, reqs)
sweep = cost([head] + upper)
second = cost(lower)
jump = 0
if lower and count_jump:
jump = ([head] + upper)[-1] - lower[0]
return sweep + jump + second
queue = [98, 183, 37, 122, 14, 124, 65, 67]
print(scan(53, queue, 199, up=False)) # 236
print(scan(53, queue, 199, up=True)) # 331
print(look(53, queue, up=False)) # 208
print(c_scan(53, queue, 199)) # 382
print(c_scan(53, queue, 199, False)) # 183
print(c_look(53, queue)) # 322
print(c_look(53, queue, False)) # 153Common errors
- Wrong initial direction. SCAN toward 0 first gave 236 here and SCAN upward gave 331. Read the direction before you start.
- Going to the edge in LOOK. LOOK and C-LOOK turn at the last request, not at the cylinder limit.
- Ignoring the maximum cylinder. SCAN and C-SCAN need the highest cylinder number. For a disk with N cylinders it is N − 1, not N.
- Requests exactly at the head. A request at the head's current cylinder costs 0 and is served immediately. Put it on the side the head is moving toward.
- Mixing up the C variants. In C-SCAN the return trip serves nothing. If your path serves requests while going back, you have drawn SCAN.
Fairness and starvation
None of these algorithms starve a request, since every sweep eventually reaches every cylinder. They differ in how evenly they treat positions.
With SCAN, cylinders in the middle of the disk are passed twice in quick succession on a sweep and its return. Cylinders near the edges are visited at longer, uneven intervals. A request that arrives just behind the head waits almost a full round trip. C-SCAN and C-LOOK treat the cylinders as a circular list, so every cylinder is visited at the same interval. This makes the waiting time more uniform, at the cost of a long jump.
LOOK and C-LOOK are the usual choices in practice, because they avoid pointless travel to an edge with no requests. Under heavy load, SCAN and C-SCAN perform well, since the queue is full and the head is rarely idle.
Exercise
The head is at cylinder 50 on a disk with cylinders 0 to 199, moving toward higher cylinders. The queue is 10, 30, 70, 90, 150, 190. Compute the total head movement for LOOK, and for C-LOOK counting the jump.
Show answer
Requests below the head are 10 and 30. Requests above it are 70, 90, 150 and 190. LOOK goes 50 → 190, which is 140, then 190 → 10, which is 180, so the total is 320. C-LOOK goes 50 → 190, which is 140, jumps 190 → 10, which is 180, then goes 10 → 30, which is 20. The total is 340, or 160 without the jump.
Exercise
Why does C-SCAN give more uniform waiting times than SCAN?
Show answer
SCAN serves requests on both the outward and return sweeps, so cylinders near the middle are visited twice per round trip and cylinders near the edges are visited once. C-SCAN serves in one direction only, so every cylinder is visited exactly once per cycle and the interval between visits is the same everywhere.