Computer Programming is boring, but many programmers talk with relish. The computer world can be used as a programmer's game, such as CPU. Next let's let the job manager in Windows listen to our command and draw a perfect sine curve.
Ideas:
0. In visual windows, the refresh frequency of the task manager is 1 s. The CPU usage of each refresh is actually the busy time/full time of this period.
1. Use the loop and sleep methods to achieve CPU usage of any value.
2. To draw a sine curve, use two arrays busytime [] and idletime [] to determine the cycle and sleep time.
Note:
1. Assume that only the current program is running.
2. Run an infinite loop in a dual-core system. One of the CPUs occupies 100%, and the other has no impact.
Source code:
# Include "stdafx. H "<br/> # include" windows. H "<br/> # include" math. H "</P> <p> // C ++ code to make Task Manager generate sine graph <br/> const Double Split = 0.01; <br/> const int COUNT = 200; <br/> const double Pi = 3.14159265; <br/> const int interval = 300; </P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> DWORD busyspan [count]; // array of busy times <br/> DWORD idlespan [count]; // array of idle times <br/> int half = interval/2; <br/> double radian = 0.0; <br/> for (INT I = 0; I <count; I ++) {<br/> busyspan [I] = (DWORD) (half + (sin (pI * radian) * Half )); <br/> idlespan [I] = interval-busyspan [I]; <br/> radian + = split; <br/>}< br/> DWORD starttime = 0; <br/> Int J = 0; <br/> while (true) {<br/> J = J % count; <br/> starttime = gettickcount (); <br/> while (gettickcount ()-starttime) <= busyspan [J]) <br/> sleep (idlespan [J]); <br/> J ++; <br/>}< br/> return 0; <br/>}< br/>
Result: