Chinaunix首页 | 论坛 | 博客
  • 博客访问: 338254
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 562
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-14 13:32
个人简介

先知者为师

文章分类

全部博文(46)

文章存档

2016年(1)

2015年(6)

2014年(20)

2013年(19)

我的朋友

分类: C/C++

2013-10-17 13:31:19

一、linux应用程序定时器

用到的数据结构为:

           struct itimerval {

               struct timeval it_interval; /* next value */

               struct timeval it_value;    /* current value */

           };

其中it_value用于设置第一次定时器到达并发送信号的时间,it_interval用于设置定时器终止后的重设值.若为0则定时器在终止后将失效;若不为0,则定时器终止后,将会以该值进行重新定时。

           struct timeval {

               long tv_sec;                /* seconds */

               long tv_usec;               /* microseconds */

           };

int setitimer(int which, const struct itimerval *new_value,

                     struct itimerval *old_value);

设置定时器间隔,其中第一个变量which的取值如下:

ITIMER_REAL :以系统真实的时间来计算,它送出SIGALRM信号。

ITIMER_VIRTUAL:以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号。

ITIMER_PROF :以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF   

号。

#include 

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);

该函数用于设置某一信号的对应处理函数,对定时器来说信号有:SIGALRM、SIGVTALRM、SIGPROF; handler用于设置某一信号到来时的处理函数。所在头文件signal.h



下面是示例程序

#include 

#include 

#include 

#include 

#include 

/*设置定时器到达时间*/

#define REFRESH_TIME 10

/*设置定时器*/

void set_refresh_time(int sec)

{

        struct itimerval nvalue;

        nvalue.it_value.tv_sec = sec;

        nvalue.it_value.tv_usec = 0;

     nvalue.it_interval.tv_sec = sec;//每隔sec时间触发一次定时器

        nvalue.it_interval.tv_usec = 0;

/*此处使用的是ITIMER_REAL,所以对应的是SIGALRM信号*/

        setitimer(ITIMER_REAL, &nvalue, NULL);

}

/*定时器处理函数*/

void timer_handler(int msg)

{

        switch(msg)

        {

                case SIGALRM:

                        printf("to do someting when set timer is comming\n");

                        break;

                default:

                        break;

        }

        return ;

}

int main(int argc, char **argv)

{

int value;

/*设置SIGALRM信号的处理函数,即定时器处理函数*/

        signal(SIGALRM, timer_handler);

if(!argv[1])

{

Printf("use default time %d\n", REFRESH_TIME);

/*设置定时器*/

         set_refresh_time(REFRESH_TIME);

}

else

{

value = atoi(argv[1]);

set_refresh_time(value);

}

        while(1)

        {

        }

}

二、内核定时器

用到的数据结构

struct timer_list {

struct list_head entry;

unsigned long expires;//定时器时间设置一般为jiffies + n(0

void (*function)(unsigned long);//定时器处理函数

unsigned long data;//定时器私有数据

struct tvec_base *base;

#ifdef CONFIG_TIMER_STATS

void *start_site;

char start_comm[16];

int start_pid;

#endif

#ifdef CONFIG_LOCKDEP

struct lockdep_map lockdep_map;

#endif

};

用到的函数:

#define init_timer(timer) init_timer_key((timer), NULL, NULL)

void init_timer_key(struct timer_list *timer,

    const char *name,

    struct lock_class_key *key)

{

debug_init(timer);

__init_timer(timer, name, key);

}

初始化一个定时器

void add_timer(struct timer_list *timer)

{

BUG_ON(timer_pending(timer));

mod_timer(timer, timer->expires);

}

该函数用于添加一个定时器

代码如下:

#include  

#include  

#include

#include

strcut timer_list timed;

static void exec_timer(unsigned long arg)

{

printf("do someting else\n");

...

timed.expires = jiffies + 128;

add_timer(&timed);//再次添加一个定时器

}

static int xx_init()

{

...

init_timer(&time);

timed.function = exec_timer;

timed.expires = jiffies + 128;

timed.data = (unsigned long)xx_datap;


add_timer(&timed)



..

.


}

阅读(1829) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~