Lesson 2 · Storage and I/O

SSTF Disk Scheduling: Nearest First and Starvation

Before every move, serve the pending request closest to the head. Cheap on average, but not optimal, and a far request can wait forever.

SSTF Disk Scheduling: Nearest First and Starvation concept diagramA visual explanation of the layout and operations shown in this lesson.always the nearest pending request — cheapest here, but 183 waits until last05010015019914376594122140183cylinder8282380281843head 57total head movement: 228 cylinders
1

How Shortest Seek Time First Works

  1. Start with the head position and the list of pending requests
  2. Find the nearest pending request below the head and the nearest above
  3. Serve whichever is closer and add that distance to the total
  4. Move the head there and remove the request from the list
  5. Repeat until no request is left

SSTF (shortest seek time first) picks the next disk request by distance alone. Before every move it looks at all pending requests and serves the one whose cylinder is closest to the head. Arrival order and the direction the head was moving play no part.

Seek time grows with the distance the arm travels, so the closest request is the cheapest one to serve right now. That is the whole idea, and it is the disk version of SJF in CPU scheduling: always take the shortest job next.

One fact makes the choice quick. The nearest request is always one of two: the closest pending request below the head or the closest above it. Anything further out on either side is beaten by its own neighbour.

  • A request at the head's own cylinder costs 0 and goes first
  • Equal distances need a tie rule; state it before working an answer
  • The choice is redone after every move, including for requests that arrive mid-run
2

SSTF Disk Scheduling Example: 228 Cylinders

This uses the same queue as every other disk scheduling lesson here: cylinders 0 to 199, head at 57, requests 94, 183, 37, 122, 14, 140, 65.

From 57 the candidates are 37 (20 away) and 65 (8 away), so the head goes to 65. From 65, 37 is 28 away and 94 is 29: the head turns back down by one cylinder's margin. It serves 37, then 14 (23), and with nothing left below it climbs 80 cylinders to 94. The upper requests then fall in a line: 122 (28), 140 (18), 183 (43).

Service order: 65, 37, 14, 94, 122, 140, 183. Total: 8 + 28 + 23 + 80 + 28 + 18 + 43 = 228 cylinders, about 32.6 per request. On the same queue FCFS needs 666 and LOOK needs 295.

  • The head changes direction twice: at 65 and at 14
  • The 80-cylinder climb from 14 to 94 is the most expensive move
  • No direction is given in the question, because SSTF does not use one
3

Why the Nearest Request Is Not Always Best

SSTF is greedy: each move is the cheapest available, but the total is not the smallest possible. On the example, going down to 37 and 14 first and then sweeping up once to 183 costs 20 + 23 + 169 = 212. SSTF paid 16 more because its first move went up 8 cylinders to 65 and then crossed those 8 again to come back down.

For a fixed batch on one axis, the best order is easy to find: go to one end of the requests, then sweep to the other end, whichever end is cheaper first. SSTF does not do this because it never looks past the next move.

Greedy does worst when requests alternate around the head at growing distances. With the head at 100 and requests 92, 110, 72, 0, 150, the nearest request switches sides four times and SSTF moves 286 cylinders. Sweeping up to 150 and then down to 0 moves 200.

  • Cheapest next move is not cheapest total
  • Static batch: the best order is one end, then the other
  • Zigzag queues are the worst case for SSTF
Implementation

SSTF on the head-57 queue

def sstf(requests, head):
    pending = list(requests)
    order, total = [], 0
    while pending:
        nearest = min(pending, key=lambda c: (abs(c - head), c))
        total += abs(nearest - head)
        head = nearest
        pending.remove(nearest)
        order.append(nearest)
    return order, total


order, total = sstf([94, 183, 37, 122, 14, 140, 65], 57)
print("Order:", " ".join(map(str, order)))
print("Cylinders moved:", total)
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

int sstf(vector<int> pending, int head, vector<int>& order) {
    int total = 0;
    while (!pending.empty()) {
        size_t best = 0;
        for (size_t i = 1; i < pending.size(); i++) {
            int d = abs(pending[i] - head), bd = abs(pending[best] - head);
            if (d < bd || (d == bd && pending[i] < pending[best])) best = i;
        }
        total += abs(pending[best] - head);
        head = pending[best];
        order.push_back(head);
        pending.erase(pending.begin() + best);
    }
    return total;
}

int main() {
    vector<int> order;
    int total = sstf({94, 183, 37, 122, 14, 140, 65}, 57, order);
    cout << "Order:";
    for (int c : order) cout << " " << c;
    cout << "\nCylinders moved: " << total << "\n";
}
import java.util.ArrayList;
import java.util.List;

