Draw a sine curve for CPU usage

Source: Internet
Author: User

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:

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.