linux c setitimer定时器

来源:互联网 发布:开过淘宝店铺能注销吗 编辑:程序博客网 时间:2024/05/21 11:22

示例程序如下


#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)  
   {   
      ;
   }  

}


0 0