以下摘自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秒,再重新计数。

 #include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h> //收到信号的回调函数
void prompt_info(int signo)
{
printf("hello world\n");
} //建立信号处理机制
void init_sigaction()
{
struct sigaction tact;
//本进程接收到信号要执行的处理函数prompt_info
tact.sa_handler = prompt_info;
tact.sa_flags = ; //初始化信号集
sigemptyset(&tact.sa_mask); //建立信号处理机制
sigaction(SIGALRM, &tact, NULL);
} void init_time()
{
//设定执行任务的时间间隔为2秒0微秒
struct itimerval value;
value.it_value.tv_sec = ;
value.it_value.tv_usec = ; //设定初始时间计数也为2秒0微秒
value.it_interval = value.it_value; //设置计时器ITIMER_REAL
setitimer(ITIMER_REAL, &value, NULL);
} int main()
{ init_sigaction(); init_time();
while()
; return ;
}

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

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

而ITIMER_PROF对应SIGPROF。

 /*
*通过自己计算时间差的方法来定时
*获得精确计算时间差,把time 函数换成gettimeofday
*/ #include <signal.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h> static time_t lasttime; void show_msg(int signo)
{
printf("hello world\n");
} int main()
{
struct sigaction act;
union sigval tsval;
act.sa_handler = show_msg;
act.sa_flags = ; sigemptyset(&act.sa_mask);
sigaction(, &act, NULL);
time(&lasttime); while()
{
time_t nowtime;
//获取当前时间
time(&nowtime); if(nowtime-lasttime>=)
{
sigqueue(getpid(), , tsval);
lasttime = nowtime;
}
} return ;
}

Linux下的定时器的更多相关文章

  1. Linux下的定时器:alarm()与setitimer()

    Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...

  2. Linux下的定时器类实现(select定时+线程)

    更好的计时器类实现:LINUX RTC机制实现计时器类(原创) 很多时候需要在LINUX下用到定时器,但像setitimer()和alarm()这样的定时器有时会和sleep()函数发生冲突,这样就给 ...

  3. linux下jiffies定时器和hrtimer高精度定时器【转】

    本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...

  4. linux下的“定时器”:crontab

    1.概述 crontab是用来设置在固定时间点或时间间隔执行某条指令,类似于时程表.使用-u user是指定user用户的时程表. 2.参数 -e[UserName] :调出编辑器,编辑定时任务,打开 ...

  5. Linux下实现定时器Timer的几种方法

    http://blog.csdn.net/lxmky/article/details/7669296 第六章 IO复用:select和poll函数 http://www.cnblogs.com/4ti ...

  6. Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...

  7. Linux下定时器

    http://unix8.net/linux%E4%B8%8B%E5%AE%9A%E6%97%B6%E5%99%A8.html 一. 基础知识 1.时间类型.Linux下常用的时间类型有4个:time ...

  8. Linux下一种高效多定时器实现

    Linux下一种高效多定时器实现 作者:LouisozZ 日期:2018.08.29 运行环境说明 由于在 Linux 系统下一个进程只能设置一个时钟定时器,所以当应用需要有多个定时器来共同管理程序运 ...

  9. linux下定时器的实现

    简介: linux下经常有这样的需求,需要定时轮询执行某种任务,当然,用shell脚本的话,crontab和at就可以满足要求.如果从C语言的角度来看,实现定时器也是一个比较简单的任务,因为具有普遍性 ...

随机推荐

  1. 如何交叉编译开源库-->编译c-ares库从失败到成功的过程[ocean]

    编译c-ares库从失败到成功的过程c-ares-master: apt-get install libtool   ####https://github.com/c-ares/c-ares ==== ...

  2. 求新的集合 A=AUB(顺序表)

    #include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCREMENT=10; typ ...

  3. Struts学习之值栈的理解

    转自:http://blog.csdn.net/hanxuemin12345/article/details/38559979 页面一个请求发送过来,依次经过一系列拦截器(处理公共部分,如:往数据中心 ...

  4. jQuery.validate 中文 API

    名称 返回类型 描述 validate(options) Validator 验证所选的 FORM. valid() Boolean 检查是否验证通过. rules() Options 返回元素的验证 ...

  5. 简单的UIScrollView 下拉刷新

    这里只贴主要代码 #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate&g ...

  6. c#的Marshal

    补充过程中~ 感觉应该是C#调用非托管的比较专门的class 例1. public struct ImageDataMsg { public char DataType; public int Srv ...

  7. Java 多线程 socket 取款例子 runnable callable

    socket部分参考 http://blog.csdn.net/kongxx/article/details/7259465 取款部分参考 http://blog.csdn.net/dayday198 ...

  8. ORA-20000: ORU-10027: buffer overflow, limit of 10000 bytes

        要用dbms_output.put_line来输出语句,遇到以下错误: ERROR 位于第 1 行: ORA-20000: ORU-10027: buffer overflow, limit ...

  9. 帝国cms7.0调用出栏目下的东西

    打开帝国后台,新建一个栏目,简历一个封面模板为 abc,套用一个封面栏目. [e:loop={"select * from {$dbtbpre}enewsclass where classi ...

  10. my.ini配置

    在家里写点东西,需要配置mysql windows版本,linux版本我一般都是直接编译后,有模板文件可以编辑,新下载的5.6没有,我用的是zip的. 在网上找了两篇博客,写的很详细: http:// ...