Eight programs ask a hard disk for data at the same moment, each wanting a different place on the disk. The kernel can serve them in any order. Does the order matter?
On a spinning disk it matters a great deal, and the reason is mechanical. This lesson looks at what makes a hard disk slow, at the algorithms the kernel uses to choose the order, and at why solid-state drives made most of that work unnecessary.
What a hard disk does
A hard disk stores data on one or more flat, spinning platters. Each surface is divided into circular tracks, and the tracks at the same distance from the centre on every surface together make a cylinder. A read-write head sits on the end of an arm, and the arm moves the head in and out across the tracks. To read a block, the disk must do three things in turn:
- Seek: move the arm so that the head is over the right cylinder.
- Rotate: wait until the right part of the track spins round under the head. This is the rotational latency.
- Transfer: read the bytes as they pass under the head.
Transfer is fast. Seek and rotation are mechanical, so they are slow, and the seek is the part the operating system can influence. The further the arm has to travel, the longer the request takes. If the kernel serves requests in a clever order, the arm travels less and the disk gets through the queue sooner.
Real disks hide their geometry behind a plain list of numbered blocks, but block numbers that are close together still sit close together on the platter, so the idea carries over. In this lesson, a request is just a cylinder number, and the cost is head movement: the number of cylinders the head crosses.
The queue used in every simulator below is the same invented one. The disk has cylinders 0 to 199, the head is on cylinder 100, and eight requests are waiting, in the order they arrived: 36, 160, 120, 12, 142, 85, 54, 185.
FCFS and SSTF
The simplest algorithm is FCFS: serve the requests in the order they arrived. It is fair, and it can be terrible, because the next request may be on the far side of the disk. Look at the zig-zag the head draws, and at the bottom pane, where SSTF serves the same queue.
SSTF (shortest seek time first) always serves the waiting request that is nearest to the head. It is the disk version of SJF (Lesson 3.2): greedy, and very good on average.
FCFS against SSTF on the same queue
-
The head is at cylinder 100
The disk has cylinders 0 to 199. The head is on cylinder 100 and 8 requests are waiting, in arrival order: 36, 160, 120, 12, 142, 85, 54, 185. FCFS will serve the requests in the order they arrived, whatever that costs in head movement.
- Head at
- cylinder 100
- Head movement so far
- 0 cylinders
- Served
- none yet
- Still waiting
- 36, 160, 120, 12, 142, 85, 54, 185
-
Serve cylinder 36
The head moves from 100 to 36: 64 cylinders. It was the next request in the queue.
- Head at
- cylinder 36
- Head movement so far
- 64 cylinders
- Served
- 36
- Still waiting
- 160, 120, 12, 142, 85, 54, 185
-
Serve cylinder 160
The head moves from 36 to 160: 124 cylinders. It was the next request in the queue.
- Head at
- cylinder 160
- Head movement so far
- 188 cylinders
- Served
- 36, 160
- Still waiting
- 120, 12, 142, 85, 54, 185
-
Serve cylinder 120
The head moves from 160 to 120: 40 cylinders. It was the next request in the queue.
- Head at
- cylinder 120
- Head movement so far
- 228 cylinders
- Served
- 36, 160, 120
- Still waiting
- 12, 142, 85, 54, 185
-
Serve cylinder 12
The head moves from 120 to 12: 108 cylinders. It was the next request in the queue.
- Head at
- cylinder 12
- Head movement so far
- 336 cylinders
- Served
- 36, 160, 120, 12
- Still waiting
- 142, 85, 54, 185
-
Serve cylinder 142
The head moves from 12 to 142: 130 cylinders. It was the next request in the queue.
- Head at
- cylinder 142
- Head movement so far
- 466 cylinders
- Served
- 36, 160, 120, 12, 142
- Still waiting
- 85, 54, 185
-
Serve cylinder 85
The head moves from 142 to 85: 57 cylinders. It was the next request in the queue.
- Head at
- cylinder 85
- Head movement so far
- 523 cylinders
- Served
- 36, 160, 120, 12, 142, 85
- Still waiting
- 54, 185
-
Serve cylinder 54
The head moves from 85 to 54: 31 cylinders. It was the next request in the queue.
- Head at
- cylinder 54
- Head movement so far
- 554 cylinders
- Served
- 36, 160, 120, 12, 142, 85, 54
- Still waiting
- 185
-
Serve cylinder 185
The head moves from 54 to 185: 131 cylinders. It was the next request in the queue. Every request is served. Total head movement: 685 cylinders.
- Head at
- cylinder 185
- Head movement so far
- 685 cylinders
- Served
- 36, 160, 120, 12, 142, 85, 54, 185
- Still waiting
- none
FCFS moved the head 685 cylinders in all. SSTF served the same eight requests with 261. The zig-zag is gone: SSTF walks down through 85, 54, 36 and 12, turns round once, and walks up through the rest. The Moves view lists every move with its distance if you want to check the sums.
Press Try it and change the head position or the queue. SSTF is usually far ahead of FCFS, but it has a flaw.
The flaw: starvation
Because SSTF always picks the nearest request, a request far from the head can wait a very long time. If new requests keep arriving near the head, the far one is never the nearest. That is starvation, the same problem SJF has with long jobs. In this fixed queue nothing new arrives, so the far requests do get served in the end, but you can still see the effect: cylinder 160 was the second request to arrive, and SSTF served it seventh.
SSTF makes the far request wait
-
The head is at cylinder 100
The disk has cylinders 0 to 199. The head is on cylinder 100 and 8 requests are waiting, in arrival order: 36, 160, 120, 12, 142, 85, 54, 185. SSTF will always serve the waiting request nearest to the head.
- Head at
- cylinder 100
- Head movement so far
- 0 cylinders
- Served
- none yet
- Still waiting
- 36, 160, 120, 12, 142, 85, 54, 185
-
Serve cylinder 85
The head moves from 100 to 85: 15 cylinders. It was the nearest of the 8 waiting requests (15 away; the next nearest was 20 away).
- Head at
- cylinder 85
- Head movement so far
- 15 cylinders
- Served
- 85
- Still waiting
- 36, 160, 120, 12, 142, 54, 185
-
Serve cylinder 54
The head moves from 85 to 54: 31 cylinders. It was the nearest of the 7 waiting requests (31 away; the next nearest was 35 away).
- Head at
- cylinder 54
- Head movement so far
- 46 cylinders
- Served
- 85, 54
- Still waiting
- 36, 160, 120, 12, 142, 185
-
Serve cylinder 36
The head moves from 54 to 36: 18 cylinders. It was the nearest of the 6 waiting requests (18 away; the next nearest was 42 away).
- Head at
- cylinder 36
- Head movement so far
- 64 cylinders
- Served
- 85, 54, 36
- Still waiting
- 160, 120, 12, 142, 185
-
Serve cylinder 12
The head moves from 36 to 12: 24 cylinders. It was the nearest of the 5 waiting requests (24 away; the next nearest was 84 away).
- Head at
- cylinder 12
- Head movement so far
- 88 cylinders
- Served
- 85, 54, 36, 12
- Still waiting
- 160, 120, 142, 185
-
Serve cylinder 120
The head moves from 12 to 120: 108 cylinders. It was the nearest of the 4 waiting requests (108 away; the next nearest was 130 away).
- Head at
- cylinder 120
- Head movement so far
- 196 cylinders
- Served
- 85, 54, 36, 12, 120
- Still waiting
- 160, 142, 185
-
Serve cylinder 142
The head moves from 120 to 142: 22 cylinders. It was the nearest of the 3 waiting requests (22 away; the next nearest was 40 away).
- Head at
- cylinder 142
- Head movement so far
- 218 cylinders
- Served
- 85, 54, 36, 12, 120, 142
- Still waiting
- 160, 185
-
Serve cylinder 160
The head moves from 142 to 160: 18 cylinders. It was the nearest of the 2 waiting requests (18 away; the next nearest was 43 away).
- Head at
- cylinder 160
- Head movement so far
- 236 cylinders
- Served
- 85, 54, 36, 12, 120, 142, 160
- Still waiting
- 185
-
Serve cylinder 185
The head moves from 160 to 185: 25 cylinders. It was the last request left. Every request is served. Total head movement: 261 cylinders.
- Head at
- cylinder 185
- Head movement so far
- 261 cylinders
- Served
- 85, 54, 36, 12, 120, 142, 160, 185
- Still waiting
- none
SCAN: the elevator
To stop the far end of the disk from being ignored, the kernel can make the head sweep like an elevator. SCAN moves the head in one direction, serving every request it passes, and when it has gone as far as it can it turns round and sweeps back. No request is passed over for more than two sweeps, so none starves.
SCAN goes all the way to the end of the disk before it turns, even if the last request is nowhere near the end. LOOK is the same sweep, but it turns round at the last request in that direction. LOOK looks ahead; SCAN does not. (Textbooks mostly follow this definition, but a few draw SCAN as stopping at the last request. This course uses the one above, and the simulator follows it.)
Here the head sweeps upwards first. Watch what happens after it serves 185.
SCAN goes to the end, LOOK turns at the last request
-
The head is at cylinder 100
The disk has cylinders 0 to 199. The head is on cylinder 100 and 8 requests are waiting, in arrival order: 36, 160, 120, 12, 142, 85, 54, 185. SCAN sweeps up (towards higher numbers) serving requests as it passes them, all the way to the end of the disk, then turns.
- Head at
- cylinder 100
- Head movement so far
- 0 cylinders
- Served
- none yet
- Still waiting
- 36, 160, 120, 12, 142, 85, 54, 185
-
Serve cylinder 120
The head moves from 100 to 120: 20 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 120
- Head movement so far
- 20 cylinders
- Served
- 120
- Still waiting
- 36, 160, 12, 142, 85, 54, 185
-
Serve cylinder 142
The head moves from 120 to 142: 22 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 142
- Head movement so far
- 42 cylinders
- Served
- 120, 142
- Still waiting
- 36, 160, 12, 85, 54, 185
-
Serve cylinder 160
The head moves from 142 to 160: 18 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 160
- Head movement so far
- 60 cylinders
- Served
- 120, 142, 160
- Still waiting
- 36, 12, 85, 54, 185
-
Serve cylinder 185
The head moves from 160 to 185: 25 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 185
- Head movement so far
- 85 cylinders
- Served
- 120, 142, 160, 185
- Still waiting
- 36, 12, 85, 54
-
Reach the end, then turn
No request is left ahead, but SCAN does not turn here. It runs on to the end of the disk, cylinder 199: 14 more cylinders of movement that serves nothing. Then it turns and sweeps down (towards 0).
- Head at
- cylinder 199
- Head movement so far
- 99 cylinders
- Served
- 120, 142, 160, 185
- Still waiting
- 36, 12, 85, 54
-
Serve cylinder 85
The head moves from 199 to 85: 114 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 85
- Head movement so far
- 213 cylinders
- Served
- 120, 142, 160, 185, 85
- Still waiting
- 36, 12, 54
-
Serve cylinder 54
The head moves from 85 to 54: 31 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 54
- Head movement so far
- 244 cylinders
- Served
- 120, 142, 160, 185, 85, 54
- Still waiting
- 36, 12
-
Serve cylinder 36
The head moves from 54 to 36: 18 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 36
- Head movement so far
- 262 cylinders
- Served
- 120, 142, 160, 185, 85, 54, 36
- Still waiting
- 12
-
Serve cylinder 12
The head moves from 36 to 12: 24 cylinders. It lies in the direction the head is sweeping. Every request is served. Total head movement: 286 cylinders.
- Head at
- cylinder 12
- Head movement so far
- 286 cylinders
- Served
- 120, 142, 160, 185, 85, 54, 36, 12
- Still waiting
- none
Both serve the requests in the same order. SCAN’s head moved 286 cylinders and LOOK’s only 258: the difference is the trip from 185 out to the last cylinder and back again. In the top pane, the path bends at 199. In the bottom pane, it bends at 185. In practice almost every real elevator algorithm is a LOOK: nobody sends the arm to the end for no reason.
The circular versions
SCAN and LOOK are slightly unfair in one way. After the head turns, it passes over the area it has just served, so requests near the middle of the disk get served twice as often as requests at the ends. The circular versions fix that by sweeping only in one direction. C-SCAN goes to the end, jumps straight back to the other end without serving anything, and sweeps again. C-LOOK does the same but only travels as far as the last request, and jumps to the first request at the other end. The jump is head movement and it counts.
C-SCAN against C-LOOK
-
The head is at cylinder 100
The disk has cylinders 0 to 199. The head is on cylinder 100 and 8 requests are waiting, in arrival order: 36, 160, 120, 12, 142, 85, 54, 185. C-SCAN sweeps up (towards higher numbers) to the end of the disk, then jumps back to the other end and sweeps the same way again.
- Head at
- cylinder 100
- Head movement so far
- 0 cylinders
- Served
- none yet
- Still waiting
- 36, 160, 120, 12, 142, 85, 54, 185
-
Serve cylinder 120
The head moves from 100 to 120: 20 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 120
- Head movement so far
- 20 cylinders
- Served
- 120
- Still waiting
- 36, 160, 12, 142, 85, 54, 185
-
Serve cylinder 142
The head moves from 120 to 142: 22 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 142
- Head movement so far
- 42 cylinders
- Served
- 120, 142
- Still waiting
- 36, 160, 12, 85, 54, 185
-
Serve cylinder 160
The head moves from 142 to 160: 18 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 160
- Head movement so far
- 60 cylinders
- Served
- 120, 142, 160
- Still waiting
- 36, 12, 85, 54, 185
-
Serve cylinder 185
The head moves from 160 to 185: 25 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 185
- Head movement so far
- 85 cylinders
- Served
- 120, 142, 160, 185
- Still waiting
- 36, 12, 85, 54
-
Reach the end of the disk
Nothing is left ahead, so the head runs on to the end of the disk, cylinder 199: 14 cylinders. Next it will jump back to the other end.
- Head at
- cylinder 199
- Head movement so far
- 99 cylinders
- Served
- 120, 142, 160, 185
- Still waiting
- 36, 12, 85, 54
-
Jump back to cylinder 0
The head jumps back to the other end of the disk, cylinder 0, without serving anything. The jump is 199 cylinders of movement, and it counts. The sweep then continues up (towards higher numbers).
- Head at
- cylinder 0
- Head movement so far
- 298 cylinders
- Served
- 120, 142, 160, 185
- Still waiting
- 36, 12, 85, 54
-
Serve cylinder 12
The head moves from 0 to 12: 12 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 12
- Head movement so far
- 310 cylinders
- Served
- 120, 142, 160, 185, 12
- Still waiting
- 36, 85, 54
-
Serve cylinder 36
The head moves from 12 to 36: 24 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 36
- Head movement so far
- 334 cylinders
- Served
- 120, 142, 160, 185, 12, 36
- Still waiting
- 85, 54
-
Serve cylinder 54
The head moves from 36 to 54: 18 cylinders. It lies in the direction the head is sweeping.
- Head at
- cylinder 54
- Head movement so far
- 352 cylinders
- Served
- 120, 142, 160, 185, 12, 36, 54
- Still waiting
- 85
-
Serve cylinder 85
The head moves from 54 to 85: 31 cylinders. It lies in the direction the head is sweeping. Every request is served. Total head movement: 383 cylinders.
- Head at
- cylinder 85
- Head movement so far
- 383 cylinders
- Served
- 120, 142, 160, 185, 12, 36, 54, 85
- Still waiting
- none
The circular versions cost more head movement in total (383 and 331 here, against 286 and 258 for the sweeping versions), but they give every part of the disk the same waiting time, which suits a disk with a heavy, steady load. C-LOOK is the same sweep as C-SCAN with the wasted trips to the ends cut off, just as LOOK trims SCAN.
SSDs change the problem
A solid-state drive has no platters, no arm and no head. Reading a block from one part of an SSD is as fast as reading from any other, so there is no seek to avoid. Everything above, the elevator and all, was about a cost that an SSD does not have.
An SSD has other problems of its own, and the controller inside the drive deals with most of them:
- Pages and erase blocks. Flash is written a page at a time, but it can only be erased a whole erase block (many pages) at a time, and a page cannot be overwritten before its block is erased. So the drive writes new data to a fresh page and marks the old one stale.
- Wear levelling. Each flash cell survives only a limited number of erases. The controller spreads the writes over all the cells so that none wears out early.
- Garbage collection and TRIM. The drive must eventually erase blocks full of stale pages to get free space. The TRIM command lets the operating system tell the drive which blocks a deleted file used to occupy, so the drive does not waste effort preserving them.
For the operating system, the result is that the clever reordering matters much less. Linux has several I/O schedulers: some reorder and merge requests, some mainly add fairness or deadlines, and none simply passes requests on in order. Fast SSDs on a modern interface such as NVMe usually use none, because the drive’s own controller can keep many requests in flight and reorder them better than the kernel can.
In the real world
On Linux you can ask which scheduler a device is using, and whether the kernel thinks it spins:
$ cat /sys/block/sda/queue/scheduler
$ lsblk -d -o NAME,ROTA
$ sudo fstrim -v /
The first command lists the schedulers available for the device, with the active one in square brackets. In the ROTA column of lsblk, 1 means a rotating disk and 0 means an SSD. fstrim tells the drive about every block that the file system has freed, and many distributions run it on a timer. Windows runs the same idea as “Optimize Drives”, and macOS issues TRIM for you. :::
Quick check
Quick check
-
SSTF always picks the nearest request. A far request that is never the nearest keeps being passed over, which is starvation.
-
Both sweep like an elevator and serve requests as they pass. LOOK saves the trip from the last request to the end and back.
-
After turning, SCAN and LOOK pass over the area they just served. The circular versions jump back and sweep the same way again, which is fairer, at the cost of the jump.
-
The elevator algorithms save head movement. An SSD has no head, and its controller already manages its own flash.
Join the discussion