In Linux, two basic timer mechanisms are provided: alarm and settimer.
1. Alarm # include <unistd. h> unsigned int alarm (unsigned int seconds); this is the simplest timer. When alarm (n) is called, a sigalrm signal is triggered after n seconds, therefore, you must set the processing function corresponding to the sigalrm signal before calling the alarm function. When Calling Alarm (0), it indicates that the current timer processing is stopped and the sigalrm signal is not sent. Return Value: return the remaining seconds of the last alarm function call. If the alarm function is not called before, 0 is returned. Example: # include <iostream> # include <unistd. h> # include <signal. h> using namespace STD; void my_alarm_handler (int A) {cerr <"my_alarm_handler" <Endl; Alarm (2 ); // change to 2 seconds to call timer} int main () {sigalrm (sigalrm, my_alarm_handler); Alarm (1); While (1) {} return 0 ;} 2. settimer # include <sys/time. h> # define itimer_real 0 # define itimer_virtual 1 # define itimer_prof 2 int getitimer (int Which, struct itimerval * value); int setitimer (INT which, const struct itimerval * value, struct itimerval * ovalue); settimer and gettimer functions provide three types of timer for use: 1) itimer_real: calculate based on the actual time of the system, and a sigalrm signal will be triggered. 2) itimer_virtual: only calculate the execution time of the process (in the user State), and The sigvtalrm signal is triggered. 3) itimer_prof: indicates the processing time of the computing process in the user State and kernel state, and the sigprof signal is triggered. The first parameter which is used to specify which timer (itimer_real, itimer_virtual, and itimer_prof) to be used ). The settimer function is used to set the trigger time of the corresponding timer, while the gettimer function is used to obtain the time set by the last timer. The set time is a schema body, struct itimerval: struct itimerval {struct timeval it_interval; struct timeval it_value ;}; struct timeval {long TV _sec; long TV _usec ;}; settimer sets the trigger time by the second parameter value, and the third parameter ovalue is used to obtain the itimerval value set by settimer (this parameter can be set to null ). For the value of the variable in itimerval, when we set it_interval to 0, timer will only trigger once, and if it_value is set to 0, timer will end. Return Value: 0 indicates success, and-1 indicates failure. Example: # include <iostream> # include <sys/time. h> # include <signal. h> using namespace STD; void my_alarm_handler (int A) {cerr <"test" <Endl ;}int main () {struct itimerval t; T. it_interval. TV _usec = 0; T. it_interval. TV _sec = 2; T. it_value. TV _usec = 0; T. it_value. TV _sec = 1; if (setitimer (itimer_real, & T, null) <0) {cerr <"settimer error. "<Endl; Return-1;} signal (sigalrm, My_alarm_handler); While (1) {sleep (2);} return 0;} through the above example, we can know that Linux built-in timer can only process three timers at the same time, if you need more than one, this is a problem. However, we can through sleep function or time function to combine the use of timing function, specific can refer to: http://hi.baidu.com/adrain001/blog/item/60580bc40871d6a18226ace4.html Reference URL: http://code.google.com/p/androidteam/wiki/TimerExample http://hi.baidu.com/adrain001/blog/item/6d488b63ff13fe680c33fae6.html http://www.ezloo.com/2008/05/linux_timer.html http://blog.sina.com.cn/s/blog_66a6a5ec0100koc0.html http://hi.baidu.com/susdisk/blog/item/03f70d35e8e2e182a61e1288.html (this article from the http://blog.sina.com.cn/s/blog_3e4774e30100puh4.html)