您的位置:首页 > 运维架构 > Linux

Linux下使用setitimer函数实现多个定时器的功能

2014-07-13 12:14 661 查看
在一些程序中,我们需要每隔一段时间执行一个函数。例如每2s,5s,10s分别执行不同的函数。如果有多个定时器,实现这个功能就很简单,只需分别定时2s,5s,10s即可。但是Linux中只允许一个进程中有一个定时器,怎么办呢?可以用以下的方法实现。

首先使用setitimer函数注册一个1s定时器one_timer,因为1s可以作为被2s,5s,10s整除的单位时间。Setitimer定时时间到达以后会产生SGIALRM信号,设置SGIALRM的处理函数为multi_timer_manage,定时时间一到便会执行multi_timer_manage函数。

定义一个timers结构体,存储两个变量。一个是定时时间,值为想要的时间除以单位时间;另一个是定时时间到时执行的函数。创建timers结构体two_timer, five_timer, ten_timer,并填写定时时间和需要执行的函数。

multi_timer_manage为实现多个定时器功能的核心部分,在multi_timer_manage中分别将one_timer, two_timer, ten_timer对应的定时时间减一,若为零,则执行对应的函数,并将定时时间复位到初始值重新开始定时。这样,就实现了多个定时器的功能,详细代码如下:

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<ctype.h>

#include<string.h>

#include<sys/time.h>

#include<signal.h>

struct itimerval one_timer;

struct timers

{

int interval; //定时时间

void(*handler)(); //处理函数

};

struct timers two_timer;

struct timers five_timer;

struct timers ten_timer;

void multi_timer_manage()

{

printf("\n---");

two_timer.interval--;

if(two_timer.interval==0)

{

two_timer.interval=2;

two_timer.handler();

}

five_timer.interval--;

if(five_timer.interval==0)

{

five_timer.interval=5;

five_timer.handler();

}

ten_timer.interval--;

if(ten_timer.interval==0)

{

ten_timer.interval=10;

ten_timer.handler();

}

}

void two_output()

{

printf("2 ");

}

void five_output()

{

printf("5 ");

}

void ten_output()

{

printf("10 ");

}

void init()

{

one_timer.it_interval.tv_sec=1; //设置单位定时器定时时间

one_timer.it_value.tv_sec=1; //设置单位定时器初始值

setitimer(ITIMER_REAL,&one_timer,NULL); //初始化单位定时器

signal(SIGALRM,multi_timer_manage); //指定单位定时器定时时间到时执行的函数

two_timer.interval=2;

two_timer.handler=two_output;

five_timer.interval=5;

five_timer.handler=five_output;

ten_timer.interval=10;

ten_timer.handler=ten_output;

}

int main()

{

init();

while(1);

}

执行结果如下:

---

---2

---

---2

---5

---2

---

---2

---

---2 5 10

---

---2

---

---2

---5

---2

---

---2

---

---2 5 10

......

可见,每秒都输出---,每隔2秒输出2,每隔5秒输出5,每隔10秒输出10。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: