timer_create()建立定时器剖析

在程序开发中经常需要使用到定时器
常用的是while(true)与sleep(int)组合的形式简单方便
如果对应用程序精度要求比较低可以直接使用sleep();
本文总结另外一种方法:使用timer_create()函数
其中利用了LINUX系统的信号量机制,注册信号量处理函数
比如信号量:SIGALRM,SIGUSR1
计时精度要求不高的情况下
使用signal函数配合setitimer实现了一个简易的定时器

使用SIGUSR1信号量定时

这里使用SIGUSR1信号量进行测试:

//signalDemo.cpp
//compile : gcc signalDemo.cpp -o testSignal -lrt
#include <unistd.h>
#include <stdio.h> 
#include <signal.h>
#include <time.h>

void testTimerSign();
void SignHandler(int iSignNo);
void printTime();

int main() {
    testTimerSign();
    while(true) {
        sleep(1); 
    }
    return 0; 
}

void SignHandler(int iSignNo){
    printTime();
    if(iSignNo == SIGUSR1){
        printf("Capture sign No.=SIGUSR1\n"); 
    }else{
        printf("Capture sign No.=%d\n",iSignNo); 
    }
}

void testTimerSign(){
    struct sigevent evp;  
    struct itimerspec ts;  
    timer_t timer;  
    int ret;  
    evp.sigev_value.sival_ptr = &timer;  
    evp.sigev_notify = SIGEV_SIGNAL;  
    evp.sigev_signo = SIGUSR1;  
    signal(evp.sigev_signo, SignHandler); 
    ret = timer_create(CLOCK_REALTIME, &evp, &timer);  
    if(ret) {
        perror("timer_create");
    } 
    ts.it_interval.tv_sec = 1; // the spacing time  
    ts.it_interval.tv_nsec = 0;  
    ts.it_value.tv_sec = 2;  // the delay time start
    ts.it_value.tv_nsec = 0;  
    printTime();
    printf("start\n");
    ret = timer_settime(timer, 0, &ts, NULL);  
    if(ret) {
        perror("timer_settime"); 
    } 
}

void printTime(){
    struct tm *cursystem;
    time_t tm_t;
    time(&tm_t);
    cursystem = localtime(&tm_t);
    char tszInfo[2048] ;
    sprintf(tszInfo, "%02d:%02d:%02d", 
        cursystem->tm_hour, cursystem->tm_min, 
        cursystem->tm_sec);
    printf("[%s]",tszInfo);
}

为了方便查看程序运行结果,打印中输出时间
这里写图片描述
定时器的开始延时2秒;

使用SIGALRM信号量定时

上面程序中使用了信号量SIGUSR1;
如果使用信号量SIGALRM;
(对 CLOCK_REALTIMER来说,默认信号就是SIGALRM)
sleep()函数使用的就是实时时钟CLOCK_REALTIMER
所以使用信号值SIGALRM会中断sleep(int second)函数的休眠;

//timercreate_demo.cpp
#include <unistd.h> 
#include <stdio.h>
#include <signal.h>
#include <time.h>

void SignHandler(int iSignNo);
void testTimerSign();
void printTime();

int main() {
    testTimerSign();
    while(true){
         int left = sleep(5);
         printTime();
         printf("sleep(5)(left=%d)\n", left);
    }
    return 0; 
}

void SignHandler(int iSignNo){
    //printTime();
    if(iSignNo == SIGUSR1){
        printf("Capture sign no : SIGUSR1\n"); 
    }else if(SIGALRM == iSignNo){
        //printf("Capture sign no : SIGALRM\n"); 
    }else{
        printf("Capture sign no:%d\n",iSignNo); 
    }
}

void testTimerSign(){
    struct sigevent evp;  
    struct itimerspec ts;  
    timer_t timer;  
    int ret;  
    evp.sigev_value.sival_ptr = &timer;  
    evp.sigev_notify = SIGEV_SIGNAL;  
    evp.sigev_signo = SIGALRM;
    signal(evp.sigev_signo, SignHandler); 
    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 = 1;
    ts.it_value.tv_nsec = 0;  
    printTime();
    printf("start\n");
    ret = timer_settime(timer, 0, &ts, NULL);  
    if(ret) {
        perror("timer_settime"); 
    } 
}

