1

I'm writing a simple application in C to handle switching relays on an old Pi 1B. When the Pi is supposed to wait for the next action to handle, I use the sleep() system call. I checked with top, only to find out that my application still uses 10% CPU! Why?? So I tried to rewrite the code using alarm() and pause(). Of course the same: 10% CPU. Again: why??

Is there a way to put the process really to sleep, 0% CPU? Tips maybe?

Proof of a simple application using 10%: enter image description here

#include <json-c/json.h>
#include <json-c/json_util.h>

main() {
    gpioInitialise();
    sleep(10000);
}
3
  • We need the source code of a simple example of your application which uses 10%.
    – joan
    Feb 20 at 16:11
  • sleep pauses execution of a process, but the original process may have spawned other processes, and these child processes won't necessarily pause. You may have to pause at the "kernel level" to reach your objective; here's a paper discussing some ways to do that
    – Seamus
    Feb 21 at 1:52
  • In the sample C-program above nothing is forked (by me), only a library is initialised and that seems to be enough to cause a constant 10% CPU load on a Raspberry Pi 1B. My objective is not to put the whole system in sleep-mode, just the process will do. I bet that if I remove the gpioInitialise() call the load will drop to <1%...
    – D.Bugger
    Feb 21 at 9:18

2 Answers 2

1

If you are using pigpio this is normal behaviour.

pigpio uses about 10% of 1 CPU to do the DMA sampling of GPIO and the generation of PWM. This is a constant overhead when using pigpio.

1
  • Thanks, it's what crossed my mind. Obvious questions: are there other libraries that do not use 10% CPU? Or can I tell pigpio somehow not to sample when I don't need it? The thing is supposed to do nothing...
    – D.Bugger
    Feb 20 at 21:51
0

I adopted a different library, and CPU utilisation during the sleep() call went down to 0: libgpiod to the rescue! It might not be the best gpio library around but at least there's no overhead in my case. Sample: https://www.ics.com/blog/gpio-programming-exploring-libgpiod-library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.