中国十大热门网站,深圳龙华区高峰社区,台州关键词优化服务,宠物店网站建设策划书一、Linux 内核定时器介绍 在 Linux 内核中很多函数是基于定时器进行驱动的#xff0c;但是内核定时器的精度并不高#xff0c;所以不能作为高精度定时器使用。并且内核定时器的运行没有周期性#xff0c;到达计时终点后会自动关闭。如果要实现周期性定时#xff0c;就要在…一、Linux 内核定时器介绍 在 Linux 内核中很多函数是基于定时器进行驱动的但是内核定时器的精度并不高所以不能作为高精度定时器使用。并且内核定时器的运行没有周期性到达计时终点后会自动关闭。如果要实现周期性定时就要在定时处理函数中重新开启定时器。 Linux 内核中使用 timer_list 结构体表示内核定时器该结构体定义在“内核源码/include/linux/timer.h”文件中具体内容如下所示
struct timer_list {/** All fields that change during normal runtime grouped to the* same cacheline*/struct hlist_node entry;unsigned long expires;void (*function)(struct timer_list *);u32 flags;#ifdef CONFIG_LOCKDEPstruct lockdep_map lockdep_map;
#endifANDROID_KABI_RESERVE(1);ANDROID_KABI_RESERVE(2);
};使用以下宏对 timer_list 结构体进行定义_name 为定义的结构体名称_function为定时处理函数该宏同样定义在文件 “内核源码/include/linux/timer.h”文件中如下所示
#define DEFINE_TIMER(_name, _function) \struct timer_list _name \__TIMER_INITIALIZER(_function, 0)例如可以使用以下代码对定时器和相应的定时处理函数进行定义
DEFINE_TIMER(timer_test,function_test);//定义一个定时器定时器定义完成之后还需要通过一系列的 API 函数来初始化此定时器部分函数说明如下
函数作用void add_timer(struct timer_list *timer)向 Linux 内核注册定时器使用add_timer 函数向内核注册定时器以后定时器就会开始运行int del_timer(struct timer_list * timer)删除一个定时器int mod_timer(struct timer_list *timer,unsigned long expires)修改定时值如果定时器还没 在使用 add_timer()函数向 Linux 内核注册定时器之前还需要设置定时时间定时时间由 timer_list 结构体中的 expires 参数所确定单位为节拍数。
二、代码示例
2.1、程序示例
#include linux/init.h
#include linux/module.h
#include linux/timer.hstatic void function_test(struct timer_list *t);//定义function_test定时功能函数
DEFINE_TIMER(timer_test,function_test);//定义一个定时器
static void function_test(struct timer_list *t)
{printk(this is function test \n);mod_timer(timer_test,jiffies_64 msecs_to_jiffies(5000));//使用mod_timer函数将定时时间设置为五秒后
}
static int __init timer_mod_init(void) //驱动入口函数
{timer_test.expires jiffies_64 msecs_to_jiffies(5000);//将定时时间设置为五秒后add_timer(timer_test);//添加一个定时器return 0;
}static void __exit timer_mod_exit(void) //驱动出口函数
{del_timer(timer_test);//删除一个定时器printk(module exit \n);
}
module_init(timer_mod_init);
module_exit(timer_mod_exit);2.2、Linux 内核定时器使用API要点
DEFINE_TIMER(timer_test,function_test);//定义一个定时器add_timer(timer_test);//添加一个定时器static void function_test(struct timer_list *t)
{printk(this is function test \n);mod_timer(timer_test,jiffies_64 msecs_to_jiffies(5000));//使用mod_timer函数将定时时间设置为五秒后
}