linux 系统中,当 cpu 上没有要执行的任务时,往往会运行 swapper 进程,即 idle 状态,这时我们可以说 cpu 处于空闲状态。
为什么在 cpu 处于无任务时要运行 swapper 而不是直接停止呢或者运行其他任务呢?
这里引用一个 stack overflow 的一个回答,大概意思是因为一些历史设计原因和编程调用算法的设计原因,原文如下:
The reason is historical and programatic. The idle task is the task running, if no other task is runnable, like you said it. It has the lowest possible priority, so that's why it's running of no other task is runnable.
Programatic reason: This simplifies process scheduling a lot, because you don't have to care about the special case: "What happens if no task is runnable?", because there always is at least one task runnable, the idle task. Also you can count the amount of cpu time used per task. Without the idle task, which task gets the cpu-time accounted no one needs?
Historical reason: Before we had cpus which are able to step-down or go into power saving modes, it HAD to run on full speed at any time. It ran a series of NOP-instructions, if no tasks were runnable. Today the scheduling of the idle task usually steps down the cpu by using HLT-instructions (halt), so power is saved. So there is a functionality somehow in the idle task in our days.
In Windows you can see the idle task in the process list, it's the idle process.