手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux 定时器编程

[日期:2011-08-30] 来源:Linux社区  作者:lickylin [字体:大 中 小]

二、内核定时器

用到的数据结构

struct timer_list {

struct list_head entry;

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

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 <linux/list.h> 

#include <linux/delay.h> 

#include<linux/timer.h>

#include<linux/jiffies.h>

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;

...

}

linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款