linux 内核定时器解析

来源:互联网 发布:重复照片整理软件 编辑:程序博客网 时间:2024/06/11 19:50

1.linux内核定时器基本结构和函数

1)struct timer_list 一个struct timer_list对应了一个定时器。
#include <linux/timer.h>
以下列出常用的接口:
struct timer_list
  {
   /*....*/
   unsigned long expires;//定时器服务函数开始执行时间
   void (*function)(unsigned long);//定义一个指向定时器服务函数的指针function,服务函数有一个 unsigned long的参数,并且返回void
   unsigned long data;//定时时间到时,data参数会传入服务函数
  }
 
void init_timer(struct timer_list* timer)//初始化一个定时器

-----------使用定时器的步骤--------------
struct timer_list  my_timer_list;//定义一个定时器,可以把它放在你的设备结构中
init_timer(&my_timer_list);//初始化一个定时器
my_timer_list.expire=jiffies+HZ;//定时器1s后运行服务程序
my_timer_list.function=timer_function;//定时器服务函数
add_timer(&my_timer_list);//添加定时器
void timer_function(unsigned long)//写定时器服务函数
del_timer(&my_timer_list);//当定时器不再需要时删除定时器
del_timer_sync(&my_timer_list);//基本和del_timer一样,比较适合在多核处理器使用,一般推荐使用del_timer_sync。
原创粉丝点击