Reading — step 1 of 5
Read
The Timer & Preemption
Here is the most important interrupt in the entire operating system, and it comes from the most boring device: a chip that does nothing but tick. The timer interrupt is what makes multitasking involuntary — the mechanism that lets the kernel wrest the CPU away from a program that would otherwise run forever. Every scheduling decision this course built (round-robin, MLFQ) is enforced by this tick. Without it, a single while(1) freezes the machine. With it, thirty programs share one core smoothly.
Why preemption needs hardware
Recall the scheduling lessons' uncomfortable dependency: the scheduler can only run when control reaches the kernel. If a program never makes a syscall and never blocks — a pure compute loop — how does the kernel ever get the CPU back to run the scheduler? It can't, from software alone. Cooperative multitasking (pre-1995 Mac/Windows) had exactly this flaw: one misbehaving program froze everything, because the kernel politely waited for a yield that never came.
The timer breaks the deadlock. Program a hardware chip to raise an interrupt every few milliseconds, and no matter what the running program is doing, the CPU vectors into the kernel (the Interrupts lesson's machinery) on every tick. Now the scheduler runs on a guaranteed schedule, checks "has this process used its quantum?", and preempts if so (the context-switch lesson does the actual swap). Preemptive multitasking is just: a periodic interrupt that runs the scheduler. That's the whole idea, and it's why the timer is the heartbeat of the system.
The PIT: a divider on a fixed clock
The classic timer is the 8253/8254 Programmable Interval Timer. It's fed a fixed input clock — 1,193,182 Hz (a gloriously arbitrary number inherited from 1981 NTSC television crystal economics) — and you can't change that. What you can set is a 16-bit divisor: the PIT counts down from the divisor to zero at the input rate, fires an interrupt (IRQ 0), reloads, repeats. So:
interrupt_frequency = 1193182 / divisor
Want 1000 Hz (a tick every millisecond, a common choice)? divisor = 1193182 / 1000 ≈ 1193. But the divisor must be an integer — 1193, not 1193.182 — so the achieved frequency is 1193182 / 1193 ≈ 1000.15 Hz, very slightly off. This tiny error is real and cumulative, which is why serious timekeeping cross-checks against better clocks (the RTC, later the HPET and TSC). Your exercise computes exactly this: target → divisor (rounded, clamped to 16 bits) → achieved frequency.
The divisor is 16-bit, so the slowest the PIT can tick is 1193182 / 65535 ≈ 18.2 Hz — which is exactly the legendary "18.2 times per second" default the BIOS leaves it at (divisor 0, interpreted as 65536). Ask for 1 Hz and you can't have it; the divisor clamps at 65535. That constraint is in your exercise too.
From ticks to time
Once the timer fires at a known rate, the kernel keeps a tick counter — increment it in the IRQ 0 handler, and you have a clock. Uptime, timeouts, sleep(), scheduler quanta, timestamp of every event: all counted in ticks and converted to real time by the achieved frequency:
milliseconds = ticks * 1000 / achieved_hz
A 1000 Hz timer makes this trivial (1 tick ≈ 1 ms); the tick counter is roughly the millisecond clock. This is the humble foundation of every time-related feature in the OS — the scheduler asking "quantum expired?", sleep(5) computing a wakeup tick, the access-log timestamps from the http-server course. It all starts with a counter incremented on a hardware heartbeat.
Your exercise: PIT Timer Configuration
FREQ hz → print the integer divisor (rounded, clamped 1..65535) and the achieved frequency; TICKS n → print elapsed milliseconds at the current rate. The graded subtleties are the real hardware ones: integer divisor rounding making achieved ≠ requested, the 65535 clamp for very low frequencies (the ~18 Hz floor), and the tick→ms conversion using the achieved rate, not the requested one. It's arithmetic — but it's the arithmetic every kernel does at boot to start its own heartbeat, and getting it exactly right is the difference between a clock that drifts and one that doesn't.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…