Preemptive multitasking

On this page 7

Preemptive multitasking is a method of running several programs concurrently in which the operating system kernel decides when a task loses the processor, interrupting it without its cooperation and switching to another ready task. In the field of Processes & scheduling, it is the model used by every mainstream desktop, server and mobile operating system, including Windows NT and its successors, Linux, macOS and the BSD family.

The defining property is involuntary removal from the CPU. A task does not need to call a yield function or wait for input before another task can run; a hardware timer interrupt or a higher-priority event returns control to the kernel, which may then select a different thread. This guarantees that no single program can monopolize the processor, which is the main reason modern systems stay responsive even when an application hangs.

How the kernel takes the processor back

The kernel regains control through interrupts that the running task cannot block. The most important source is a programmable hardware timer, such as the local APIC timer on x86 processors or the generic timer on ARM, which fires periodically or at a precisely programmed deadline. When the interrupt arrives, the processor switches to kernel mode and runs the scheduler logic.

The scheduler then checks whether the current thread has exhausted its time slice or whether a higher-priority thread has become ready. If either is true, the kernel performs a context switch: it saves the registers, program counter and stack pointer of the current thread, loads the saved state of the chosen thread and returns to user mode. The sequence of events is summarized below.

StepWhat happensWho performs it
Timer firesHardware raises an interrupt at the end of the quantumTimer hardware
Mode switchCPU enters kernel mode and saves minimal stateProcessor
AccountingKernel charges elapsed time to the running threadKernel
Scheduling decisionScheduler picks the next thread from the run queueKernel scheduler
Context switchRegisters and address space of the new thread are loadedKernel
ResumeNew thread continues in user modeProcessor
Steps of a timer-driven preemption

Preemption is not caused only by the timer. When a device interrupt wakes a blocked thread with a higher priority, for example a thread waiting for keyboard input, the kernel can preempt the current thread immediately instead of waiting for the slice to expire. This is why interactive latency stays low even under heavy background load.

Key parameters that shape preemptive behavior

The behavior of a preemptive system depends on a handful of tunable scheduler parameters. Their values differ between operating systems and workloads, and the ranges below are typical rather than fixed.

ParameterTypical rangeWhat it affects
Time slice lengthAbout 1 to 100 msResponsiveness versus switching overhead
Timer frequency100 to 1000 Hz, or ticklessGranularity of scheduling decisions
Context switch costRoughly 1 to 10 microsecondsShare of CPU time lost to switching
Priority levels32 in Windows, 140 in LinuxHow finely urgency can be expressed
Kernel preemption modelNone, voluntary or fullLatency inside system calls
Typical parameters of preemptive schedulers

Many current kernels are tickless: instead of interrupting at a fixed rate, they program the timer for the next needed event. This reduces power consumption on idle cores while keeping preemption precise when work is present. A separate question is whether the kernel itself is preemptible. Linux offers configurations ranging from no forced kernel preemption to fully preemptible kernels and the PREEMPT_RT patch set for real-time use.

Preemptive compared with cooperative multitasking

The clearest way to understand preemptive multitasking is to contrast it with cooperative multitasking, in which a task keeps the CPU until it voluntarily yields. Classic Mac OS before version X and 16-bit Windows 3.x used the cooperative model, and one misbehaving program could freeze the entire system.

AspectPreemptive multitaskingCooperative multitasking
Who triggers a switchKernel, via interruptsRunning task, via yield
Effect of an infinite loopOther tasks keep runningWhole system stalls
Synchronization needsLocks required almost everywhereFewer races between yield points
Latency guaranteesBounded by slice and priorityDepends on program discipline
Typical use todayGeneral-purpose operating systemsCoroutines, async runtimes, small firmware
Preemptive and cooperative multitasking side by side

The trade-off is that preemption makes concurrency bugs more likely. Because a thread can be interrupted between any two instructions, shared data must be protected with mutexes, atomic operations or other synchronization. Cooperative designs avoid some of these races but shift the burden of fairness onto every program author.

Scheduling policies built on preemption

Preemption is a mechanism, while the scheduling policy decides which thread runs next. Several well-known policies depend on the ability to interrupt a running thread. Round-robin scheduling rotates threads of equal priority through fixed quanta, priority scheduling always runs the highest-priority ready thread, and a multilevel feedback queue moves threads between queues based on observed behavior so that I/O-bound processes receive quick service.

Linux used the Completely Fair Scheduler for many years and replaced it with the EEVDF scheduler in version 6.6, both of which track virtual runtime rather than fixed slices. Windows uses a priority-based preemptive scheduler with dynamic boosts for foreground and I/O-completing threads. Real-time classes such as SCHED_FIFO and earliest deadline first variants preempt ordinary threads unconditionally.

Limitations, risks and common misconceptions

Preemptive multitasking solves fairness and responsiveness but introduces its own failure modes. Experienced engineers watch for the following symptoms, which are summarized with their usual causes and remedies.

SymptomLikely causeTypical remedy
Data corruption under loadRace condition on shared stateAdd locking or atomic operations
High-priority task stallsPriority inversion on a lockPriority inheritance protocols
Low-priority task never runsStarvation by busy threadsAging or fair-share scheduling
Throughput drops with many threadsExcessive context switchingFewer threads or longer slices
Audio or video glitchesNon-preemptible kernel sectionsPreemptible or real-time kernel
Common problems in preemptive systems
  • Preemption is not parallelism: on a single core, tasks still run one at a time, only interleaved.
  • More threads do not mean more speed: beyond the number of cores, extra threads mostly add switching cost.
  • Disabling interrupts is not a general lock: it fails on multiprocessor systems and harms latency.

The short version

Imagine a teacher who gives each student a few seconds to speak and then politely moves on to the next one, whether or not the first student has finished. Nobody can talk forever, and everyone gets a turn.

Preemptive multitasking works the same way inside a computer. The operating system gives each program a very short turn on the processor and then takes it back. Because the turns are so short, all programs seem to run at once, and a frozen program cannot freeze the whole computer.

Where it makes a difference

A data analyst runs a long statistical calculation on a four-core laptop. The job starts eight worker threads that each try to use the CPU at full speed for twenty minutes. At the same time, the analyst wants to answer messages, browse documentation and join a video call.

Because the operating system uses preemptive multitasking, the timer interrupt returns control to the kernel every few milliseconds. The scheduler gives the interactive threads of the video call a priority boost when their audio buffers need refilling, so they preempt the calculation threads immediately. The call stays smooth, the mouse pointer never freezes and the calculation finishes only slightly later than it would on an idle machine.

On a cooperative system from the early 1990s, the same calculation would have held the processor until it yielded, and the entire desktop would have appeared frozen for long periods.