Chinaunix首页 | 论坛 | 博客
  • 博客访问: 544136
  • 博文数量: 99
  • 博客积分: 5015
  • 博客等级: 大校
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-28 23:08
文章存档

2011年(7)

2010年(6)

2009年(86)

我的朋友

分类: LINUX

2009-07-06 20:32:46

 

setitimer()为Linux的API,并非C语言的Standard Library,setitimer()有两个功能,一是指定一段时间后,才执行某个function,二是每间格一段时间就执行某个function,以下程序demo如何使用setitimer()。

  1. /* 
  2.   
  3. Filename    : timer.cpp 
  4. Compiler    : gcc 4.1.0 on Fedora Core 5 
  5. Description : setitimer() set the interval to run function 
  6. Synopsis    : #include  
  7.               int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue); 
  8.               struct itimerval { 
  9.                 struct timerval it_interval; 
  10.                 struct timerval it_value; 
  11.               }; 
  12.   
  13.               struct timeval { 
  14.                 long tv_sec; 
  15.                 long tv_usec; 
  16.               }             
  17. Release     : 11/25/2006 
  18. */  
  19.   
  20. #include     // for printf()  
  21. #include    // for pause()  
  22.   
  23. #include    // for signal()  
  24. #include    // for memset()  
  25.   
  26. #include  // struct itimeral. setitimer()  
  27.   
  28.   
  29.   
  30. void printMsg(int);  
  31.   
  32.   
  33.   
  34. int main() {  
  35.   
  36.   // Get system call result to determine successful or failed  
  37.   
  38.   int res = 0;  
  39.   
  40.   // Register printMsg to SIGALRM  
  41.   
  42.   signal(SIGALRM, printMsg);  
  43.   
  44.     
  45.   
  46.   struct itimerval tick;  
  47.   
  48.   // Initialize struct  
  49.   
  50.   memset(&tick, 0, sizeof(tick));  
  51.   
  52.   // Timeout to run function first time  
  53.   
  54.   tick.it_value.tv_sec = 1;  // sec  
  55.   
  56.   tick.it_value.tv_usec = 0; // micro sec.  
  57.   
  58.   // Interval time to run function  
  59.   
  60.   tick.it_interval.tv_sec = 1;  
  61.   
  62.   tick.it_interval.tv_usec = 0;  
  63.   
  64.   // Set timer, ITIMER_REAL : real-time to decrease timer,  
  65.   
  66.   //                          send SIGALRM when timeout  
  67.   
  68.   res = setitimer(ITIMER_REAL, &tick, NULL);  
  69.   
  70.   if (res) {  
  71.   
  72.     printf("Set timer failed!!\n");  
  73.   
  74.   }  
  75.   
  76.   
  77.   
  78.   // Always sleep to catch SIGALRM signal  
  79.   
  80.   while(1) {  
  81.   
  82.     pause();  
  83.   
  84.   }  
  85.   
  86.   
  87.   
  88.   return 0;    
  89.   
  90. }  
  91.   
  92.   
  93.   
  94. void printMsg(int num) {  
  95.   
  96.   printf("%s","Hello World!!\n");  
  97.   
  98. }  

当setitimer()所执行的timer时间到了,会呼叫SIGALRM signal,所以在第30行用signal()将要执行的function指定给SIGALRM。 在第43行呼叫setitimer()设定timer,但setitimer()第二个参数是sturct,负责设定timeout时间,所以第36行到第 40行设定此struct。itimerval.it_value设定第一次执行function所延迟的秒数, itimerval.it_interval设定以后每几秒执行function,所以若只想延迟一段时间执行function,只要设定 itimerval.it_value即可,若要设定间格一段时间就执行function,则it_value和it_interval都要设定,否则 funtion的第一次无法执行,就别说以后的间隔执行了。 第36行和第39行的tv_sec为sec,第37行和40行为micro sec(0.001 sec)。 第43行的第一个参数ITIMER_REAL,表示以real-time方式减少timer,在timeout时会送出SIGALRM signal。第三个参数会存放旧的timeout值,如果不需要的话,指定NULL即可。 第47 行的pause(),命令系统进入sleep状态,等待任何signal,一定要用while(1)无穷循环执行pause(),如此才能一直接收 SIGALRM signal以间隔执行function,若拿掉while(1),则function只会执行一次而已。

阅读(994) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~