以应用实例解释timer用法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <pthread.h>
#include <linux/unistd.h>
#if 0
/*
* posix timer
* timer_create(clockid_t clockid, struct sigevent *sevp,timer_t *timerid);
* timer_settime(timer_t timerid, int flags,
* const struct itimerspec *new_value,
* struct itimerspec * old_value);
* timer_gettime(timer_t timerid, struct itimerspec *curr_value);;
* timer_delete(timer_t timerid);
*struct timespec {
* time_t tv_sec; /* Seconds */
* long tv_nsec; /* Nanoseconds */
* };
*
* struct itimerspec {
* struct timespec it_interval; /* Timer interval */
* struct timespec it_value; /* Initial expiration */
* };
* union sigval { /* Data passed with notification */
* int sival_int; /* Integer value */
* void *sival_ptr; /* Pointer value */
* };
*老版本
* struct sigevent {
* int sigev_notify; /* Notification method */
* int sigev_signo; /* Notification signal */
* union sigval sigev_value; /* Data passed with
* notification */
* void (*sigev_notify_function) (union sigval);
* /* Function used for thread
* notification (SIGEV_THREAD) */
* void *sigev_notify_attributes;
* /* Attributes for notification thread
* (SIGEV_THREAD) */
* pid_t sigev_notify_thread_id;
* /* ID of thread to signal (SIGEV_THREAD_ID) */
* };
*新版本
*typedef struct sigevent {
* sigval_t sigev_value;
* int sigev_signo;
* int sigev_notify;
* union {
* int _pad[SIGEV_PAD_SIZE];
* int _tid;
*
* struct {
* void (*_function)(sigval_t);
* void *_attribute; /* really pthread_attr_t */
* } _sigev_thread;
* } _sigev_un;
*} sigevent_t;
*
*clockid:
* CLOCK_REALTIME: 系统的实时时钟时间,滴答
* CLOCK_MONOTONIC: 系统从某个时间点,消耗的时间
* CLOCK_PROCESS_CPUTIME_ID: 进程运行消耗的时间
* CLOCK_THREAD_CPUTIME_TID: 线程运行消耗的时间
*
*struct sigevent t
*{
* .sigev_notify = {
* SIGEV_NONE: 当时间消耗完之后,不会进行通知,需要自己timer_gettime()获取。
* SIGEV_SIGNAL: 当时间消耗完之后,给进程发送一个信号
* SIGEV_THREAD: 当时间消耗完之后,调用sigev_notify_function处理
* SIGEV_THREAD_ID: 当时间消耗完之后,给调定线程发送信号
* },
*}
*
*
*
*
*
*
*
*
*/
#endif
/// SIGEV_THREAD
char *str = "hello world\n";
void handler(union sigval v)
{
char *str = v.sival_ptr;
printf("%s\n", str);
}
int main()
{
timer_t timer_id;
struct sigevent se;
struct itimerspec ts;
int ret = 0;
se.sigev_notify = SIGEV_THREAD;
se.sigev_value.sival_ptr = str;
se.sigev_notify_function = handler;
se.sigev_notify_attributes = NULL; //注意如果不用必须为null
ret = timer_create(CLOCK_REALTIME, &se, &timer_id);
if(ret < 0)
{
printf("timer create error\n");
return -1;
}
ts.it_value.tv_sec = 3;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec =3;
ts.it_interval.tv_nsec = 0;
ret = timer_settime(timer_id, 0, &ts, 0);
if(ret < 0)
return -1;
while(1)
sleep(1);
return 0;
}
/// SIGEV_NONE
int main()
{
timer_t timer_id;
struct sigevent se;
struct itimerspec ts;
struct itimerspec res;
int ret = 0;
se.sigev_notify = SIGEV_NONE;
ret = timer_create(CLOCK_REALTIME, &se, &timer_id);
if(ret < 0)
{
printf("timer create error\n");
return -1;
}
ts.it_value.tv_sec = 3;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec =3;
ts.it_interval.tv_nsec = 0;
ret = timer_settime(timer_id, 0, &ts, 0);
if(ret < 0)
return -1;
while(1)
{
sleep(1);
timer_gettime(timer_id, &res);
printf("it_value.tv_sec: %d\n", res.it_value.tv_sec);
printf("it_value.tv_nsec: %d\n", res.it_value.tv_nsec);
printf("it_interval.tv_sec: %d\n", res.it_interval.tv_sec);
printf("it_interval.tv_nsec: %d\n", res.it_interval.tv_nsec);
}
return 0;
}
/// SIGEV_SIGNAL
int main()
{
timer_t timer_id;
struct sigevent se;
struct itimerspec ts;
struct itimerspec res;
int ret = 0;
se.sigev_notify = SIGEV_SIGNAL;
se.sigev_signo = SIGALRM; //缺省的动作是终止
ret = timer_create(CLOCK_REALTIME, &se, &timer_id);
if(ret < 0)
{
printf("timer create error\n");
return -1;
}
ts.it_value.tv_sec = 3;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec =3;
ts.it_interval.tv_nsec = 0;
ret = timer_settime(timer_id, 0, &ts, 0);
if(ret < 0)
return -1;
while(1)
{
sleep(1);
printf("I am live\n");
}
return 0;
}
/// SIGEV_THREAD_ID
int main()
{
timer_t timer_id;
struct sigevent se;
struct itimerspec ts;
struct itimerspec res;
int ret = 0;
pid_t pid;
pid = getpid();
se.sigev_notify = SIGEV_THREAD_ID;
se.sigev_signo = SIGALRM; //缺省的动作是终止
se._sigev_un._tid = pid; //从实验来看,老版与新版没有完全兼容
ret = timer_create(CLOCK_REALTIME, &se, &timer_id);
if(ret < 0)
{
printf("timer create error\n");
return -1;
}
ts.it_value.tv_sec = 3;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec =3;
ts.it_interval.tv_nsec = 0;
ret = timer_settime(timer_id, 0, &ts, 0);
if(ret < 0)
return -1;
while(1)
{
sleep(1);
printf("I am live\n");
}
return 0;
}