Multitasking

On this page 8

Multitasking is the ability of an operating system to keep several tasks in progress at once by sharing processor time, memory and devices among them, so that a browser, a music player and a background backup all appear to run simultaneously. Among the OS fundamentals, it is the capability that turns a single-purpose machine into an interactive, general-purpose computer.

In operating systems, multitasking is an illusion built on fast switching: on one CPU core, only one instruction stream executes at any instant, but the kernel alternates between tasks in slices of a few milliseconds. On multicore hardware, true parallel execution adds to that illusion, and the scheduler must decide both when and where each task runs.

How multitasking shares the processor

Multitasking works by suspending one task and resuming another often enough that every task makes steady progress. The process scheduler chooses the next task to run, and a context switch saves the registers, program counter and memory mapping of the outgoing task before loading those of the incoming one.

The trigger for a switch is usually a hardware timer interrupt. When a task exhausts its time slice, typically between 1 and 100 milliseconds depending on the kernel and workload, the interrupt hands control back to the kernel, which re-evaluates priorities. A switch also happens whenever a task blocks on input or output, for example while waiting for a disk read or a network packet, because an idle CPU would otherwise waste cycles.

The following table summarizes the main events that cause the kernel to change the running task.

EventWhat happensTypical cause
Timer interruptKernel preempts the running taskTime slice expired
Blocking system callTask waits and yields the CPUDisk, network or user input
Higher-priority wakeupWaiting task preempts current oneData arrived for an urgent task
Voluntary yieldTask gives up the CPU earlyExplicit sleep or yield call
Task exitScheduler picks a successorProgram finished or crashed
Events that trigger a task switch

Cooperative and preemptive multitasking

The two classic models differ in who decides when a task gives up the CPU. In cooperative multitasking, each program must yield voluntarily; in preemptive multitasking, the kernel can interrupt any task at any time. Early desktop systems such as Windows 3.x and classic Mac OS used the cooperative model, while every mainstream desktop and server system today is preemptive.

The weakness of the cooperative approach is that one misbehaving program can freeze the whole system simply by never yielding. Preemption removes that dependency on application discipline, at the price of more complex kernel code, because shared data structures must be protected against interruption at any point.

PropertyCooperativePreemptive
Switch decisionApplication yieldsKernel scheduler decides
ResponsivenessDepends on every programGuaranteed by time slices
Fault isolationOne hang can stall allA hung task affects only itself
Kernel complexityLowerHigher, needs locking
Typical use todaySome embedded loops, coroutinesDesktop, server and mobile systems
Comparison of multitasking models

Processes, threads and levels of multitasking

Multitasking operates on several units of work, and the distinction matters for performance and safety. A process owns an isolated address space, while threads inside one process share memory and can be scheduled independently. Switching between threads of the same process is cheaper because the memory mapping and translation lookaside buffer contents often stay valid.

Modern kernels such as Linux, Windows and macOS schedule threads as the basic unit, and processes serve mainly as containers for resources and protection. The table below places the common terms side by side.

FormUnit switchedMemory sharingSwitch cost
Multiprocessing of programsProcessIsolated by virtual memoryHighest
MultithreadingKernel threadShared within processModerate
User-level threadsGreen thread or coroutineSharedLow, no kernel entry
Simultaneous multithreadingHardware thread on one coreShared core resourcesHandled by the CPU
Units and forms of multitasking

Multitasking is not the same as multiprocessing. A single-core machine can multitask through time sharing alone, while a multiprocessor system can run several tasks at exactly the same moment. Most real systems combine both: dozens of cores, each one time-sliced among hundreds of runnable threads.

Costs and limits of switching between tasks

Every context switch has a direct cost of saving and restoring state, usually around one to several microseconds on current hardware, and a larger indirect cost from cold caches and flushed address translations. A task that resumes after another one ran may spend thousands of cycles reloading data it had already fetched.

This creates the central trade-off between responsiveness and throughput. Short time slices make interactive programs feel instant but waste more time on switching; long slices improve batch throughput but make the desktop feel sluggish. Kernels therefore adapt slice lengths dynamically and favor tasks that recently woke from waiting on user input.

Typical symptoms of poorly managed multitasking, and their usual causes, are listed below.

SymptomLikely causeCommon remedy
Laggy user interfaceCPU saturated by background workLower background priority
High system time, low progressExcessive context switchingFewer threads, larger work batches
Constant disk activityThrashing from memory overcommitClose programs or add RAM
One task never runsStarvation by higher prioritiesPriority aging in the scheduler
Corrupted shared dataRace condition between threadsLocks or atomic operations
Multitasking problems and remedies

Multitasking with memory and devices

Sharing the CPU is only half the task, because memory and devices must be shared safely as well. Virtual memory gives each process its own address space, so one program cannot overwrite another, and paging lets the combined working sets of all tasks exceed physical RAM up to a point.

Devices are shared through drivers and kernel queues: the kernel serializes requests to a disk or a network card and wakes the waiting task when the operation completes. Communication between tasks happens through inter-process communication mechanisms such as pipes, sockets and shared memory segments, all mediated or set up by the kernel.

  • Memory protection prevents a crashed task from damaging others.
  • Interrupt-driven I/O lets the CPU serve other tasks during slow device operations.
  • Priority classes let audio, video and input handling stay smooth under load.

Common misconceptions about multitasking

A frequent belief is that more threads always mean more speed. In practice, once runnable threads exceed the number of hardware threads, additional ones mostly add switching overhead and lock contention. Another misconception is that an idle-looking program consumes nothing; background tasks still wake periodically, which affects battery life on laptops and phones.

Mobile platforms also show that multitasking policy is a design choice. iOS and Android suspend or freeze background applications aggressively to save energy, granting execution time only for defined purposes such as audio playback, location updates or brief completion work. The kernels are fully preemptive, yet the visible multitasking is deliberately restricted.

Stripped to the essentials

A computer processor can really do only one thing at a time per core. Multitasking is the trick that hides this. The operating system gives each program a tiny turn, a few thousandths of a second, then quickly moves to the next one. Because the turns are so short, everything seems to happen at once.

Think of a chess master playing twenty opponents. The master moves from board to board, making one move at each, and every game keeps going. The operating system is that master, and it also makes sure no single program can hog all the attention.

Where it is applied

A video editor on a laptop with eight CPU cores exports a 4K project while joining a video call and syncing files to cloud storage. The export spawns sixteen worker threads, the call application runs audio and video threads, and the sync client checks for changes in the background.

The preemptive scheduler gives the call threads higher priority, so the editor’s voice stays clear even with every core busy, while the export threads absorb the remaining capacity. When the editor notices the export slowing slightly, it is because the scheduler is time-slicing cores among more runnable threads than there are hardware threads. The outcome is a smooth call and a completed export, finishing a few minutes later than it would on an idle machine.