Multitasking in an operating system means running more processes than there are CPU cores, so that many programs appear to run at the same time. The operating system achieves this by switching the processor rapidly between runnable tasks, saving and restoring each task's state, and deciding which task runs next according to a scheduling policy.
The key idea is that a single CPU core executes only one instruction stream at any instant. What creates the illusion of simultaneity is that the switches happen far faster than a human can perceive, typically many times per second, and that tasks spend much of their time waiting for input, disk, or the network rather than needing the processor. On a machine with several cores, true parallelism is possible as well, but multitasking as a concept predates multicore hardware and still governs how the system shares cores among far more tasks than it has.
Background: From Batch Jobs to Interactive Systems
Early computers ran one job at a time, and operators loaded work in batches. The processor sat idle whenever a job waited for a tape drive or a card reader, which was most of the time. The first step toward multitasking was multiprogramming: keeping several jobs in memory so that when one blocked on input/output, another could use the CPU. This raised utilization dramatically without any need for fast switching, because a switch happened only when a job could not proceed.
Time-sharing came next and changed the goal. Instead of maximizing throughput, it aimed to give many interactive users the impression of a private machine. That required preemption, the ability of the system to interrupt a running program and take the CPU away, plus a timer that fires periodically so no single program can monopolize the processor. Modern desktop and mobile systems inherit both ideas: they preempt aggressively for responsiveness and also try to keep the hardware busy.
How Context Switching Works
The mechanism behind multitasking is the context switch. Each task has a context: the values of the CPU registers, the program counter, the stack pointer, and the memory mapping it uses. To switch tasks, the kernel saves the current context, selects a new task, and restores that task's context so execution resumes exactly where it left off.
- The running task is interrupted, either by a timer tick, by a system call, or because it blocks waiting for something.
- The kernel saves the task's register state and other hardware context into its process control block.
- The scheduler picks the next task to run from the run queue.
- The kernel restores the chosen task's saved state and, if the task uses a separate address space, switches the memory mapping.
- Execution resumes in the new task, which has no awareness that it was paused.
Context switches are not free. Saving and restoring registers takes little time, but switching address spaces can flush translation caches that map virtual addresses to physical memory, and the new task may then run more slowly until those caches refill. This is why systems try to avoid unnecessary switches and why very small time slices can hurt overall performance even as they improve responsiveness.
Preemptive vs Cooperative Multitasking
In preemptive multitasking, the operating system decides when to take the CPU away, usually on a timer interrupt. A misbehaving program that loops forever cannot lock up the machine, because the scheduler will still run other tasks. Nearly all general-purpose systems today are preemptive.
In cooperative multitasking, a task keeps the CPU until it voluntarily yields, for example by waiting on input. This is simpler and cheaper, and it was common in early graphical environments and is still used inside some runtimes where tasks are lightweight. Its weakness is that one task that fails to yield stalls everything else. The distinction matters to users because it explains why a frozen program on a modern system can usually still be killed from a task manager, while on a cooperative system the whole environment could hang.
Scheduling: Choosing What Runs Next
The scheduler is the component that decides which runnable task gets the processor. Different goals lead to different policies, and most real schedulers blend several ideas.
- Round robin: each task gets a fixed time slice in turn. Simple and fair, but treats all tasks alike regardless of urgency.
- Priority scheduling: higher-priority tasks run first. Risk: low-priority tasks can starve if high-priority work keeps arriving, so systems often add aging to raise the priority of long-waiting tasks.
- Multilevel feedback queues: tasks move between queues based on behavior. Interactive tasks that block often stay in high-priority queues, while CPU-bound tasks drift to lower ones.
- Fair-share and proportional scheduling: groups of tasks, such as users or applications, receive a defined share of CPU time rather than individual tasks competing equally.
- Real-time policies: used where deadlines matter, giving strict priority to tasks that must respond within a bounded time.
Desktop systems generally favor responsiveness: a task that has just become runnable after waiting for input is often boosted so the interface feels immediate. Server systems may favor throughput, accepting slightly longer waits in exchange for fewer switches and better cache use.
Threads, Processes, and What Actually Gets Scheduled
A process is a program in execution with its own address space; a thread is a unit of execution within a process that shares that address space with sibling threads. Most modern systems schedule threads, not processes, because threads are the finer-grained unit. Two threads of the same process can run on two cores at once, and switching between threads of one process is cheaper than switching between processes because the memory mapping does not change.
This distinction explains a common experience: an application can feel responsive while one of its threads does heavy computation, as long as the interface runs on a different thread. It also explains why a single-threaded program cannot use more than one core no matter how many the machine has. The scheduler can only distribute work that has been split into separately schedulable units.
Common Problems and Misconceptions
A frequent misconception is that multitasking makes a computer do more work per second. It does not increase the capacity of a single core; it shares that capacity. When several CPU-hungry tasks compete for one core, each finishes later than it would alone, though total throughput may stay similar. The benefit is overlap of waiting time, not multiplication of computing power.
Other recurring issues include:
- Thrashing: too many active tasks compete for limited physical memory, so the system spends its time moving pages to and from disk instead of doing useful work. Adding memory or reducing the number of active programs is the remedy.
- Priority inversion: a high-priority task waits on a resource held by a low-priority task, which is itself prevented from running by a medium-priority task. Systems mitigate this with priority inheritance.
- Starvation: a task never gets scheduled because others always outrank it. Aging policies exist to prevent it.
- Overhead: excessive switching consumes CPU time that could go to real work, which is why time slices are a compromise rather than something made as small as possible.
On the human side, the word multitasking also describes people switching between tasks. Research on human attention generally finds that switching carries a cost and that divided attention reduces performance on demanding tasks, but that is a separate subject from how an operating system schedules threads.
Frequently Asked Questions
Does multitasking require multiple CPU cores?
No. Multitasking existed long before multicore processors and works on a single core by switching rapidly between tasks. Multiple cores allow genuine simultaneous execution, but the scheduling machinery is the same idea.
Why does my computer slow down when many programs are open?
Usually because the programs compete for limited memory or for a small number of cores. When memory runs short, the system may spend time swapping data to disk, which is far slower than keeping it in memory.
What is the difference between multitasking and multiprocessing?
Multitasking is sharing processor time among tasks, whether on one core or several. Multiprocessing generally refers to using more than one processor or core at the same time, or to a system with multiple processors.
Can a frozen program freeze the whole computer?
On a preemptive system, usually not, because the scheduler can still run other tasks and the user can terminate the stuck program. Severe memory exhaustion or a fault in the kernel itself can still affect the whole machine.
Is a smaller time slice always better?
No. Smaller slices improve responsiveness but increase switching overhead and can degrade cache performance. Systems choose a balance, often adjusting behavior for interactive versus background work.
Do mobile phones multitask the same way?
They use the same core principles but manage background work more strictly to save battery and memory. Apps may be suspended or frozen when not visible, then restored later, which is a policy choice layered on top of ordinary scheduling.