| |

VerySource

 Forgot password?
 Register
Search
View: 1047|Reply: 5

How to control the CPU usage of a process?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-18 18:20:02
| Show all posts |Read mode
For example, a scientific calculation program, which mainly performs non-stop loop calculations. If no special processing is done, the CPU usage of the program is generally 100%. Is there any way to control his usage It is best to be able to control a certain percentage. When the anti-virus software MCAFEE checks the virus, it can control the CPU usage to a specific value.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 17:00:01
| Show all posts
attention
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 17:45:02
| Show all posts
Lower thread priority
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 18:00:09
| Show all posts
Do you want to control the CPU usage of this program or the final CPU usage of the system?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 19:00:01
| Show all posts
Two threads are used, one is in the running loop, and the other is to obtain the CPU usage.When the CPU usage reaches the specified value, send a message to the running process to let it temporarily sleep down.

But it may not meet your requirements!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 19:15:01
| Show all posts
/*
************************************************** ************************
*
* This allows us to better catch runaway realtime processes that
* might otherwise hang the whole system (if they're POSIX realtime
* processes).
*
* We do this by getting a "lease" on CPU time, and then extending
* the lease every so often as real time elapses. Since we only
* extend the lease by a bounded amount computed on the basis of an
* upper bound of how much CPU the code is "expected" to consume during
* the lease interval, this means that if we go into an infinite
* loop, it is highly probable that this will be detected and our
* process will be terminated by the operating system with a SIGXCPU.
*
* If you want to handle this signal, then fine... Do so...
*
* If not, the default is to terminate the process and produce a core
* dump. This is a great default for debugging...
*
*
* The process is basically this:
*-Set the CPU percentage limit with cl_cpu_limit_setpercent()
* according to what you expect the CPU percentage to top out at
* measured over an interval at >= 60 seconds
*-Call cl_cpu_limit_ms_interval() to figure out how often to update
* the CPU limit (it returns milliseconds)
*-At least as often as indicated above, call cl_cpu_limit_update()
* to update our current CPU limit.
*
* These limits are approximate, so be a little conservative.
* If you've gone into an infinite loop, it'll likely get caught ;-)
*
* As of this writing, this code will never set the soft CPU limit less
* than four seconds, or greater than 60 seconds.
*
*/
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list