Topic 1: Write a program to show CPU occupancy in windows Task Manager as a sinusoidal function curve.
At first glance, I really don't have much idea about this topic. Because I haven't thought too much about performance and CPU usage. It's really a good application question after reading and searching materials. The following tasks will be completed step by step.
2. My operating environment:
Operating system: win 7 32 bits
CPU: Intel i7 2630 QM (2.0GHZ 4 core 8 threads)
III. Specific Realization
1. Multi-CPU Solutions
Because the program will show confusion under multi-CPU, code is needed to specify which CPU the program runs under.
The CPU can be specified using the Windows API with the following code:
CPU has two states: busy and idle.
Control busy: Dead cycle.
Control leisure: sleep.
Windows Explorer: The CPU curve displayed is a recorded curve that records the current CPU occupancy and then draws the line slowly.
3. Control CPU by calculating CPU frequency
Operation effect:
As can be seen, the effect is not very good. Manual calculation must be inaccurate! And other programs also take up CPU.
4. Set the interval by GetTickCount() function
The advantage of doing this is that you don't need to estimate the CPU frequency, which is much better than the above method.
Effect:
Of course, the effect is general, paving the way for the following.
5. Drawing Sinusoidal Functions
To draw a sinusoidal curve, CPU busy time must increase slowly and then decrease. CPU idle time must be slowly decreasing and then increasing.
This is a cycle. The sinusoidal function curve is obtained by repeating the last straight cycle.
Print out part of the busy time relationship:
Code:
Operation effect:
Changing the interval time INTERVAL = 100 can reduce the period accordingly:
For reprinting, please indicate the source: http://blog.csdn.net/xn4545945
Reference to Beauty of Programming and Internet