CPU – How Can a Process Use Less Than 100% CPU?

cpu

I don't know too terrible much about the CPU, but I know it processes assembly instructions and that Windows can say that it is at anywhere from 1-100% usage. How is it possible for a program to use less than 100% of the CPU? Wouldn't an instruction being executed cause it to be at "100%" usage?

Is it implemented in software? For example, when Windows is running a program, does it just decide that a program is allowed to run a certain number of instructions, and if so, how does it decide which programs should only take up a little bit of the overall usage?

Best Answer

The % usage you see, for example, in the Windows task manager, is an average value over a certain time period. And indeed, processing on a one-CPU machine works basically the way you already sketched in your question - the operating system assigns each process (and each thread inside the process) of a program a certain time slice for the execution of instructions, and then switches to another thread or process. Doing this many times per second creates the illusion of parallel processing even with only one CPU core. The part of the operating system which does this is called the scheduler.

But beware, this is a very simplified point of view, in reality, things are more complicated:

  • Different processes/threads may have different priorities, so the processes with higher priority are likely to get more instruction cycles than ones with lower priority.

  • Processes can willingly "wait" for certain events, and hand the execution over to other processes until that event (like a timer or I/O event) occurs.

  • As you can see in the Wikipedia article from the above link, different scheduling algorithms exist, and different operating systems implement different variants of them.

  • In case the machine has multiple CPU cores, using one core to 100% will show up as "100 / # of CPU cores" percent of the total available CPU usage, and the scheduler will have to distribute all processes and threads among all available CPUs.