daemon中的定时器
时间:2009-03-15 来源:CUDev
项目中使用了定时器来提供一个类似于心跳的功能,在调试的过程中,总是出现,定时器不准的情况,实际上是在大量线程并发执行的时候,心跳得不到执行,使用了定时器,为什么会不准呢?
忽然想起,守护进程会调用fork()来创建新的子进程,子进程对信号是继承的,但是对定时器却不是继承的。
man 3 fork中的一段:
* The set of signals pending for the child process shall be initialized to the empty set.
* Interval timers shall be reset in the child process.
示例代码:
参考:
http://blog.chinaunix.net/u2/72751/showart_1133793.html
http://www.eefocus.com/article/07-10/26802s.html
忽然想起,守护进程会调用fork()来创建新的子进程,子进程对信号是继承的,但是对定时器却不是继承的。
man 3 fork中的一段:
* The set of signals pending for the child process shall be initialized to the empty set.
* Interval timers shall be reset in the child process.
示例代码:
void init_timer() { struct itimerval value; //设置SIGALARM信号处理函数 signal(SIGALRM, redline_timer_func); //设置定时器 value.it_value.tv_sec=10; //间隔10s value.it_value.tv_usec=0; value.it_interval.tv_sec=10; value.it_interval.tv_usec=0; setitimer(ITIMER_REAL, &value, NULL); } ============================================== signal(SIGINT, sig_func); signal(SIGALRM, timer_func); GoDaemon(); init_timer(); |
参考:
http://blog.chinaunix.net/u2/72751/showart_1133793.html
http://www.eefocus.com/article/07-10/26802s.html
相关阅读 更多 +