C++ / a working model

141 / 163   ·   C11   ·   8 min

Hard Disk Drives

Keep this sentence

This chapter explains how hard disks persist data as a sector array, the platter-track-head geometry, and how seek plus rotational delay dominate access cost. Schedulers reorder requests to raise effective throughput.

In this lesson
  1. Interface and Linear Address Space
  2. Platters, Tracks and Head Mechanics
  3. Seek, Rotation Costs and Request Scheduling
  4. Example
  5. Exercise

Official chapter PDF

Interface and Linear Address Space

The operating system sees the drive as a consecutively numbered collection of fixed-size sectors starting at zero. Multi-sector transfers are common, yet only a single-sector write is guaranteed atomic. Logically adjacent sectors are almost always physically nearby, so sequential streams outperform random jumps.

Platters, Tracks and Head Mechanics

Data is magnetically recorded on both sides of rapidly spinning platters. A spindle motor keeps constant rotation while thousands of concentric tracks occupy each surface. Read-write heads ride on a pivoting arm that must settle exactly over the chosen track before any transfer begins.

Seek, Rotation Costs and Request Scheduling

Head motion comprises acceleration, coasting, deceleration and a lengthy settling interval. Once on the right track the platter must still rotate the desired sector under the head. Schedulers such as SSTF or SCAN sort outstanding requests by track number, shrinking total seek distance and raising sustained bandwidth.

Pitfalls

  • Treating multi-sector writes as atomic and then suffering torn writes after power loss.
  • Omitting head-settling time when estimating seeks, which under-states latency.
  • Expecting random workloads to achieve sequential transfer rates.

Run an example

Minimum C11 · complete program · Download .c

#include <stdio.h>

int main(void) {
    const double rpm = 7200.0;
    const double full_rotation_ms = 60000.0 / rpm;
    const double avg_seek_ms = 9.0;
    const double settle_ms = 1.0;
    printf("Hard Disk Drive Access Time Simulator\n");
    printf("=====================================\n");
    printf("RPM: %.0f, Full rotation: %.2f ms\n", rpm, full_rotation_ms);
    printf("Average seek: %.1f ms, Settle: %.1f ms\n\n", avg_seek_ms, settle_ms);
    double random_access = avg_seek_ms + settle_ms + 0.5 * full_rotation_ms;
    printf("Typical random I/O time: %.2f ms\n", random_access);
    printf("Sequential transfer much faster due to no extra seeks/rotations.\n");
    int current_track = 0;
    int target_track = 100;
    double seek_time = 0.08 * (target_track - current_track) + settle_ms;
    double rot_delay = 0.3 * full_rotation_ms;
    double total = seek_time + rot_delay;
    printf("\nExample request: from track %d to %d\n", current_track, target_track);
    printf("Seek time: %.2f ms, Rotational delay: %.2f ms, Total: %.2f ms\n", seek_time, rot_delay, total);
    return 0;
}

Compile locally

gcc -std=c11 -Wall -Wextra -Wpedantic -Werror ostep-37-hard-disks.c -o example && ./example

Expected result

Hard Disk Drive Access Time Simulator
=====================================
RPM: 7200, Full rotation: 8.33 ms
Average seek: 9.0 ms, Settle: 1.0 ms

Typical random I/O time: 14.17 ms
Sequential transfer much faster due to no extra seeks/rotations.

Example request: from track 0 to 100
Seek time: 9.00 ms, Rotational delay: 2.50 ms, Total: 11.50 ms

CHECK YOUR UNDERSTANDING

Close the answer. Explain it.

A disk spins at 15000 RPM. What is the worst-case rotational delay in milliseconds?

Show a reference answer

4 ms, because one full revolution takes 60/15000 = 0.004 seconds.

Check the sources

Drafts and official chapters change. The version mark is only the example’s minimum.

Back to the catalog