How the Scheduler Works in Xv6

Here’s an ELI5 (Explain Like I’m 5) version of what the scheduler() function in xv6 is doing: What is a scheduler? Imagine a classroom with a bunch of students (processes) but only one teacher (CPU). The teacher can only help one student at a time, so she needs a way to pick which student to help next. That’s what the scheduler does — it chooses which process gets to use the CPU next. ...

August 5, 2025 · 4 min

Fix High CPU Usage in Xv6

When you run MIT’s xv6 in QEMU, you’ll often see 100% CPU usage on one of the CPU threads, even when the system is doing absolutely nothing. I first noticed this when I could hear the normally super-quiet fan of my PC spinning. But why? The Problem: Busy-Waiting in the Scheduler xv6 implements a simple process scheduler in proc.c. Here’s a simplified view: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void scheduler(void) { for(;;){ acquire(&ptable.lock); for(p in process table){ if(p->state == RUNNABLE){ run(p); } } release(&ptable.lock); } } This loop runs forever. Even when no processes are runnable, the scheduler keeps spinning, checking over and over, eating CPU cycles. ...

August 5, 2025 · 3 min