Program: Control cpu usage

Source: Internet
Author: User

I have seen a book named "the beauty of programming" in recent days. I want to share my knowledge with you here. If you have better suggestions and solutions, please give me more advice.

The simplest requirement: write a program to control the CPU usage of 50%.

Consider: the task manager refresh every second. In this second, the average CPU usage time and idle time account for 50%, instead of 1 second for the CPU usage. In this case, the CPU usage image displayed in the task manager is high or low. In windows, the scheduling time slice is 10 ms, so the cpu can keep working for 10 ms during programming, and then idle for 10 ms. Idle operations are implemented through sleep (10. In this way, the task manager will get the average CPU usage in one second when refreshing the CPU usage. Shows the level of 50%.

An empty loop is used to keep the cpu busy and idle by sleep.

Machine commands are the closest to assembly languages.

An empty for loop is about five sentences in assembly language. Currently, the CPU can execute two machine commands in one clock cycle, which is equivalent to two Assembly statements.

Considering that the clock speed of my computer is 2.0 GB, it should be 2000000000*2/5 = 800000000 null loops per second. However, the machine cannot execute an empty loop for one second. Therefore, the time slice should be executed. A time slice is 10 ms, so the execution of an empty loop should be 8000000 times.

Code:
 
# Include <stdio. h>
# Include <stdlib. h>
 
Int main (int argc, char * argv [])
{
Long n = 8000000;
Int I = 0;
While (1)
{
For (I = 0; I <n; I ++)
{
// Idle
}
Sleep (10 );
}
System ("PAUSE ");
Return 0;
}

Execution result

 

The actual execution is not very good, because the operating system currently runs more than our programs, there are many other operating system components. At the same time, the operating system also performs IO operations, kernel scheduling, and other operations.

However, there are other solutions that can be solved. The next article will describe the second solution.

Author: Watkins. Song

Related Article

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.