首页 >> 知识 >> 定时器struct itimerval

定时器struct itimerval

struct itimerval {                 struct timevals it_interval; /* 计时器间隔时间 */                 struct timevals it_value;    /* 计时器开始执行时间*/             };

struct timevals {                 long tv_sec;                /* 秒 */                 long tv_usec;               /* 微妙 */             };

示例代码:

#include #include #include #include #include static int count = 0; void set_timer() {     struct itimerval itv;     itv.it_interval.tv_sec = 1;//间隔时间     itv.it_interval.tv_usec = 0;

    itv.it_value.tv_sec = 3;//启动时间     itv.it_value.tv_usec = 0;          setitimer(ITIMER_REAL,&itv,NULL); }

void timer_handal() {     printf("3秒开始了");     count++;     printf("第%d秒 ",count); } int main() {          set_timer();     signal(SIGALRM,timer_handal);     while(count

网站地图