以下摘自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. UVa1586 Molar mass

    #include <stdio.h> int GetQuantity(char* q, char** p){    int quantity = 0;    while (*q & ...

  2. A Byte of Python 笔记(4)控制流:if、for、while、break、continue

    第6章  控制流 3种控制流语句-- if  for  while 默认pyhon使用ASCII码来解释程序的,默认不支持中文,需要在程序的第一行或者第二行声明编码.官方参考具体参考以下三种方式:1. ...

  3. [LeetCode]题解(python):129-Sum Root to Leaf Numbers

    题目来源: https://leetcode.com/problems/sum-root-to-leaf-numbers/ 题意分析: 一棵树,从跟节点到叶子节点比如1->2那么这个叶子代表12 ...

  4. [LeetCode]题解(python):075-Sort Colors

    题目来源: https://leetcode.com/problems/sort-colors/ 题意分析: 给定n个颜色,红色,白色和蓝色.分别用0,1,2代替,将这些颜色排序,0在1前,1在2前. ...

  5. 自定义Chrome 背景色 (FF需要User Styles插件)

    Chrome有个自定义背景色的文件  Custom.css 默认里面什么字都没写 html, body {background-color: #e0dcc0!important;}      这个颜色 ...

  6. 基于Visual C++2013拆解世界五百强面试题--题1-定义各种类型指针

    用变量a给出下面的定义    a)一个整型数    b)一个指向整型数的指针    c)一个指向指针的指针,它指向的指针是指向一个整型数    d)一个有10个整型数的数组    e)一个有10个指针 ...

  7. JAVA GUI学习 - JTable表格组件学习_A ***

    public class JTableKnow_A extends JFrame { public JTableKnow_A() { this.setBounds(300, 100, 400, 300 ...

  8. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  9. HDU4712+随机算法

    随机算法 求n个20位的2进制串的MinDist. Dist:两个串的异或结果中1的个数 /* 随机算法 */ #include<algorithm> #include<iostre ...

  10. rman 使用catalog备份的演示

    介绍了如何使用catalog方式做RMAN备份,以及如何取消以catalog方式做备份. 第一步:创建RMAN CATALOG表空间及用户. [oracle@oel-01 ~]$ sqlplus / ...