Secondary storage holds programs and data permanently. Since disks are slow, the order in which requests are served makes a large difference to performance.

Disk Structure

A magnetic hard disk has one or more platters, each with two surfaces coated with magnetic material. Each surface is divided into concentric tracks, and each track into sectors, usually of 512 bytes or 4 KB. The tracks at the same position on all surfaces form a cylinder. A read-write head for each surface is carried on a disk arm, and all the heads move together. To the OS, the disk appears as a one-dimensional array of logical blocks, mapped onto sectors from the outermost cylinder inwards.

Definition: Seek time. The time taken by the disk arm to move the read-write heads to the cylinder containing the requested sector.

Disk Access Time

The time to read a block has three parts. Seek time: moving the arm to the right cylinder, the largest part. Rotational latency: waiting for the sector to rotate under the head, on average half a rotation. Transfer time: reading the data. Disk scheduling aims to reduce the total seek distance, measured in cylinders.

Disk Scheduling Algorithms

FCFS serves requests in arrival order. It is fair but can make the head swing wildly. SSTF (Shortest Seek Time First) serves the request closest to the current head position. It reduces seek time but may starve distant requests. SCAN (elevator) moves the head in one direction to the end of the disk, serving requests on the way, then reverses. C-SCAN (Circular SCAN) moves in one direction to the end, then returns to the start without serving requests, giving more uniform waiting times. LOOK and C-LOOK are like SCAN and C-SCAN, but the head goes only as far as the last request in each direction, not to the end of the disk. SCAN and C-SCAN suit heavily loaded disks. Solid-state drives (SSDs) have no moving parts, so they need little or no seek-based scheduling.

Table 4.6: Disk scheduling algorithms

Algorithm Rule Weakness
FCFS Arrival order Long seeks
SSTF Nearest request first Starvation
SCAN Sweep to the end and back Ends wait longer
C-SCAN Sweep one way, jump back Return trip unused
LOOK / C-LOOK Sweep only to the last request Slightly more logic

Example 4.6: Total head movement

Problem: The head is at cylinder 53. The queue is 98, 183, 37, 122, 14, 124, 65, 67 (cylinders 0 to 199). Find the total head movement for FCFS and SSTF, and for SCAN moving towards 0.

Solution:

  1. FCFS: 53→98→183→37→122→14→124→65→67 = 45 + 85 + 146 + 85 + 108 + 110 + 59 + 2 = 640.
  2. SSTF: 53→65→67→37→14→98→122→124→183 = 12 + 2 + 30 + 23 + 84 + 24 + 2 + 59 = 236.
  3. SCAN toward 0: 53→37→14→0, then 65→67→98→122→124→183 = 53 + 183 = 236.

Result: FCFS 640, SSTF 236 and SCAN 236 cylinders.

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int q[] = {98, 183, 37, 122, 14, 124, 65, 67}, n = 8, head = 53;
    int total = 0, h = head, done[8] = {0};
    for (int i = 0; i < n; i++) { total += abs(q[i] - h); h = q[i]; }
    printf("FCFS total = %d\n", total);
    total = 0; h = head;
    for (int k = 0; k < n; k++) {
        int b = -1;
        for (int i = 0; i < n; i++)
            if (!done[i] && (b < 0 || abs(q[i] - h) < abs(q[b] - h)))
                b = i;
        total += abs(q[b] - h); h = q[b]; done[b] = 1;
    }
    printf("SSTF total = %d\n", total);
    return 0;
}

Output

FCFS total = 640
SSTF total = 236

Common mistakes

  • Forgetting the move to the end of the disk in SCAN, which LOOK leaves out.
  • Thinking that SSTF is always best. It can starve far requests.
  • Applying seek-based scheduling to SSDs, which have no seek time.

Key takeaways

  • A disk has platters, tracks, sectors and cylinders.
  • Access time = seek time + rotational latency + transfer time.
  • SSTF serves the nearest request; SCAN sweeps like an elevator.
  • C-SCAN and C-LOOK give more uniform waiting times.

Practice questions

  1. What is a cylinder?
  2. How does SSTF work?
  3. How does LOOK differ from SCAN?

Answers to the practice questions

  1. The set of tracks at the same arm position on all the surfaces of a disk.
  2. It serves the pending request closest to the current head position.
  3. LOOK reverses at the last request in each direction instead of going to the end of the disk.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit IV