CPU Scheduling
- In Turkish
- CPU Zamanlama
- Pronunciation
- see-pee-YOO SKEJ-oo-ling or SHED-yoo-ling
In short
CPU scheduling is how an operating system decides which ready process or thread runs on each CPU core next, and for how long, so the processor is shared fairly.
What is CPU scheduling?
CPU scheduling is the job of the scheduler, a part of the kernel that chooses which of the many ready threads or processes gets to run on each CPU core at any moment. A typical computer has far more runnable tasks than cores, so the scheduler switches between them many times per second, creating the illusion that everything runs at once.
Most modern systems use preemptive scheduling: each task gets a short time slice, and when it runs out or a higher-priority task becomes ready, the kernel interrupts the task, saves its state, and loads another one in a step called a context switch. Classic algorithms include first-come first-served (FCFS), shortest job first (SJF), round robin, and priority scheduling, and real kernels combine these ideas. Linux, for example, used the Completely Fair Scheduler for many years and switched to the EEVDF scheduler in 2023, both aiming to give each task a fair share of CPU time.
A good analogy is a single checkout lane where the cashier serves each customer for one minute before moving on to the next person in line. Everyone makes steady progress, and a shopper with one item is never stuck behind a full cart for long. Schedulers balance goals that pull in different directions: high throughput, low latency for interactive apps, fairness, and energy efficiency.
CPU scheduling is different from job scheduling tools such as cron. A cron job decides what time a task should start, often hours apart, while the CPU scheduler decides which already-running task uses the processor over the next few milliseconds. Your code can create threads, but the scheduler ultimately decides when each one actually runs.
Key takeaways
- The scheduler is the part of the kernel that picks which task runs on each CPU core.
- Preemptive scheduling gives tasks short time slices and interrupts them when needed.
- Switching between tasks requires a context switch, which has a small performance cost.
- Classic algorithms include first-come first-served, shortest job first, round robin, and priority scheduling.
- Schedulers balance throughput, responsiveness, fairness, and power use.
Example
# Start a CPU-heavy task with lower priority (a higher "nice" value)
nice -n 10 ./build.sh &
# Lower the priority of an existing process by its PID
renice -n 15 -p 12345
# Show the top CPU users along with their nice values
ps -eo pid,ni,pcpu,comm --sort=-pcpu | head -n 5Readers ask
What is a context switch?
A context switch is when the CPU stops running one thread or process, saves its registers and state, and loads the saved state of another. It lets many tasks share a core, but each switch costs time, so too many switches can hurt performance.
What is the difference between preemptive and non-preemptive scheduling?
In preemptive scheduling the operating system can interrupt a running task to give the CPU to another one. In non-preemptive, or cooperative, scheduling a task keeps the CPU until it finishes or voluntarily gives it up.
What is round robin scheduling?
Round robin gives each ready task the same small time slice in turn, cycling through the queue repeatedly. It is simple and fair, which makes it a common building block in real schedulers.
See also
- ProcessOperating Systems, p. 23A process is a running instance of a program, with its own memory space, resources, and at least one thread of execution managed by the operating system.
- ThreadOperating Systems, p. 33A thread is the smallest unit of execution an operating system can schedule, running inside a process and sharing that process's memory with other threads.
- KernelOperating Systems, p. 17A kernel is the core part of an operating system that manages the CPU, memory, and hardware devices and controls how programs get access to those resources.
- ConcurrencyProgramming Fundamentals, p. 12Concurrency is a program's ability to make progress on several tasks in overlapping time periods, such as serving many users at once rather than one at a time.
- LatencyNetworking, p. 14Latency is the delay between sending a request and the start of a response, usually measured in milliseconds, and it shapes how responsive an app feels.
- Cron JobBackend & APIs, p. 9A cron job is a command or script that runs automatically on a repeating schedule, such as every night at 2 a.m., defined by a five-field cron expression.
Spotted a mistake or something missing on this page?Suggest an edit