How does a CPU idle (or run below 100%)?

2020-06-13 13:42发布

问题:

I first learned about how computers work in terms of a primitive single stored program machine.

Now I'm learning about multitasking operating systems, scheduling, context switching, etc. I think I have a fairly good grasp of it all, except for one thing. I have always thought of a CPU as something which is just charging forward non-stop. It always knows where to go next (program counter), and it goes to that instruction, etc, ad infinitum.

Clearly this is not the case since my desktop computer CPU is not always running at 100%. So how does the CPU shut itself off or throttle itself down, and what role does the OS play in this? I'm guessing there's an input on the CPU somewhere which allows it to power down... and the OS can set this if it has nothing to schedule, but the next logical question is how does it start back up again? I'm guessing either one of two things:

  • It never shuts down completely, just runs at a very low frequency waiting for the scheduler to get busy again
  • It shuts down completely but is woken up by interrupts

I searched all over for info on this and came up fairly empty-handed. Any insight would be much appreciated.

回答1:

The answer is that is depends on the hardware, the operating system and the way that the operating system has been configured.

And it could involve either or both of the strategies you proposed.

Another possibility for machines based on the x86 architecture, is that x86 has an HLT instruction that causes the core to stop until it receives an external interrupt. So the "Idle" task could simply execute HLT in a tight loop.



回答2:

Just go to task manager, performance tab, and watch the cpu usage while you're doing absolutely nothing on your computer. it never stops fluctuating. Having an operating system like windows running, the cpu is going to ALWAYS be functioning, it never completely shuts down.

Having your monitor display an image requires your cpu to process a function allowing it to display anything. etc.

Everything runs through the CPU, just like your brain, it controls everything. nothing would function without it.



回答3:

Some CPUs do have a 'wait for interrupt' instruction which allows the CPU to stop executing instructions when there is nothing to do, and will not re-awake until there is an interrupt event. This is particularly useful in microcontrollers, where they can sit for long periods of time waiting for something to happen.

  • Intel = HLT (Halt)
  • ARM = WFI (Wait for interrupt)

Sometimes a 'busy wait' is also used, where the CPU sits in a little 'idle' loop, checking for things to do. In this case, the CPU is still running instructions, but the operating system is in an idle state. It's not as efficient as using a HLT.

Modern CPUs can also adjust their power usage, and are capable of reducing clock rates, or shutting down parts of the CPU that aren't being used. In this way, power usage during an active idle state can be less than during active processing, even though the core CPU is still running and executing instructions.



回答4:

If speaking about x86 architecture when an operating system has nothing to do it can use HLT instruction. HLT instruction stops the CPU till next interrupt. See http://en.m.wikipedia.org/wiki/HLT for details.

Other architectures have similar instruction to give CPU a rest.