Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The CPU doesn't do the usage calculations by itself. It may have hardware features to make that task easier, but it's mostly the job of the operating system. So obviously the details of implementations will vary (especially in the case of multicore systems).</p> <p>The general idea is to see how long is the queue of things the CPU needs to do. The operating system may take a look at the scheduler periodically to determine the number of things it has to do. </p> <p><a href="http://en.wikipedia.org/wiki/Load_(computing)#CPU_Load_Computation" rel="nofollow noreferrer">This is a function Linux in (ripped from Wikipedia) that performs said calculation</a>:</p> <pre><code>#define FSHIFT 11 /* nr of bits of precision */ #define FIXED_1 (1&lt;&lt;FSHIFT) /* 1.0 as fixed-point */ #define LOAD_FREQ (5*HZ) /* 5 sec intervals */ #define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */ #define EXP_5 2014 /* 1/exp(5sec/5min) */ #define EXP_15 2037 /* 1/exp(5sec/15min) */ #define CALC_LOAD(load,exp,n) \ load *= exp; \ load += n*(FIXED_1-exp); \ load &gt;&gt;= FSHIFT; unsigned long avenrun[3]; static inline void calc_load(unsigned long ticks) { unsigned long active_tasks; /* fixed-point */ static int count = LOAD_FREQ; count -= ticks; if (count &lt; 0) { count += LOAD_FREQ; active_tasks = count_active_tasks(); CALC_LOAD(avenrun[0], EXP_1, active_tasks); CALC_LOAD(avenrun[1], EXP_5, active_tasks); CALC_LOAD(avenrun[2], EXP_15, active_tasks); } } </code></pre> <p>As for the second part of your question, most modern operating systems are <a href="http://en.wikipedia.org/wiki/Computer_multitasking" rel="nofollow noreferrer">multi-tasked</a>. That means the OS is not going to let programs take up all the processing time and not have any for itself <a href="http://blogs.msdn.com/b/oldnewthing/archive/2010/06/10/10022675.aspx" rel="nofollow noreferrer">(unless you make it do that)</a>. In other words, even if an application appears hung, the OS can <em>still</em> steal some time away for its own work. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload