Why This Matters
Your computer runs dozens of programs simultaneously, but most machines have only a handful of CPU cores. The OS must constantly decide which process gets to run next and for how long. This is process scheduling, and it directly affects how responsive your system feels.
A bad scheduler makes your music stutter while compiling code. A good scheduler keeps everything smooth by giving each process a fair share of CPU time. Understanding scheduling explains why your system slows down under load, how priority works, and why real-time systems use different algorithms than your laptop.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Round-robin scheduling: each process gets a time slice, then returns to the back of the queue.
Code Example
// Simulate a round-robin scheduler
function roundRobinScheduler(processes, quantum) {
// Each process: { name, burstTime }
const queue = processes.map(p => ({ ...p, remaining: p.burstTime }));
const timeline = [];
let time = 0;
while (queue.length > 0) {
const process = queue.shift(); // take from front
const runTime = Math.min(quantum, process.remaining);
timeline.push({
process: process.name,
start: time,
end: time + runTime
});
time += runTime;
process.remaining -= runTime;
if (process.remaining > 0) {
queue.push(process); // back to the end
} else {
console.log(process.name + " finished at time " + time);
}
}
return timeline;
}
const processes = [
{ name: "P1", burstTime: 10 },
{ name: "P2", burstTime: 4 },
{ name: "P3", burstTime: 6 }
];
const result = roundRobinScheduler(processes, 4);
console.log("Timeline:", JSON.stringify(result));Interactive Experiment
Try these exercises:
- Modify the quantum value. What happens with a very small quantum (1)? What about a very large quantum (100)? Think about the tradeoff between fairness and overhead.
- Add a fourth process with a burst time of 2. Trace through the scheduling by hand before running the code. Does it finish earlier than the others?
- Modify the scheduler to implement FCFS (First Come First Served) by setting the quantum to infinity (or a very large number).
Quick Quiz
Coding Challenge
Write a function called `schedule` that takes an array of processes (each with a `name` and `burstTime`) and a `quantum`. Return an array of strings showing the execution order. Each string should be the process name, repeated for each quantum it runs. For example, with quantum 2: P1(burst 5), P2(burst 3) produces ['P1','P2','P1','P2','P1'].
Real-World Usage
Scheduling algorithms are everywhere:
- Linux CFS (Completely Fair Scheduler) uses a red-black tree to track virtual runtimes, ensuring each process gets its fair share of CPU proportional to its weight.
- Real-time systems (medical devices, robotics) use priority scheduling with hard deadlines. Missing a deadline can be catastrophic.
- Cloud computing uses scheduling to allocate CPU time across virtual machines. AWS and GCP schedulers balance fairness, performance, and cost.
- JavaScript runtime uses the event loop as a cooperative scheduler: microtasks (promises) run before macrotasks (setTimeout), creating a priority system.