Chinaunix

标题: alarm与timer_settime的区别 [打印本页]

作者: hbmhalley    时间: 2011-12-17 12:33
标题: alarm与timer_settime的区别
本帖最后由 hbmhalley 于 2011-12-17 12:35 编辑

我想搞一个计时器,由于alarm不够精准,于是找到了timer
但是 我希望在exec之后仍能超时,这点alarm可以做到,但timer却做不到,这是为什么?

  1. //dead.c
  2. // gcc dead.c -o dead
  3. int main () {
  4.         for (;;);
  5. }
复制代码

  1. //alrm.c
  2. // gcc alrm.c -o alrm -lrt
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #include <time.h>

  8. int main () {
  9. #ifdef ALARM
  10.         alarm (1) ;
  11. #else
  12.         timer_t tid ;
  13.         timer_create (CLOCK_REALTIME , NULL , &tid) ;
  14.         struct itimerspec itimer ;
  15.         memset (&itimer , 0 , sizeof itimer) ;
  16.         itimer . it_value . tv_sec = 1 ;
  17.         itimer . it_value . tv_nsec = 0 ;
  18.         timer_settime (tid , 0 , &itimer , NULL) ;
  19. #endif
  20.         execl ("./noend" , "./dead" , NULL) ;
  21. }
复制代码
设置ALARM后,./alrm一秒后被中断,否则死循环




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2