Learning path

Full curriculum

Full curriculum

Unit content

Timer interrupts and preemptive multitasking

Preemptive multitasking requires a way for the operating system to regain control even when the running program never voluntarily yields the processor.

A hardware timer can be configured to generate periodic interrupts. Because those interrupts arrive independently of the current user program, the kernel receives regular opportunities to account for processor time and reconsider which task should run.

When a timer interrupt occurs, the processor preserves execution state and transfers control to the kernel's interrupt path. The scheduler can then decide whether the current task should continue or whether another runnable task should take its place.

If another task is selected, the kernel saves the current task's remaining context and restores the context of the selected task before returning from privileged execution.

running task
    ↓ timer interrupt
kernel scheduler
    ↓
same task or different task

The timer supplies the periodic hardware event; scheduling policy decides what to do with that opportunity. Together they let the operating system preempt code that would otherwise continue running indefinitely.