public class Main {
    static int sstf(List<Integer> pending, int head, List<Integer> order) {
        int total = 0;
        while (!pending.isEmpty()) {
            int best = 0;
            for (int i = 1; i < pending.size(); i++) {
                int d = Math.abs(pending.get(i) - head), bd = Math.abs(pending.get(best) - head);
                if (d < bd || (d == bd && pending.get(i) < pending.get(best))) best = i;
            }
            total += Math.abs(pending.get(best) - head);
            head = pending.remove(best);
            order.add(head);
        }
        return total;
    }

    public static void main(String[] args) {
        List<Integer> pending = new ArrayList<>(List.of(94, 183, 37, 122, 14, 140, 65));
        List<Integer> order = new ArrayList<>();
        int total = sstf(pending, 57, order);
        StringBuilder sb = new StringBuilder("Order:");
        for (int c : order) sb.append(" ").append(c);
        System.out.println(sb);
        System.out.println("Cylinders moved: " + total);
    }
}
Watch it run

Step through it

Running on requests 94 183 37 122 14 140 65, head 57, disk 0-199

the queue on one cylinder axis01999418337122141406557headrule: next = the pending request with the smallest seek
Step 1

Seven requests, head at 57

Cylinders run 0 to 199. Seven requests wait at 94, 183, 37, 122, 14, 140 and 65, and the head is at 57. SSTF ignores arrival order and direction: before every move it asks only which pending request is closest to where the head is now.

Output
1 / 11
4

Starvation and Uneven Waiting

The serious problem with SSTF is starvation. A request far from the head is served only when nothing closer is pending. If new requests keep arriving near the head, each one wins the comparison and the far request waits with no limit. It is never rejected; it just never becomes the nearest.

This happens in practice when a program reads one busy region of the disk over and over. Requests near the middle of the disk also do better than requests at the edges, because the head spends most of its time near the middle. So the average wait is low, but the spread of waits is large.

The usual fix is aging: the longer a request waits, the more its effective distance shrinks, until it wins. The other fix is to stop choosing by distance alone and commit to a direction, which is what SCAN does.

  • A far request can wait forever under a steady nearby load
  • Edge cylinders wait longer than middle ones
  • Aging or a deadline bounds the wait
5

SSTF vs SCAN

Both cut seek distance compared with FCFS, but they make different promises. SSTF promises the cheapest next move. SCAN promises that every request is reached within one trip across the disk and back.

SSTF vs SCAN on the head-57 queue
SSTFSCAN (moving up)
Next request chosen bySmallest distance from the headNext request in the current direction
Head movement here228327
Needs a directionNoYes
StarvationPossibleNot possible
Spread of waiting timesLargeSmaller
  • SSTF wins on total distance for this queue
  • SCAN wins on the worst wait any request can see
  • Exam answers usually ask for both the total and the starvation point
6

Advantages and Disadvantages

SSTF gives high throughput and a low average response time, which is why textbooks present it right after FCFS. Its costs are starvation, uneven waits, and the work of finding the nearest request before each move.

  • Advantage: much less head movement than FCFS on most queues
  • Advantage: simple rule, no direction to track
  • Disadvantage: far requests can starve
  • Disadvantage: waits vary widely between requests
  • Disadvantage: frequent direction changes, which slow the arm
7

Implementing SSTF

The simple version scans every pending request before each move: O(n²) for n requests, fine for a short queue. The code here does that and breaks ties toward the lower cylinder.

Because the served requests always form one unbroken range around the start position, a faster version sorts the requests once and keeps two pointers, one on each side of the head. Each move compares just those two and steps one pointer outward: O(n log n) for the sort and O(n) after it.

Requests that arrive while the head is moving are added to the pending set and compete in the next comparison. That is exactly how starvation happens, so a real implementation also records when each request arrived.

  • Scan all pending: O(n²)
  • Sort plus two pointers: O(n log n)
  • Store arrival times if waits must be bounded
8

SSTF in Real Drivers

Plain SSTF was rarely shipped because of starvation. What shipped was SSTF with a time limit. Linux's mq-deadline keeps requests sorted by block number and serves the nearest, but every request also gets an expiry, 500 ms for reads and 5 seconds for writes by default. An expired request jumps the queue.

On an SSD there is no arm, so distance between block numbers says nothing about cost. Linux usually uses the none scheduler for NVMe drives and leaves ordering to the drive itself.

  • mq-deadline: nearest-first plus an expiry per request
  • Reads expire sooner than writes, because a program usually waits on a read
  • SSDs gain nothing from distance-based ordering