3

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?

  • 1
    The OS running the process usually prevents that situation. – πάντα ῥεῖ Sep 19 '19 at 18:25
10

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.

  • So then, for a process to have a 100% usage, it would have to be assigned a higher priority by the operating system? For example, in Windows, it would see that a program is using a lot of reads and writes, then assign it a higher priority so that it can use more cycles? – Fishy Sep 19 '19 at 19:14
  • @Fishy: as I wrote, in reality, things are more complicated. A process may get 100% CPU usage, but only if it contains at least as many threads as CPU cores in the machine, and only if these threads don't wait for events. A program which does a lot "read and writes (to a storage device, for example), does usually this - it sends data to some device, or asks to get some data from there, and then waits for the device - this way, it won't get necessarily more CPU usage by gettings its priority increased. – Doc Brown Sep 19 '19 at 19:22
  • @Fishy Generally, I/O heavy programs that does a lot of reads and writes typically use only around 0.1% CPU usage of a single core. This is because the majority of the program is stuck waiting for data to arrive from disk/network therefore the OS will notice this and stop running that program until the data arrives. Programs that use 100% CPU tend to be ones that does not do a lot of reads and writes (or delegate them to a separate thread) but instead does a lot of math (for example rendering a scene in a 3D movie) – slebetman Sep 25 '19 at 5:08
3

A CPU does not run calculations on several processes at the same time: it does a little bit of one, then another, then goes back to the first one, etc. Because each calculation takes fractions of seconds to execute, it feels for the user that several applications are running at the same time. So process A may keep the CPU 100% busy for 5 ms (milliseconds) then process B for 3 ms, then A again for 2 ms. Keeping in mind each time the CPU is "kept busy", it is used by the process at 100%. Note I have no idea if in reality the order of magnitude is milliseconds, or maybe even shorter.

The percentage of usage you can track will typically indicate how much was used by each process for a sampling period of time. For example if the sampling period is 1 second and over that period we recorded 200ms of usage for process A; this will result in showing 20% usage.

If you want to learn more about how a CPU will decide which process to execute first, you can search on CPU scheduling. This Microsoft article is a good start.

Back to your infered question: how can I prevent a process from showing 100% of usage? You need to set a lower scheduling priority to your process. As a rule of thumb:

  • User interface processes would want to have a high priority, in order to avoid the feeling the application is not performing well.
  • Ressource consumming processes will be given an average to low priority.
  • Non time-critical processes (for example a garbage collector) will be given the lowest priority.

The Scheduling priority section of the article I mentionned above is helpfull.

  • "how can I prevent a process for showing 100% of usage?" you can't prevent a processing from showing 100% usage by setting priority; a low priority process will still get 100% usage if there's no other higher priority processes that are ready to run at the time. If you want to limit the amount of CPU usage that a process can take at a time even when the system is otherwise idle, you'll need to set a quota/resource cap. – Lie Ryan Sep 20 '19 at 3:38
  • Your comment is subject to an interpretation of the OP's question; that is "how can I prevent a process from ever showing 100% usage", instead of "how can I prevent a process from showing 100% when other processes are waiting". However, would there be any benefits in preventing a process to use 100% of the CPU when no other process needs it? – Ama Sep 21 '19 at 7:14
  • 2
    "would there be any benefits in preventing a process to use 100% of the CPU when no other process needs it?" Yes -- power consumption and heat generation. I have an interest in not letting my unused CPU cycles get stolen by (for example) web browser bitcoin miners, because (for a desktop) I have to pay for the electricity used by the computer and also by the air conditioning to cool it and (for a portable) that drains the battery quickly. – Ben Voigt Sep 23 '19 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.