(十二)linux内核定时器

(一)内核定时器介绍

内核定时器并不是用来简单的定时操作,而是在定时时间后,触发事件的操作,类似定时器中断,是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,内核中采用的定时器以jiffies为单位。 单位秒=jiffies/HZ

几个重要跟时间有关的名词或变数

HZ:Linux核心每隔固定周期会发出timer interrupt (IRQ 0),HZ是用来定义每一秒有几次
     timer interrupts。举例来说,HZ为1000,代表每秒有1000次timer interrupts。
Tick:Tick是HZ的倒数,意即timer interrupt每发生一次中断的时间。如HZ为250时,
      tick为4毫秒(millisecond)。
Jiffies:Jiffies为Linux核心变数(unsigned long),它被用来记录系统自开机以来,已经过
         了多少tick。每发生一次timer interrupt,Jiffies变数会被加一。值得注意的是,
         Jiffies于系统开机时,并非初始化成零,而是被设为-300*HZ ,类似Linux系统
         中time(日历时间)
(二)内核定时器相关接口

和定时器先关的数据结构:

struct timer_list {
	unsigned long expires;	//未来时间点,即超时时间
	void (*function)(unsigned long);//超时回调函数
	unsigned long data;	//传递给回调函数的数据,也就是定时器数据		
};

初始化定时器相关的数据结构

1. 静态定以定时器数据结构:
#define DEFINE_TIMER(_name, _function, _expires, _data)		\
	struct timer_list _name =				\
		TIMER_INITIALIZER(_function, _expires, _data)

2. 动态初始化:-----手动对变量进行初始化
#define init_timer(timer)						\
	do {								\
		static struct lock_class_key __key;			\
		init_timer_key((timer), #timer, &__key);		\
	} while (0)

可延时定时:

#define init_timer_deferrable(timer)					\
	do {								\
		static struct lock_class_key __key;			\
		init_timer_deferrable_key((timer), #timer, &__key);	\
	} while (0)

设置定时时间

#define setup_timer(timer, fn, data)					\
do {								\
    static struct lock_class_key __key;			\
    setup_timer_key((timer), #timer, &__key, (fn), (data));\
} while (0)

向内核添加定时器:

/**
 * add_timer - start a timer
 * @timer: the timer to be added
 *
 * The kernel will do a ->function(->data) callback from the
 * timer interrupt at the ->expires point in the future. The
 * current time is 'jiffies'.
 *
 * The timer's ->expires, ->function (and if the handler uses it, ->data)
 * fields must be set prior calling this function.
 *
 * Timers with an ->expires field in the past will be executed in the next
 * timer tick.
 */
void add_timer(struct timer_list *timer)
{
	BUG_ON(timer_pending(timer));
	mod_timer(timer, timer->expires);
}

从内核删除定时器:

/**
 * del_timer - deactive a timer.
 * @timer: the timer to be deactivated
 *
 * del_timer() deactivates a timer - this works on both active and inactive
 * timers.
 *
 * The function returns whether it has deactivated a pending timer or not.
 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
 * active timer returns 1.)
 */
int del_timer(struct timer_list *timer)

修改定时时间:

/**
 * mod_timer - modify a timer's timeout
 * @timer: the timer to be modified
 * @expires: new timeout in jiffies
 *
 * mod_timer() is a more efficient way to update the expire field of an
 * active timer (if the timer is inactive it will be activated)
 *
 * mod_timer(timer, expires) is equivalent to:
 *
 *     del_timer(timer); timer->expires = expires; add_timer(timer);
 *
 * Note that if there are multiple unserialized concurrent users of the
 * same timer, then mod_timer() is the only safe way to modify the timeout,
 * since add_timer() cannot modify an already running timer.
 *
 * The function returns whether it has modified a pending timer or not.
 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
 * active timer returns 1.)
 */
int mod_timer(struct timer_list *timer, unsigned long expires)

jiffies和时间的转换:

extern unsigned int jiffies_to_msecs(const unsigned long j);
extern unsigned int jiffies_to_usecs(const unsigned long j);
extern unsigned long msecs_to_jiffies(const unsigned int m);
extern unsigned long usecs_to_jiffies(const unsigned int u);
(三)使用步骤

1、向内核添加定时器

   setup_timer();设置定时器
   add_timer();.

2、解绑定时器

   int del_timer(struct timer_list *timer)
(四)实例代码
#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/module.h>

struct timer_list timer;
void function(unsigned long data)
{
  static int count=0;
  printk("this is timer test:%d\n",count++);
  mod_timer(&timer,jiffies+1*HZ);
}
static int __init timer_module_init(void)
{
  timer.expires = jiffies+5*HZ;
  setup_timer(&timer,function,0);
  add_timer(&timer);
  return 0;
}
static void __exit timer_module_cleanup(void)
{
  del_timer(&timer);
}

module_init(timer_module_init);
module_exit(timer_module_cleanup);
MODULE_LICENSE("GPL");

测试


本文章仅供学习交流用禁止用作商业用途,文中内容来水枂编辑,如需转载请告知,谢谢合作

微信公众号:zhjj0729

微博:文艺to青年

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值