CPU Scheduling

CPU Scheduling Algorithms Explained

How an operating system decides which process runs next, the main CPU scheduling algorithms, and how each is judged on waiting time and fairness.

On this page
  1. What CPU scheduling does
  2. When the scheduler makes a choice
  3. Switching has a cost
  4. How schedulers are judged
  5. The main algorithms
  6. Trade-offs to keep in mind
  7. How real systems use these ideas
  8. Where to start

What CPU scheduling does

A computer usually has more programs that want to run than it has processor cores to run them. The operating system handles this with a CPU scheduler, the part of the system that decides which process gets the processor next and how long it can keep it. A process is a running program together with the state the system keeps for it.

Processes that are ready to run wait in a list called the ready queue. When the processor is free, the scheduler picks one process from that queue, and the system switches the CPU over to it.

When the scheduler makes a choice

A decision is needed at certain moments. The most common ones are:

  • the running process finishes,
  • it has to wait for something, such as a disk read or network data,
  • its time slice runs out, in systems that use time slicing,
  • a waiting process becomes ready, for example when its input arrives, and the scheduler decides whether it should run now.

Some schedulers let the running process keep the CPU until it stops or waits. These are called non-preemptive. Others can take the CPU away from a running process when a more suitable one is ready. These are called preemptive. Preemption can make a system more responsive, but it also means more switches.

Switching has a cost

Switching from one process to another is called a context switch. The system saves the state of the process that was running and loads the state of the next one. That work takes time, and during it the CPU runs no program code for the user. This is why schedulers do not switch on every small event, and why the length of a time slice matters.

How schedulers are judged

Several measures come up often:

  • Turnaround time is the time from when a process arrives until it finishes.
  • Waiting time is the time a process spends in the ready queue. In a simple model it equals turnaround time minus the time the process actually needs on the CPU, called its burst.
  • Response time is the time from arrival until the process first gets the CPU. It matters most for interactive programs, where people notice delay.
  • CPU utilization and throughput describe how busy the processor stays and how many processes finish in a given time.

No algorithm does best on all of these at once. A scheduler that finishes short jobs quickly may leave long jobs waiting, and one that treats every process evenly may take longer to finish any single one. That trade-off is why many different algorithms exist.

The main algorithms

First come, first served

Processes are served in the order they arrive, from a first-in, first-out queue. It is simple, but a long process at the front can hold up many short ones behind it. This delay is called the convoy effect.

Shortest job first

The scheduler picks the process with the shortest next CPU burst. For a fixed set of jobs whose lengths are known, the non-preemptive form gives the lowest average waiting time of any non-preemptive schedule. The catch is that the next burst length is not known in advance, so systems estimate it from past behavior. The preemptive form is called shortest remaining time first. Long jobs can wait a long time if short ones keep arriving.

Priority scheduling

Each process has a priority, and the scheduler runs the highest-priority ready process. Priority scheduling can be preemptive or non-preemptive. A priority queue is often built on a binary heap, the same structure used in the heapsort algorithm, so the most urgent process can be found quickly. The main risk is starvation, where low-priority processes wait for a very long time. Aging is the usual fix: a process’s priority rises the longer it waits.

Round robin

Each ready process gets a fixed time slice, sometimes called a quantum. When the slice ends, the process goes to the back of the ready queue. Every process gets a turn, and response times stay reasonable for interactive work. Slices are often tens of milliseconds. A slice that is too short adds so many context switches that the system spends much of its time switching. A very long slice makes round robin behave much like first come, first served.

Multilevel queues and multilevel feedback queues

A multilevel queue splits the ready processes into separate queues, for example one for interactive programs and one for background batch work. Each queue has its own algorithm, and processes usually stay in their assigned queue.

A multilevel feedback queue lets processes move between queues based on how they behave. A new process starts at the top. A process that uses its whole time slice may be moved down, while one that often waits for input can stay high. Many designs also periodically move processes back to the top to prevent starvation. These are the most flexible of the common schemes, and also the hardest to tune.

Trade-offs to keep in mind

Three choices come up again and again:

  • Short versus long jobs. Favoring short jobs lowers average waiting time, but it can starve long ones.
  • Fairness versus responsiveness. Round robin spreads CPU time evenly, but some processes finish later than they would under a simpler rule.
  • Overhead versus control. Shorter slices and more queues give finer control, but they cost more switching and more complexity.

How real systems use these ideas

Modern operating systems usually combine several of these ideas, such as priorities together with time slices. Their policies also change over time. The details differ between operating systems and between versions, so check the documentation for the system you use.

Where to start

Learn first come, first served and round robin first. They show the basic trade-off between simplicity and fairness. Shortest job first and priority scheduling come next, since they show how favoring some processes can starve others. Working through a small example on a timeline makes the differences clearer than definitions alone.

Keep exploring

Guides from other topics

All topics →