linux编程 定时器,Linux 定时器编程

二、内核定时器

用到的数据结构

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;

...

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值