Linux C / C ++ Linear Timer Signal Handler in User Space

I need a function (like a signal handler) in C / C ++ linux that gets activated every "n" milliseconds. How to set up signals, etc. For logging timer events with a resolution in milliseconds.

Accuracy is not supercritical, but is needed for hundreds of ms or so.

I am new to Linux and I really don't know where to start.

+4
source share
2 answers

setitimer(2) is a good start, but do you really want to asynchronously with signals? Otherwise, you may have a main loop with select(2) or poll(2) and the corresponding timeout.

+3
source

A safer alternative to setitimer (which POSIX 2008 stands for OBS olete) will use POSIX timers and use the expiration function of the timer in the stream rather than the signal handler. Thus, you are not limited to using functions that support async signal. They are described here:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_05

If you don't like the POSIX Timer API, you can create a thread that just sleeps in a loop and block the timer signal in all threads except this thread. Then you can freely use any functions that you like in the signal handler, because they will work in a separate stream, and there is no danger that it interrupts the function of the non-synchronizing signal of the asynchronous signal.

+6
source

Source: https://habr.com/ru/post/1345270/


All Articles