ENع
←Disk scheduling
Lesson 50 · I/O, virtualisation and isolation

Disk scheduling

14 min8 steps5 questionsAdvanced
About this lesson

A hard disk can only read a track after its arm swings the head there, and that seek takes milliseconds. When many programs are waiting, the order the operating system serves their requests decides how far the head travels. Follow one queue of eight requests, head on track 53, served three ways: first come first served (640 tracks), shortest seek time first (236, but it can starve a far request) and the elevator, SCAN (236, and nobody waits forever). The Hardware lesson on interrupts and I/O covers how a request reaches the device and how the device says it is done.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 50 · Interactive
01 / 08

Why order matters

A hard disk keeps data on rings called tracks. To read one, an arm must move the head to that track first: a seek. Seeks are slow, about 4 to 9 ms on average, so less arm travel means faster reads.

time for one read = seek + rotation + transfer

seek       move the arm to the right track
rotation   wait for the data to spin under the head
transfer   read the bytes

average seek (OSTEP, Fig. 37.5):
  Cheetah 15K.5    15,000 RPM   4 ms
  Barracuda         7,200 RPM   9 ms

Below, the tracks are laid out in a straight line:
track 0 at the outer edge, track 199 at the inner edge.
Loading figure…
or ← →
Read the lesson step by step
  1. Why order matters

    A hard disk keeps data on rings called tracks. To read one, an arm must move the head to that track first: a seek. Seeks are slow, about 4 to 9 ms on average, so less arm travel means faster reads.

    time for one read = seek + rotation + transfer
    
    seek       move the arm to the right track
    rotation   wait for the data to spin under the head
    transfer   read the bytes
    
    average seek (OSTEP, Fig. 37.5):
      Cheetah 15K.5    15,000 RPM   4 ms
      Barracuda         7,200 RPM   9 ms
    
    Below, the tracks are laid out in a straight line:
    track 0 at the outer edge, track 199 at the inner edge.
  2. A queue of requests

    Many programs ask for data at once, so the operating system keeps a queue of requests, each naming a track. Eight wait here: 98, 183, 37, 122, 14, 124, 65, 67. The head is on track 53. Which one should go next?

    head     track 53
    queue    98  183  37  122  14  124  65  67   (arrival order)
    disk     tracks 0 .. 199
    
    the disk scheduler picks the order;
    we score each order by the total tracks the head moves
  3. First come, first served

    FCFS serves requests in arrival order. That is fair, but the head zigzags across the whole disk: 53 to 98, out to 183, back to 37. In total it moves 640 tracks.

    FCFS (also called FIFO)
    
     53 ->  98    45
     98 -> 183    85
    183 ->  37   146
     37 -> 122    85
    122 ->  14   108
     14 -> 124   110
    124 ->  65    59
     65 ->  67     2
                   ---
    total          640 tracks
  4. Shortest seek first

    SSTF always serves the request nearest the head. From 53 it goes to 65 and 67, then down to 14, then up to 183. The head moves only 236 tracks instead of 640.

    SSTF: shortest seek time first
    
     53 ->  65    12
     65 ->  67     2
     67 ->  37    30
     37 ->  14    23
     14 ->  98    84
     98 -> 122    24
    122 -> 124     2
    124 -> 183    59
                   ---
    total          236 tracks
    
    An OS sees block numbers, not tracks, so it does the
    same thing by block number: nearest-block-first.
  5. The catch: starvation

    SSTF has a flaw. Say the head is on 65 and 183 is waiting. If new requests keep arriving near the head, each one is nearer, so it jumps ahead. Track 183 can wait forever: it starves.

    head 65, waiting: 183  (118 tracks away)
    
    new 90   25 away  -> SSTF picks 90
    new 115   25 away  -> SSTF picks 115
    new 75   40 away  -> SSTF picks 75
    new 130   55 away  -> SSTF picks 130
    
    183 is never the nearest while the stream lasts
  6. The elevator: SCAN

    SCAN sweeps like a lift: one way to the edge, serving everything it passes, then back. Down from 53: 37, 14, the edge, then up to 183. 236 tracks, and nobody waits forever.

    SCAN, the elevator algorithm (heading down first)
    
     53 ->  37    16
     37 ->  14    23
     14 ->   0    14   edge: turn round
      0 ->  65    65
     65 ->  67     2
     67 ->  98    31
     98 -> 122    24
    122 -> 124     2
    124 -> 183    59
                   ---
    total          236 tracks
    
    a request behind the head waits for the way back,
    but never longer than that
    C-SCAN sweeps one way only, then jumps back to the
    start: fairer to the outer and inner tracks.
  7. Comparing the three

    FCFS moved 640 tracks, SSTF 236, SCAN 236. SSTF and SCAN both cut the travel by more than 60 percent. Of those two fast ones, only SCAN also promises that every request gets served: no starvation.

    the three orders as a Python simulation:
    
    def fcfs(head, q):
        moved = 0
        for t in q:
            moved += abs(t - head); head = t
        return moved
    
    def sstf(head, q):
        q, moved = list(q), 0
        while q:
            t = min(q, key=lambda t: abs(t - head))
            q.remove(t); moved += abs(t - head); head = t
        return moved
    
    def scan_down(head, q):  # to track 0, then up
        return head + max([t for t in q if t > head], default=0)
    
    queue = [98, 183, 37, 122, 14, 124, 65, 67]
    for f in (fcfs, sstf, scan_down):
        print(f.__name__, f(53, queue))
    
    prints:
    fcfs 640
    sstf 236
    scan_down 236
  8. Who schedules today

    Modern disks know their exact head position, so the OS hands them several requests and the drive orders them itself. Linux still offers schedulers such as mq-deadline. Solid-state drives (SSDs) have no moving head, so there is often less to gain.

    see (and change) the scheduler for a disk on Linux:
    
    $ cat /sys/block/sda/queue/scheduler
    [mq-deadline] kyber bfq none
    
    the same with open() and read() in C (C99, POSIX):
    
    #define _POSIX_C_SOURCE 200809L
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    
    /* print the I/O schedulers Linux offers for a disk */
    int main(int argc, char *argv[]) {
        char path[256], buf[256];
        snprintf(path, sizeof path, "/sys/block/%s/queue/scheduler",
                 argc > 1 ? argv[1] : "sda");
        int fd = open(path, O_RDONLY);
        if (fd < 0) { perror(path); return 1; }
        ssize_t n = read(fd, buf, sizeof buf - 1);
        close(fd);
        if (n < 0) { perror("read"); return 1; }
        buf[n] = '\0';
        fputs(buf, stdout);   /* e.g. [mq-deadline] kyber bfq none */
        return 0;
    }

Quiz Unlocks after the last step.