以下摘自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. Android应用开发基础篇(10)-----Menu(菜单)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/28/2372101.html 一.概述 Menu,简单来理解就是当你按下手机的“menu”键时所 ...

  2. POJ 2689 Prime Distance(素数筛选)

    题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...

  3. Last Defence (run time error)

    Last Defence时间限制:1000 ms | 内存限制:65535 KB描述Given two integers A and B. Sequence S is defined as follo ...

  4. java代理课程测试 spring AOP代理简单测试

    jjava加强课程测试代码 反射. 代理 .泛型.beanUtils等 项目源码下载:http://download.csdn.net/detail/liangrui1988/6568169 热身运动 ...

  5. js限制图片的大小

    <form id="financialForm" action="<%=basePath%>riskcontrol/website/review_bor ...

  6. PHP学习(变量)

    PHP学习(变量) 1. PHP属于松散类型,创建变量时不用指定类型. 2.变量命名规范: 1)第一个字符必须是$ 2)$后的第一个字符必须是 字母 或 下划线 3)其他字符可以是 字母, 数字, 下 ...

  7. 9.PHP 教程_PHP运算符

    PHP 运算符 在 PHP 中,赋值运算符 = 用于给变量赋值. 在 PHP 中,算术运算符 + 用于把值加在一起. PHP 算术运算符 运算符 名称 描述 实例 结果 x + y 加 x 和 y 的 ...

  8. [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: ...

  9. Oracle10g--plSql命令

    每天学点Oracle10g--plSql命令 附录B SQL*PLUS Sql*plus 中使用绑定变量: sql> variable x number; sql> exec :x := ...

  10. mybatis+postgresql平台

    mybatis+postgresql平台        最近有个项目的数据库使用postgresql,使用原生态的mybatis操作数据,原生态的没什么不好,只不过国内有个tk.mybatis的工具帮 ...