- 论坛徽章:
- 0
|
本帖最后由 hbmhalley 于 2011-12-17 12:35 编辑
我想搞一个计时器,由于alarm不够精准,于是找到了timer
但是 我希望在exec之后仍能超时,这点alarm可以做到,但timer却做不到,这是为什么?
- //dead.c
- // gcc dead.c -o dead
- int main () {
- for (;;);
- }
复制代码
- //alrm.c
- // gcc alrm.c -o alrm -lrt
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <signal.h>
- #include <time.h>
- int main () {
- #ifdef ALARM
- alarm (1) ;
- #else
- timer_t tid ;
- timer_create (CLOCK_REALTIME , NULL , &tid) ;
- struct itimerspec itimer ;
- memset (&itimer , 0 , sizeof itimer) ;
- itimer . it_value . tv_sec = 1 ;
- itimer . it_value . tv_nsec = 0 ;
- timer_settime (tid , 0 , &itimer , NULL) ;
- #endif
- execl ("./noend" , "./dead" , NULL) ;
- }
复制代码 设置ALARM后,./alrm一秒后被中断,否则死循环 |
|