linux环境实现定时器----- itimerval实现
分析:实现定时器,通过itimerval结构体以及函数setitimer产生的信号,系统随之使用signal信号处理函数来处理产生的定时信号。从而实现定时器。
先看itimerval的结构体
struct itimerval
{
/* Value to put into `it_value' when the timer expires. */
struct timeval it_interval;
/* Time to the next timer expiration. */
struct timeval it_value;
};
it_interval:代表定时器重新启动的间歇值
it_value:计时器安装后首先启动的初始值。
struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};
int setitimer (__itimer_which_t __which,
const struct itimerval *__restrict __new,
struct itimerval *__restrict __old)
setitimer()将value指向的结构体设为计时器的当前值,如果ovalue不是NULL,将返回计时器原有值。
which:三种类型
- ITIMER_REAL //数值为0,计时器的值实时递减,发送的信号是SIGALRM。
- ITIMER_VIRTUAL //数值为1,进程执行时递减计时器的值,发送的信号是SIGVTALRM。
- ITIMER_PROF //数值为2,进程和系统执行时都递减计时器的值,发送的信号是SIGPROF。
- 返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EFAULT:value或ovalue是不有效的指针
EINVAL:其值不是ITIMER_REAL,ITIMER_VIRTUAL 或 ITIMER_PROF之一
实现代码:
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
static int i;
void signal_handler(int signam)
{
i++;
printf("catch signal num is :%d\n",signam);
printf("i = %d\n",i);
}
void settimer()
{
struct itimerval itv;
//设置3s后开始定时
itv.it_value.tv_sec = 3;
itv.it_value.tv_usec = 0;
//每1s
itv.it_interval.tv_sec = 1;
itv.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL,&itv,NULL);
signal(SIGALRM,signal_handler);
}
int main()
{
settimer();
while (i < 20)
{
;
}
return 0;
}
该代码实现的功能时3s后开启定时器,然后每隔1s向终端打印1的数值。直到20程序退出。结果应该打印出20个1且打印出捕捉的信号值。结果如下: