简体   繁体   English

如何减少自己C程序的CPU使用率

[英]How to reduce CPU usage from own C programms

User C programm do cpu usage of 200% on my RPi3+. 用户C程序在我的RPi3 +上执行200%的CPU使用率。 I noticed it because I wondered about the hight temperature. 我注意到了,因为我想知道高温。

I checked my other Pis with other own written C programms. 我用其他自己编写的C程序检查了我的其他Pi。 Nearly the same: CPU usage 100% (Pi3 but no plus). 几乎相同:CPU使用率100%(Pi3但无加号)。 When I kill/stop just this programm, the CPU usage and temperature drop down. 当我仅终止/停止该程序时,CPU使用率和温度下降。

In the while(1) loop i check the gpio state (connected to a button) . 在while(1)循环中,我检查了gpio状态(已连接到按钮)。 If it pressed i do things depending on duration that the button is pressed. 如果按下,我会根据按下按钮的持续时间来执行操作。 For expample print over cups or delete/change mysql data. 用于在杯子上打印示例或删除/更改mysql数据。

I read something about select() but i didnt understood the context or usage. 我读了一些有关select()的信息,但我不了解上下文或用法。 But I understood that slowering the loop with sleeps ist not the way of art. 但是我知道,通过睡眠来减缓循环并不是艺术的方法。

...
while(1)
{
   if (digitalRead(butPin)) // Button is released if this returns 1
   {
       digitalWrite(ledPin, LOW);     // Regular LED off
   }
   else 
   {
      //evalute button press
      ......
   }
...

All works fine and the pi response fast usally. 一切正常,圆周率响应通常很快。 But for this easy work the pi is too hot. 但是对于这种简单的工作,圆周率太高了。 So it also needs too much power. 因此它也需要太多功率。

EDIT: 编辑:

First try: add usleep() to the end of while(1) 首先尝试:将usleep()添加到while(1)的末尾

this reduce the CPU usage but not to low level. 这样可以减少CPU使用率,但不会降低到较低水平。

usleep(0) => 200% usleep(0) => 200%

usleep(5) => 111% usleep(5) => 111%

usleep(100) => 105% usleep(100) => 105%

usleep(5000) => 100% usleep(5000) => 100%

(on Raspberry 3+) (在Raspberry 3+上)

Top RPI3+ CPU 200% 顶级RPI3 + CPU 200%

You may let the OS spare resources (compute other things) by telling your program to sleep. 您可以通过让程序进入睡眠状态来让操作系统节省资源(计算其他事情)。

usleep(int timeInMicroseconds);

this will make the program sleep for timeInMicroseconds. 这将使程序休眠timeInMicroseconds。

As your program take in account Button pushes sleeping for a even a few milliseconds shouldn't make your program less efficient in detecting pushes. 由于您的程序考虑了Button推送,即使睡眠了几毫秒也不会降低您的程序检测推送的效率。

The more your program will sleep the more it will release the CPU, but also be less reactive to detect your pushes (and even doesn't detect them if the sleep period is too long). 程序睡眠的次数越多,释放的CPU就会越多,但是检测到推送的反应就更少(如果睡眠时间过长,甚至不会检测到它们)。 It's a balance; 这是一种平衡; some trial and error should give you a good value. 一些反复试验应该会给您带来很好的价值。

#include <unistd.h>

while(1)
{
   if (digitalRead(butPin)) // Button is released if this returns 1
   {
       digitalWrite(ledPin, LOW);     // Regular LED off
   }
   else 
   {

   }
   usleep(5000); //Sleep for 5ms
}

Another option would be to use interruption: Interruption with wiringPi . 另一个选择是使用中断: 用connectionPi打断

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM