欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POSIX定时器

程序员文章站 2022-07-14 13:18:33
...

代码(1s定时以创建线程的方式运行回调函数)

void handle(union sigval v)
{
 time_t t;
 char p[32];

 time(&t);
 strftime(p, sizeof(p), "%T", localtime(&t));

 printf("%s thread %lu, val = %d, signal captured.\n", p, pthread_self(), v.sival_int);
 return;
}

 

int main()
{
 struct sigevent evp;
 struct itimerspec ts;
 timer_t timer;
 int ret;

 memset  (&evp,  0,  sizeof   (evp));
 evp.sigev_value.sival_ptr = &timer;
 evp.sigev_notify = SIGEV_THREAD;
 evp.sigev_notify_function = handle;
 evp.sigev_value.sival_int = 3;   //作为handle()的参数

 ret = timer_create(CLOCK_REALTIME, &evp, &timer);
 if( ret)
  perror("timer_create");

 ts.it_interval.tv_sec = 1;
 ts.it_interval.tv_nsec = 0;
 ts.it_value.tv_sec = 3;
 ts.it_value.tv_nsec = 0;

 ret = timer_settime(timer, TIMER_ABSTIME, &ts, NULL);
 if( ret )
  perror("timer_settime");

 while(1);
}

代码(1s定时发送信号,并执行指定的函数)

void handle(union sigval v)
{
 time_t t;
 char p[32];

 time(&t);
 strftime(p, sizeof(p), "%T", localtime(&t));

 printf("%s thread %lu, val = %d, signal captured.\n", p, pthread_self(), v.sival_int);
 return;
}

 

int main()
{
 struct sigevent evp;
 struct itimerspec ts;
 timer_t timer;
 int ret;

 memset  (&evp,  0,  sizeof   (evp));
 evp.sigev_value.sival_ptr = &timer;
 evp.sigev_notify = SIGEV_THREAD;
 evp.sigev_notify_function = handle;
 evp.sigev_value.sival_int = 3;   //作为handle()的参数

 ret = timer_create(CLOCK_REALTIME, &evp, &timer);
 if( ret)
  perror("timer_create");

 ts.it_interval.tv_sec = 1;
 ts.it_interval.tv_nsec = 0;
 ts.it_value.tv_sec = 3;
 ts.it_value.tv_nsec = 0;

 ret = timer_settime(timer, TIMER_ABSTIME, &ts, NULL);
 if( ret )
  perror("timer_settime");

 while(1);
}

==============================================================================

相关说明

定时器接口来自POSIX时钟系列,其创建、初始化以及删除一个定时器的行动被分为三个不同的函数:timer_create()(创建定时器)、timer_settime()(初始化定时器)以及timer_delete(销毁定时器)。

注:
1.系统中如果一个模块需要频繁的获取系统时间,使用linux中内置的函数开销过大。
2.SIGALRM的中断信号回终止sleep,因为sleep就是用SIGALRM信号量实现的。

相关函数详解

==============================================================================

1.timer_create

//创建一个定时器
int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid)

…未完待续

==============================================================================

2.timer_settime

//设置定时时间并启动定时器
int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspect *ovalue);

…未完待续

==============================================================================

3.timer_gettime

//获得一个活动定时器的剩余时间:
int timer_gettime(timer_t timerid,struct itimerspec *value);

…未完待续

==============================================================================

4.timer_getoverrun

///获得一个定时器的超限运行次数:
int timer_getoverrun(timer_t timerid);

…未完待续

==============================================================================

5.timer_delete

删除一个定时器:
int timer_delete (timer_t timerid);

…未完待续

转载自
POSIX定时器:timer_settime()

相关标签: linux编程 linux

上一篇: POSIX 定时器

下一篇: Timer定时器