CPU Scheduling

CPU scheduling

Imagine one busy kitchen counter with a single chef, and a dozen orders that all want the chef's hands. Only one order can be worked on at any instant, so somebody has to decide who gets the chef next, and for how long, and what happens when an order has to wait for the oven. CPU scheduling is exactly that decision-making for a computer: there is usually one CPU core (or a handful), but many processes and threads that all want to run, and the operating system must keep choosing which one runs next.

Here is how it actually works. The OS keeps a set of processes that are ready to run in a ready queue. When the running process can no longer use the CPU — because it finished, or it asked to read from disk and must wait, or its time slice ran out — the scheduler (the policy) picks one of the ready processes, and the dispatcher (the mechanism) hands the CPU over to it by loading its saved state and jumping into its code. This happens thousands of times a second, far faster than a person notices, which is why a single core seems to run your browser, music, and editor all at once.

Why it matters: the scheduling policy quietly shapes how the whole machine feels. A good policy keeps the CPU busy, finishes work quickly, and still lets you type without lag; a bad one can let one heavy job hog everything while everyone else waits. There is no single best policy — fast response for interactive apps and high throughput for batch jobs pull in different directions, so every real OS picks a compromise tuned to its users.

Three jobs arrive at the same moment, each needing 4 ms of CPU. On one core they cannot all run at once; the scheduler runs them in some order — say A then B then C — so A finishes at 4 ms, B at 8 ms, and C at 12 ms. Choosing a different order, or interleaving them, changes how long each one waits.

With one CPU and several ready jobs, the only lever the OS has is order and timing — that is scheduling.

Scheduling does not make any single job run faster — the CPU does the same work either way. It only changes the ORDER and timing, which is what affects waiting and responsiveness.

Also called
process scheduling處理器排程