Create your own timer routine in Linux | My Linux and Telecom Experiences
- add signal handler for SIGALRM/SIGVTALRM/SIGPROF, depends on the type of timer used.
- call setitimer() to install a periodic timer
Timekeeping in Linux Userspace | ctrLinux
Linux C/C++ Timer signal handler in userspace - Code - Help To User
setitimer(2)is a good start, but do you really want to go asynchronous with signals? Otherwise, you could have a main loop with
poll(2)and an appropiate timeout.
A much safer alternative to
setitimer(which POSIX 2008 marks
OBSolete) would be to use POSIX timers, and have the timer expiration function run in a thread rather than a signal handler. This way you are not restricted to only using async-signal-safe functions. They're documented here:
If you don't like the POSIX timers API, you could instead create a thread that merely sleeps in a loop, and block the timer signal in all threads except that thread. Then you will be free to use whatever functions you like in the signal handler, since it will run in a separate thread and there is no danger of it interrupting an async-signal-unsafe function.