Multitasking and Scheduling

Multitasking

Multitasking is the ability of a computer to run multiple programs apparently simultaneously. It is an illusion, a single-core CPU can only execute one instruction at a time. The OS creates the illusion by switching between processes so rapidly that the user cannot perceive the switching.

How It Works

  1. Each running program (process) is given a small time slice of CPU time
  2. The CPU executes instructions for Program A
  3. It then switches to Program B for its time slice
  4. Then Program C, and so on
  5. This cycle repeats so quickly (millions of times per second) that all programs appear to run simultaneously

Example

At any given moment, a computer might appear to be running simultaneously:

  • A word processor
  • A web browser
  • A music player
  • Antivirus software
  • A system clock

In reality, the CPU rapidly time-slices between each.


Scheduling

Scheduling is the OS process that decides which process runs next and for how long.

The Ready Queue

When a process enters the system, it is placed in a ready queue waiting for CPU time. A process leaves the queue when:

  • It completes and exits the CPU
  • Its time slice expires, it moves back to the queue
  • It is blocked by a higher-priority process, it moves back to the queue
New Process → [Ready Queue] → CPU → Complete
                    ▲              │
                    └──────────────┘
               (time slice expired / blocked)

Scheduling Algorithms

There are many different algorithms (e.g., Round Robin, Priority Scheduling, First-Come-First-Served). The details are beyond GCSE scope, you only need to know that they exist and that the OS uses them.


Memory Fragmentation

As the OS loads and unloads programs, memory management creates a side-effect:

  1. Program A is loaded into RAM, occupies blocks at addresses 0-100
  2. Program B is loaded, occupies blocks at 101-150
  3. Program A closes, blocks 0-100 are marked free (not erased)
  4. Program C is loaded, it is larger but fits in the gaps around Program B

Over time, non-contiguous gaps of free memory accumulate across RAM. This is memory fragmentation. The OS must manage these scattered free blocks when fitting new programs in.

Note

Fragmentation is primarily a concern for magnetic HDD storage and for RAM management, not for SSDs (no benefit from defragmentation).


Exam Questions

Q: How does the OS use time-slicing to achieve multitasking?

The OS gives each running program a small amount of processor time called a time slice and rapidly cycles between all open programs. Because this switching happens so fast, it appears to the user that programs are running simultaneously.

Q: What is memory fragmentation?

Memory fragmentation is when small, non-contiguous gaps of free memory are created in RAM as programs of different sizes are opened and closed over time.

Q: What is a ready queue?

A queue of processes waiting for CPU time. A new process enters the queue, receives a time slice, and either completes or returns to the queue to wait for the next slice.


Exam Tip

Multitasking is an illusion, the CPU doesn’t run things simultaneously, it just switches very fast. Saying “the CPU runs all programs at the same time” would be wrong.