新闻  |   论坛  |   博客  |   在线研讨会
setitimer函数使用笔记
电子禅石 | 2021-11-16 17:41:05    阅读:16446   发布文章

转载

setitimer函数使用笔记

函数原型int setitimer(int which, const struct itimerval *value,
                     struct itimerval *ovalue);

DESCRIPTION
       The  system  provides each process with three interval timers, each decrementing in a distinct time domain.  When any timer expires, a
       signal is sent to the process, and the timer (potentially) restarts.

       ITIMER_REAL    decrements in real time, and delivers SIGALRM upon expiration.

       ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.

       ITIMER_PROF    decrements both when the process executes and when the system is executing on behalf  of  the  process.   Coupled  with
                      ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application in user and kernel space.  SIG-
                      PROF is delivered upon expiration.

       Timer values are defined by the following structures:

            struct itimerval {
                struct timeval it_interval; /* next value */
                struct timeval it_value;    /* current value */
            };
            struct timeval {
                long tv_sec;                /* seconds */
                long tv_usec;               /* microseconds */
            };

#include <stdio.h>
#include 
<unistd.h>
#include 
<signal.h>
#include 
<string.h>
#include 
<sys/time.h>
#include 
<errno.h>

void PrintMsg(int Num)
{
    printf(
"%s\n""Hello World");

    
return;
}

int main(int argc, char* argv[])
{
    signal(SIGALRM, PrintMsg);

    
struct itimerval tick;
    tick.it_value.tv_sec 
= 10;  //十秒钟后将启动定时器
    tick.it_value.tv_usec = 0;
    tick.it_interval.tv_sec  
=1//定时器启动后,每隔1秒将执行相应的函数
    tick.it_interval.tv_usec = 0;

    
//setitimer将触发SIGALRM信号
    int ret = setitimer(ITIMER_REAL, &tick, NULL);

    
if ( ret != 0)
    {
        printf(
"Set timer error. %s \n", strerror(errno) );

        
return -1;
    }

    printf(
"Wait!\n");

    getchar();

    
return 0;
}

加群:1732700158


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
推荐文章
最近访客