CPU usage can be understood as the ratio of busy time to total time. If so, there is a natural solution: Empty loop and Sleep. The specific methods and codes are detailed in the original book "The beauty of programming. It should be noted that after measuring the number of empty loops that can be executed by the CPU in one second (assuming that the C empty loop can be executed), do not simply empty the C/2 cycles, and then rest for half a second. Although the curve obtained in this way is generally the required starting point, the jitter is very high. What is the solution?
The solution is to use a smaller time slice. If the original one-second empty loop can be performed C times, then they can be reduced by two orders of magnitude in the same direction. First empty loop C/100 times, and then Sleep for 10 milliseconds. The resulting curve is very smooth. This is an improvement of the first method: more precise simulation with finer granularity.
The first two methods both need to calculate how many empty loops can be made by the computer CPU in one second. If you change to a computer, you must recalculate the data. In fact, we can also feel that, since the usage is a ratio, the ratio is always relative. If we require a CPU usage of 50%, we can leave the CPU idle for half the time in a period of time, which is correct anywhere. You can just Sleep for a while, so how can you get busy for a fixed period of time? Or use an empty loop? If you have checked the clock function on MSDN, you will find that the following sample code is provided on MSDN:
/* Pauses for a specified number of milliseconds. */</p> <p> void sleep (clock_t wait) </p> <p >{< br/> clock_t goal; <br/> goal = wait + clock (); </p> <p> while (goal> clock () <br/>; <br/>}</p> <p>
Isn't it the time to use an empty loop to keep the CPU busy? As a result, the new method comes out, which does not require specific CPU parameters and is simpler, more agile, and more common. The following is a complete program written by myself to display the sine curve of CPU usage:
# Include <stdio. h> <br/> # include <time. h> <br/> # include <math. h> <br/> # include <windows. h> </p> <p> # define PI3.1415926535897932 <br/> # define TIME_UNIT500 // unit: millisecond </p> <p> # define COUNT100 // a complete sine curve cycle is divided into COUNT units </p> <p> int busy [COUNT], idle [COUNT]; </p> <p> int main () {<br/> const int half = TIME_UNIT/2; <br/> int I, j; <br/> for (I = 0; I <COUNT; I ++) {<br/> // The value range of sin (x) is [-], for full display, you must adjust the parameters. <br/> // Add the independent variables of sin (x. 5 * PI equals to shift sin (x) Left/4 cycles, <br/> // to display it from the beginning instead of % <br/> busy [I] = half * sin (2 * PI/COUNT * I + 1.5 * PI) + half; <br/> idle [I] = TIME_UNIT-busy [I]; <br/>}< br/> I = 0; <br/> clock_t begin; <br/> while (1) {<br/> begin = clock (); <br/> while (clock ()-begin <= busy [I]); <br/> Sleep (idle [I]); <br/> I = (I + 1) % COUNT; <br/>}< br/> return 0; <br/>}
The above three solutions are simple and common, and their ideas are worth learning.