• Linux下的定时器


    以下摘自linux下的man文件:(man  getitimer)

      #include  <sys/time.h>

      int  getitimer(int which,  struct itimerval * curr_value);

      int  setitimer(int which,  const struct itimerval  * new_value, struct itimerval  * old_value);

    描述:

      Linux系统中提供为每个进程提供了三个间隔定时器,在不同的时间域的每一个递减。 

           当任何定时器超时,则发送一个信号到该过程,并在定时器(可能)重新启动。

      ITIMER_REAL: 实时递减,时间到发送SIGALRM信号;

      ITIMER_VIRTUAL:递减只有当进程正在执行,并且期满后可提供SIGVTALRM

      ITIMER_PROF: 当进程执行或者是系统为进程调度的时候,减少计数,时间到了,发出SIGPROF信号,这个和ITIMER_VIRTUAL联合,常用来计算系统内核时间和用户时间。

    以下结果体中定义了定时器的值:

      struct itimerval

      {

        struct timerval it_interval;  //next value;

        struct timerval it_value;   //current value;

      };

      struct timeval

      {

        long tv_sec;   //seconds; 

        long tv_usec;  //microseconds;

      }

    it_interval用来指定每隔多长时间执行任务, it_value用来保存当前时间离执行任务还有多长时间。比如说, 你指定it_interval为2秒(微秒为0),开始的时候我们把it_value的时间也设定为2秒(微秒为0),当过了一秒, it_value就减少一个为1, 再过1秒,则it_value又减少1,变为0,这个时候发出信号(告诉用户时间到了,可以执行任务了),并且系统自动把it_value的时间重置为 it_interval的值,即2秒,再重新计数。

     1 #include <sys/time.h>
     2 #include <stdio.h>
     3 #include <unistd.h>
     4 #include <signal.h>
     5 #include <string.h>
     6 
    10 //收到信号的回调函数
    11 void prompt_info(int signo)
    12 {
    13     printf("hello world
    ");
    14 }
    15 
    16 //建立信号处理机制
    17 void init_sigaction()
    18 {
    19     struct sigaction tact;
    20     //本进程接收到信号要执行的处理函数prompt_info
    21     tact.sa_handler = prompt_info;
    22     tact.sa_flags   = 0;
    23 
    24     //初始化信号集
    25     sigemptyset(&tact.sa_mask);
    26 
    27     //建立信号处理机制
    28     sigaction(SIGALRM, &tact, NULL);
    29 }
    30 
    31 void init_time()
    32 {
    33     //设定执行任务的时间间隔为2秒0微秒
    34     struct itimerval value;
    35     value.it_value.tv_sec  = 2;
    36     value.it_value.tv_usec = 0;
    37 
    38     //设定初始时间计数也为2秒0微秒
    39     value.it_interval = value.it_value;
    40     
    41     //设置计时器ITIMER_REAL
    42     setitimer(ITIMER_REAL, &value, NULL);
    43 }
    44 
    45 int main()
    46 {48     init_sigaction();
    49 
    50     init_time();
    51     while(1)
    52         ;
    53 
    54     return 0;
    55 }

    使用ITMER_REAL定时器,每隔2秒钟都会发送一个SIGALRM信号,当主函数接收到了这个信号之后,调用信号处理函数 prompt_info在输出time is running out这个字符串。

    对于ITIMER_VIRTUAL和ITIMER_PROF的使用方法类似,在setitimer里面设置的定时器为 ITIMER_VIRTUAL的时候,并把sigaction里面的SIGALRM改为SIGVTALARM,

    而ITIMER_PROF对应SIGPROF。

     1 /*
     2  *通过自己计算时间差的方法来定时
     3  *获得精确计算时间差,把time 函数换成gettimeofday
     4  */
     5 
     6 #include <signal.h>
     7 #include <string.h>
     8 #include <unistd.h>
     9 #include <time.h>
    10 #include <stdio.h>
    11 
    12 static time_t lasttime;
    13 
    14 void show_msg(int signo)
    15 {
    16     printf("hello world
    ");
    17 }
    18 
    19 int main()
    20 {
    21     struct sigaction act;
    22     union sigval tsval;
    23     act.sa_handler = show_msg;
    24     act.sa_flags = 0;
    25         
    26     sigemptyset(&act.sa_mask);
    27     sigaction(50, &act, NULL);
    28     time(&lasttime);
    29         
    30     while(1)
    31     {   
    32         time_t nowtime;
    33         //获取当前时间
    34         time(&nowtime);
    35 
    36         if(nowtime-lasttime>=2)
    37         {
    38             sigqueue(getpid(), 50, tsval);
    39             lasttime = nowtime;
    40         }
    41     }
    42 
    43     return 0;
    44 }
  • 相关阅读:
    VOL.1 利用vmware ThinApp 制作非XP下可以运行的IE6 【无插件版】(windows vista/7/8 x86/x64 )
    js中each的用法
    Math函数floor、round、ceil的区别
    JS中的prototype解析
    Ubuntu虚拟机搭建
    我的第一篇blog
    实验四+124+高小娟
    实验三+124+高小娟
    第5次作业+105032014124+高小娟
    第4次作业类测试代码+105032014124+高小娟
  • 原文地址:https://www.cnblogs.com/chris-cp/p/3894450.html
Copyright © 2020-2023  润新知