void printTime(){
    struct tm *cursystem;
    time_t tm_t;
    time(&tm_t);
    cursystem = localtime(&tm_t);
    char tszInfo[2048] ;
    sprintf(tszInfo, "%02d:%02d:%02d", 
        cursystem->tm_hour, 
        cursystem->tm_min, 
        cursystem->tm_sec);
        printf("[%s]",tszInfo);
}

这里写图片描述
因为timer_settime()中定时器间隔时间为1秒
于是sleep(5)每次都被打断不能按时休眠,剩余4秒未能执行;

SIGALRM信号量不同线程的影响

因为休眠sleep(unsigned int)为线程内操作
所以如果不同线程,信号量SIGALRM是不能中断sleep();

编写程序进行测试

//timercreate_demo.cpp
#include <unistd.h> 
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <pthread.h>  

void SignHandler(int iSignNo);
void testTimerSign();
void printTime();
void *function(void *arg);

int main() {
    pthread_t thread1;  
    pthread_create(&thread1,NULL,function,(char*)"111"); 
    testTimerSign();
    while(true);
    return 0; 
}

void SignHandler(int iSignNo){
    if(iSignNo == SIGUSR1){
        printf("Capture sign no : SIGUSR1\n"); 
    }else if(SIGALRM == iSignNo){
        //printf("Capture sign no : SIGALRM\n"); 
    }else{
        printf("Capture sign no:%d\n",iSignNo); 
    }
}

void testTimerSign(){
    struct sigevent evp;  
    struct itimerspec ts;  
    timer_t timer;  
    int ret;  
    evp.sigev_value.sival_ptr = &timer;  
    evp.sigev_notify = SIGEV_SIGNAL;  
    evp.sigev_signo = SIGALRM;
    signal(evp.sigev_signo, SignHandler); 
    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 = 1;
    ts.it_value.tv_nsec = 0;  
    printTime();
    printf("start\n");
    ret = timer_settime(timer, 0, &ts, NULL);  
    if(ret) {
        perror("timer_settime"); 
    } 
}

void printTime(){
    struct tm *cursystem;
    time_t tm_t;
    time(&tm_t);
    cursystem = localtime(&tm_t);
    char tszInfo[2048] ;
    sprintf(tszInfo, "%02d:%02d:%02d", 
        cursystem->tm_hour, 
        cursystem->tm_min, 
        cursystem->tm_sec);
        printf("[%s]",tszInfo);
}

void *function(void *arg){  
    char *m;  
    m = (char *)arg;  
    while(true) {
        while(true){
            int left = sleep(3);
            printTime();
            printf("sleep(3)(left=%d)\n", left);
            }
    }
}

这里写图片描述
可以看出,在主线程的定时器中的信号量SIGALRM是无法中断子线程thread1的休眠;

函数手册说明

查看函数手册中关于接口的使用:
1查看sigevent的说明

root@ubuntu:/home# man sigevent

NAME
       sigevent - structure for notification from asynchronous routines
       // 由异步线程通知

SYNOPSIS
       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) */
       };


DESCRIPTION
       The sigevent structure is used by various APIs 
       about an event (e.g., completion of an asynchronous request, 
       expiration of a timer, or the arrival of a message).
       // 结构体sigevent在各个API中用来描述线程中事件通知的方式

       The definition shown in the SYNOPSIS is approximate: 
       as part of a union.  Programs  should  only  employ  those
       fields relevant to the value specified in sigev_notify.
       // sigevent结构体中的一些字段会被定义为共同体的一部分

       The sigev_notify field specifies how notification is to be performed
       .  This field can have one of the following values:

CONFORMING TO
       POSIX.1-2001.

SEE ALSO
       timer_create(2),...

这里建议查看timer_create(2)的使用说明

root@ubuntu:/home# man sigevent

