What is Multitasking?
Right now, your computer is probably running dozens of programs simultaneously — a browser, a music player, a chat app, antivirus software, and the operating system itself. How does the CPU handle all of this when it can only execute one instruction at a time?
The answer is multitasking: the OS rapidly switches the CPU between different programs, giving each one a tiny slice of time in turn. This happens so fast — thousands of times per second — that it looks like everything is running simultaneously.
On a computer with a single-core CPU, only one program is actually executing at any given instant. Multitasking creates the illusion of simultaneous execution by switching rapidly. Multi-core CPUs can run programs truly in parallel on different cores.
Processes
Each running program is called a process. When you open a browser, the OS creates a process for it. Every process has its own:
- Section of RAM to work in
- Program instructions
- Current state (what it is doing right now)
The OS keeps track of all active processes in a process table.
CPU Scheduling
The CPU scheduler is the part of the OS that decides which process runs next and for how long. A small unit of time given to a process is called a time slice (typically a few milliseconds).
Common scheduling strategies include:
| Strategy | How it Works |
|---|---|
| Round Robin | Each process gets an equal time slice in turn |
| Priority scheduling | Higher-priority processes get more CPU time |
| Shortest Job First | Shortest tasks are completed first |
Priority scheduling is why your music keeps playing smoothly even when your computer is busy — the audio process is given a higher priority to ensure it always gets CPU time before less urgent tasks.
Memory Management
When multiple processes run simultaneously, the OS must ensure they do not interfere with each other's memory. Each process is given its own isolated area of RAM. If one program crashes, it should not corrupt the memory of another.
The OS can also use virtual memory (space on the hard drive used as extra RAM) when RAM is full, though this is much slower.
Context Switching
When the CPU switches from one process to another, it needs to save the current process's state (what it was doing, what values were in its registers) so it can resume later. This save and restore operation is called a context switch. Frequent context switching has a small overhead, but modern CPUs are very efficient at it.
Practice Problems
Explain how multitasking works on a single-core CPU, even though the CPU can only execute one instruction at a time.
Show answer
A computer is running a music player, a file download, and a video game at the same time. Explain why the OS might give the music player a higher scheduling priority than the file download.