Disk Scheduling and I/O
A hard disk spends most of each request moving its head, so the order in which requests are served changes the total time a lot. Disk scheduling picks that order. On an SSD there is no head, and most of the problem disappears.
Why Order Matters on a Disk
A hard disk request has three costs. Seek time moves the head to the right cylinder, typically 5 to 10 ms. Rotational latency waits for the sector to come under the head, on average half a turn: about 4 ms at 7,200 rpm. Transfer time reads the data, usually well under a millisecond.
Disk scheduling in an operating system means choosing the order of pending requests so the head travels as little as possible. Only the seek depends on that order, so seek time in these problems is measured as total head movement in cylinders. Ordering only helps when several requests are queued; with one request there is nothing to choose.
- Seek 5–10 ms, rotation about 4 ms, transfer under 1 ms
- Exam answers count total head movement, the sum of |next − current|
The Six Algorithms
All six disk scheduling algorithms below are run on the same queue: head at 57, requests 94, 183, 37, 122, 14, 140, 65, cylinders 0 to 199, sweeping upwards first. Each one has its own lesson with a full worked example.
FCFS serves requests in arrival order. It is fair and simple, but the head zigzags across the disk. SSTF always serves the nearest request; it moves least but can starve a far request while near ones keep arriving.
SCAN moves in one direction to the end of the disk, serving everything on the way, then reverses, like a lift. LOOK does the same but turns at the last request instead of the edge.
C-SCAN serves only in one direction: at the end it jumps back to cylinder 0 and sweeps again, which makes waiting times more even. C-LOOK jumps to the lowest pending request instead of to 0.
| Algorithm | Order served | Cylinders | Can starve |
|---|---|---|---|
| FCFS | 94, 183, 37, 122, 14, 140, 65 | 666 | No |
| SSTF | 65, 37, 14, 94, 122, 140, 183 | 228 | Yes |
| SCAN | 65, 94, 122, 140, 183, (199), 37, 14 | 327 | No |
| LOOK | 65, 94, 122, 140, 183, 37, 14 | 295 | No |
| C-SCAN | 65, 94, 122, 140, 183, (199), (0), 14, 37 | 378 | No |
| C-LOOK | 65, 94, 122, 140, 183, 14, 37 | 318 | No |
- Cheapest is not fairest: SSTF wins on distance and is the only one that can starve
- SCAN's extra 32 over LOOK is the empty trip from 183 to 199 and back
- C-SCAN counts the 199-cylinder return jump; some textbooks leave it out, giving 179
How I/O Reaches Memory
A device driver gives every device the same interface, such as open, read and write, so the same read() works on a hard disk, an SSD or a USB stick. Below the driver, there are three ways to move the data.
With programmed I/O the CPU copies every byte between the device and memory and waits while it does. With interrupt-driven I/O the CPU starts a request and does other work, and the device interrupts when it is ready; the CPU still copies the data. With direct memory access (DMA), the CPU gives a DMA controller the source, destination and length, the controller copies the whole block into memory, and it raises one interrupt when done.
On top of this, buffering lets a slow device and a fast program work at their own speeds (spooling and buffering), and caching keeps recently used blocks in RAM so repeated reads never reach the disk. Linux uses free memory as a page cache for exactly this reason.
| Technique | CPU copies data | Interrupts | CPU free during transfer |
|---|---|---|---|
| Programmed I/O | Yes, every byte | None (CPU polls) | No |
| Interrupt-driven | Yes | One per byte or word | Between transfers |
| DMA | No | One per block | Yes |
- DMA is what makes fast disks and network cards practical
- Free memory used as page cache is working memory, not waste
Terms, operations, and practical uses
Where the time goes
- Seek timeMoving the head — 5-10 ms, and the dominant cost.
- Rotational latencyWaiting for the sector, ~4 ms at 7200 rpm.
- Transfer timeThe actual read, usually well under a millisecond.
Algorithms
- FCFSArrival order, no reordering; zigzags across the platter. 666 cylinders.
- SSTFNearest request first; cheapest here at 228, but starves distant ones.
- SCANSweeps to the physical end, then reverses — the elevator. 327 cylinders.
- LOOKThe same sweep, but turns at the last real request instead of the disk edge. 295.
- C-SCANOne direction only, then jumps back to cylinder 0 without serving. Evens out waiting. 378.
- C-LOOKC-SCAN that jumps to the lowest request rather than to 0. 318.
Getting data across
- Programmed I/OThe CPU copies every byte itself.
- Interrupt-drivenCPU is free between transfers but still copies.
- DMAThe controller moves data; one interrupt per transfer.
- Page cacheFree memory used for cached blocks by design.
Six disk scheduling algorithms on one request queue
REQS = [94, 183, 37, 122, 14, 140, 65]
START, MAXC = 57, 199
def cost(order):
return sum(abs(order[i] - order[i - 1]) for i in range(1, len(order)))
def fcfs(reqs, head):
return [head] + reqs # arrival order, no reordering
def sstf(reqs, head):
order, pending = [head], reqs[:]
while pending:
# always the closest one left
nxt = min(pending, key=lambda c: abs(c - order[-1]))
pending.remove(nxt)
order.append(nxt)
return order
def scan(reqs, head, end=MAXC):
up = sorted(c for c in reqs if c >= head)
down = sorted((c for c in reqs if c < head), reverse=True)
return [head] + up + [end] + down # walks to the physical end
def look(reqs, head):
up = sorted(c for c in reqs if c >= head)
down = sorted((c for c in reqs if c < head), reverse=True)
return [head] + up + down # turns at the last request
def c_scan(reqs, head, end=MAXC):
up = sorted(c for c in reqs if c >= head)
low = sorted(c for c in reqs if c < head)
return [head] + up + [end, 0] + low # one direction, then reset
def c_look(reqs, head):
up = sorted(c for c in reqs if c >= head)
low = sorted(c for c in reqs if c < head)
return [head] + up + low # jump straight to the lowest
algos = [("FCFS", fcfs), ("SSTF", sstf), ("SCAN", scan),
("LOOK", look), ("C-SCAN", c_scan), ("C-LOOK", c_look)]
parts = [f"{name} {cost(fn(REQS, START))}" for name, fn in algos]
print(" | ".join(parts))#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
// Six disk scheduling algorithms on one request queue.
// Cost is total head movement in cylinders -- the only thing that matters
// on a spinning disk, where a seek costs far more than the transfer.
const int START = 57, MAXC = 199;
int cost(const vector<int>& o) {
int t = 0;
for (size_t i = 1; i < o.size(); i++) t += abs(o[i] - o[i - 1]);
return t;
}
vector<int> above(vector<int> r, int h) {
vector<int> v;
for (int c : r) if (c >= h) v.push_back(c);
sort(v.begin(), v.end());
return v;
}
vector<int> below(vector<int> r, int h, bool desc) {
vector<int> v;
for (int c : r) if (c < h) v.push_back(c);
sort(v.begin(), v.end());
if (desc) reverse(v.begin(), v.end());
return v;
}
vector<int> fcfs(vector<int> r, int h) {
vector<int> o{h}; // arrival order, no reordering
for (int c : r) o.push_back(c);
return o;
}
vector<int> sstf(vector<int> r, int h) {
vector<int> o{h}, p = r;
while (!p.empty()) { // always the closest one left
size_t best = 0;
for (size_t i = 1; i < p.size(); i++)
if (abs(p[i] - o.back()) < abs(p[best] - o.back())) best = i;
o.push_back(p[best]);
p.erase(p.begin() + best);
}
return o;
}
vector<int> scanAlgo(vector<int> r, int h) {
vector<int> o{h}, u = above(r, h), d = below(r, h, true);
for (int c : u) o.push_back(c);
o.push_back(MAXC); // walks to the physical end
for (int c : d) o.push_back(c);
return o;
}
vector<int> look(vector<int> r, int h) {
vector<int> o{h}, u = above(r, h), d = below(r, h, true);
for (int c : u) o.push_back(c); // turns at the last request
for (int c : d) o.push_back(c);
return o;
}
vector<int> cScan(vector<int> r, int h) {
vector<int> o{h}, u = above(r, h), l = below(r, h, false);
for (int c : u) o.push_back(c);
o.push_back(MAXC);
o.push_back(0);
// one direction, then reset;
for (int c : l) o.push_back(c);
return o;
}
vector<int> cLook(vector<int> r, int h) {
vector<int> o{h}, u = above(r, h), l = below(r, h, false);
for (int c : u) o.push_back(c); // jump straight to the lowest
for (int c : l) o.push_back(c);
return o;
}
int main() {
vector<int> reqs{94, 183, 37, 122, 14, 140, 65};
cout << "FCFS " << cost(fcfs(reqs, START))
<< " | SSTF " << cost(sstf(reqs, START))
<< " | SCAN " << cost(scanAlgo(reqs, START))
<< " | LOOK " << cost(look(reqs, START))
<< " | C-SCAN " << cost(cScan(reqs, START))
<< " | C-LOOK " << cost(cLook(reqs, START)) << "\n";
}import java.util.*;
class Main {
// Six disk scheduling algorithms on one request queue.
// Cost is total head movement in cylinders -- the only thing that matters
// on a spinning disk, where a seek costs far more than the transfer.
static final int START = 57, MAXC = 199;
static int cost(List<Integer> o) {
int t = 0;
for (int i = 1; i < o.size(); i++) t += Math.abs(o.get(i) - o.get(i - 1));
return t;
}
static List<Integer> above(int[] r, int h) {
List<Integer> v = new ArrayList<>();
for (int c : r) if (c >= h) v.add(c);
Collections.sort(v);
return v;
}
static List<Integer> below(int[] r, int h, boolean desc) {
List<Integer> v = new ArrayList<>();
for (int c : r) if (c < h) v.add(c);
Collections.sort(v);
if (desc) Collections.reverse(v);
return v;
}
static List<Integer> fcfs(int[] r, int h) {
List<Integer> o = new ArrayList<>(); // arrival order, no reordering
o.add(h);
for (int c : r) o.add(c);
return o;
}
static List<Integer> sstf(int[] r, int h) {
List<Integer> o = new ArrayList<>(), p = new ArrayList<>();
o.add(h);
for (int c : r) p.add(c);
while (!p.isEmpty()) { // always the closest one left
int best = 0;
for (int i = 1; i < p.size(); i++)
if (Math.abs(p.get(i) - o.get(o.size() - 1))
< Math.abs(p.get(best) - o.get(o.size() - 1))) best = i;
o.add(p.remove(best));
}
return o;
}
static List<Integer> scan(int[] r, int h) {
List<Integer> o = new ArrayList<>();
o.add(h);
o.addAll(above(r, h));
o.add(MAXC); // walks to the physical end
o.addAll(below(r, h, true));
return o;
}
static List<Integer> look(int[] r, int h) {
List<Integer> o = new ArrayList<>();
o.add(h);
o.addAll(above(r, h)); // turns at the last request
o.addAll(below(r, h, true));
return o;
}
static List<Integer> cScan(int[] r, int h) {
List<Integer> o = new ArrayList<>();
o.add(h);
o.addAll(above(r, h));
o.add(MAXC);
o.add(0);
// one direction, then reset;
o.addAll(below(r, h, false));
return o;
}
static List<Integer> cLook(int[] r, int h) {
List<Integer> o = new ArrayList<>();
o.add(h);
o.addAll(above(r, h)); // jump straight to the lowest
o.addAll(below(r, h, false));
return o;
}
public static void main(String[] args) {
int[] reqs = {94, 183, 37, 122, 14, 140, 65};
System.out.println("FCFS " + cost(fcfs(reqs, START))
+ " | SSTF " + cost(sstf(reqs, START))
+ " | SCAN " + cost(scan(reqs, START))
+ " | LOOK " + cost(look(reqs, START))
+ " | C-SCAN " + cost(cScan(reqs, START))
+ " | C-LOOK " + cost(cLook(reqs, START)));
}
}Step through it
Running on head at 57; requests 94, 183, 37, 122, 14, 140, 65; cylinders 0-199
What SSDs Changed
An SSD has no head and no platter, so there is no seek and no rotational latency. A random read costs about the same as the next block in line, so the algorithms above optimise a cost that no longer exists.
Linux's default scheduler for NVMe drives is none: requests go to the device in the order they arrive, and the drive's own controller spreads them across its flash chips. For spinning disks, mq-deadline and bfq are still used: mq-deadline sorts requests by position but gives each one an expiry time so none starves, and bfq shares disk time fairly between processes.
Flash brings its own limits. A page cannot be overwritten in place; whole blocks must be erased first. The drive's flash translation layer remaps writes, and wear levelling spreads them so no cell wears out early. The TRIM command tells the drive which blocks the file system no longer needs.
- No seek: reordering buys almost nothing on NVMe
mq-deadline= sort by position + a deadline per request- Hard disks are still common in bulk storage, so disk scheduling still matters there