NAME
       sigevent - structure for notification from asynchronous routines
    // 由异步线程通知

SYNOPSIS
       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) */
       };


DESCRIPTION
       The sigevent structure is used by various APIs to describe the way 
       a process is to be notified about an event 
       (e.g., completion of an asynchronous request, expiration of a
       timer, or the arrival of a message).
       // 结构体sigevent在各个API中用来描述线程中事件通知的方式

       The definition shown in the SYNOPSIS is approximate: 
       some of the fields in the sigevent structure may be defined as part of a union.  
       Programs  should  only  employ  those  fields 
       relevant to the value specified in sigev_notify.
       // sigevent结构体中的一些字段会被定义为共同体的一部分

       The sigev_notify field specifies how notification is to be performed.  
       This field can have one of the following values:

CONFORMING TO
       POSIX.1-2001.

SEE ALSO
       timer_create(2),...



这里建议查看timer_create(2)的使用说明

root@ubuntu:/home# man 2 timer_create

NAME
       timer_create - create a POSIX per-process timer

SYNOPSIS
       #include <signal.h>
       #include <time.h>

       int timer_create(clockid_t clockid, struct sigevent *sevp,
                        timer_t *timerid);

       Link with -lrt.

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       timer_create(): _POSIX_C_SOURCE >= 199309L

DESCRIPTION
       timer_create()  creates a new per-process interval timer.
       // 创建一个新的计时器 
       The ID of the new timer is returned in the buffer pointed to by timerid, 
       // 返回新的计时器所指向的缓存区的指针
       which must be a non-NULL pointer.  
       This ID is unique within the process, until the timer is deleted.  
       // ID是唯一值,直到计时器销毁
       The new timer is initially disarmed.
       // 新的计时器初始默认状态为安静状态

RETURN VALUE
       On success, timer_create() returns 0, 
       and the ID of the new timer is placed in *timerid.  
       On failure, -1 is returned, and errno is set to indicate the error.

VERSIONS
       This system call is available since Linux 2.6.
       // 在Linux 2.6 版本之后使用

SEE ALSO
       clock_gettime(2),  setitimer(2),  timer_delete(2),  timer_settime(2), 
       pthreads(7),  sigevent(7), signal(7), time(7) .......

EXAMPLE
    Program Source 
    // 使用timer_create API编写的demo源码

在timer_create说明有:
头文件:
signal.h
time.h

编译说明:
Link with -lrt.

推荐一种简单的计时器

来自http://www.cnblogs.com/wenqiang/p/5525261.html

//timercreate_demo.cpp
#include <stdio.h>  
#include <signal.h>  
#include <unistd.h>

void timer(int sig)  {  
        if(SIGALRM == sig){  
                printf("timer\n");  
                alarm(1);
        }
        return ; 
}  

int main()  {  
        signal(SIGALRM, timer);
        alarm(1); //trigger the timer  
        getchar();  
        return 0;  
}  
timer_create是POSIX中的一个函数,用于创建定时器。它的原型是: int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timer); 其中,clockid指定了定时器使用的时钟类型,sevp是一个指向sigevent结构的指针,用于指定定时器的属性和回调函数,timer是一个指向timer_t类型的指针,用于存储创建的定时器的标识符。 在使用timer_create函数创建定时器时,需要指定一个回调函数,该函数会在定时器到期时被调用。可以通过设置sigevent结构的sigev_notify_function字段来指定回调函数。 例如,可以使用timer_create函数创建一个定时器,并将自定义的回调函数注册为SIGUSR2信号的处理函数,这样就可以在定时器到期时执行回调函数,而不会中断sleep操作。具体的代码示例可以参考引用\[1\]中的代码。 总结起来,timer_create函数是用于创建定时器的函数,可以通过设置回调函数来实现定时器到期时的操作。 #### 引用[.reference_title] - *1* [使用SIGALRM信号量和sleep冲突的解决办法](https://blog.csdn.net/littlefang/article/details/6830724)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Timer定时器 GNU linux](https://blog.csdn.net/u012294613/article/details/127730531)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值