Why Does the Kernel Enter an Idle Loop and How Does It Wake Up?
When no runnable processes are available, the kernel switches to an idle loop that repeatedly executes the HLT instruction until an interrupt occurs, after which it resumes normal scheduling by switching from the idle process to a ready task.
Reading font
The kernel's primary responsibility is process scheduling; when a process blocks, the kernel schedules another runnable process.
If no processes are ready to run, the kernel enters an idle state. The idle logic continuously loops, executing the hlt assembly instruction to halt the CPU until an interrupt occurs.
while (1) {
while (!need_resched()) { // check if any other process can run
asm("hlt"); // no runnable process, halt the CPU
}
schedule_idle(); // a process became runnable, switch to it
}When an interrupt arrives—e.g., a new TCP packet—the kernel wakes up, a blocked process becomes runnable, and need_resched() returns true. The idle loop exits the hlt state, calls schedule_idle(), and switches from the idle (PID 0, named "swapper") to the target process.
After the newly scheduled process finishes and all processes block again, the kernel returns to the idle loop, repeating the cycle.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.