linux c setitimer定時器

示例程序如下


#include <stdio.h>    // for printf()    
#include <signal.h>   
#include <sys/time.h>  
#include <stdio.h>
#include <errno.h>  
 
void sigFunc()  
{  
   static int iCnt = 0;  
   printf("The %d Times: Hello world\n", iCnt++);  
}  
 
int main(void)  
{  
   struct itimerval tv, otv;  
   signal(SIGALRM, sigFunc);  
   //how long to run the first time  
   tv.it_value.tv_sec = 1;  
   tv.it_value.tv_usec = 0;  
   //after the first time, how long to run next time  
   tv.it_interval.tv_sec = 1;  
   tv.it_interval.tv_usec = 0;  
 
   if (setitimer(ITIMER_REAL, &tv, &otv) != 0)  
    printf("setitimer err %d\n", errno);  

   printf("start timer after 1 second ...\n");
   while(1)  
   {   
      ;
   }  

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章