2015年7月27日 星期一

Linux Timer

Create your own timer routine in Linux | My Linux and Telecom Experiences
https://madalanarayana.wordpress.com/2014/01/25/create-your-own-timer-routine-in-linux/

Userspace application

  • add signal handler for SIGALRM/SIGVTALRM/SIGPROF, depends on the type of timer used.
  • call setitimer() to install a periodic timer
When the timer expiry, system will send a signal (SIGALRM/SIGVTALRM/SIGPROF)


Timekeeping in Linux Userspace | ctrLinux
http://www.ctrlinux.com/blog/?p=52

Linux C/C++ Timer signal handler in userspace - Code - Help To User
http://www.helptouser.com/code/5437240-linux-c-c-timer-signal-handler-in-userspace.html
setitimer(2) is a good start, but do you really want to go asynchronous with signals? Otherwise, you could have a main loop with select(2) or 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:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_05
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.
 

2015年7月9日 星期四