Linux定时器

定时器在内核的定义:

12 struct timer_list {
13 /*
14 * All fields that change during normal runtime grouped to the
15 * same cacheline
16 */
17 struct list_head entry; //定时器的链表
18 unsigned long expires;//以节拍为单位的定时时间,表示为定时器触发的到期时间
19 struct tvec_base *base;
20
21 void (*function)(unsigned long); //该指针指向定时器处理函数,函数参数为长整形
22 unsigned long data; //处理函数的参数值
23
24 int slack;
25
26 #ifdef CONFIG_TIMER_STATS
27 void *start_site;
28 char start_comm[16];
29 int start_pid;
30 #endif
31 #ifdef CONFIG_LOCKDEP
32 struct lockdep_map lockdep_map;
33 #endif
34 };

使用定时器的步骤:
1) 定义定时器:

struct timer_list my_timer

2)初始化定时器:

初始化定时器的到期节拍数 my_timer.expires = jiffies +delay ;该设置让定时器的触发时间设置为 激活定时器后的delay个节拍点

my_timer.function = 处理函数的名称 该设置设置定时器触发时处理的函数

my_timer.data 初始化处理函数的参数值,若处理函数没有参数则可简单设置为0或任意其他数值

3)激活定时器:即内核会开始定时,直到my_timer.expires

使用函数add_timer 即 add_timer(&my_timer);

内核原型为:

849 void add_timer(struct timer_list *timer)
850 {
851 BUG_ON(timer_pending(timer));
852 mod_timer(timer, timer->expires); //该函数设置定时器timer的定时时间为timer->expires;
853 }

4)删除定时器:如果需要在定时器到期之前停止定时器,则可以使用该函数,若是定时器已经过期则不需调用该函数,因为它们会自动删除

del_timer(&my_timer);

定时器的简单实例:该例子的功能是首先初始化一个定时器,当定时器时间到后触发定时器出俩函数的执行,该函数又重新设置了该定时器的时间,即该定时器又在下一次定时时间的到来继续处理函数,一直循环,知道最后在该模块卸载时进行删除定时器,结束该定时器

代码中 HZ为内核每一秒的节拍数,是通过宏进行定义的,通过该程序的打印结果可以得到,本人电脑的节拍数测试结果为250

#include< linux/module.h >
#include< linux/init.h >
#include< linux/sched.h >
#include < linux/timer.h >
#include < linux/kernel.h >
struct timer_list stimer; //定义定时器
static void time_handler(unsigned long data){ //定时器处理函数
mod_timer(&stimer, jiffies + HZ);
printk(“current jiffies is %ld\n”, jiffies);
}
static int __init timer_init(void){ //定时器初始化过程
printk(“My module worked!\n”);
init_timer(&stimer);
stimer.data = 0;
stimer.expires = jiffies + HZ; //设置到期时间
stimer.function = time_handler;
add_timer(&stimer);
return 0;
}
static void __exit timer_exit(void){
printk(“Unloading my module.\n”);
del_timer(&stimer);//删除定时器
return;
}
module_init(timer_init);//加载模块
module_exit(timer_exit);//卸载模块
MODULE_AUTHOR(“fyf”);
MODULE_LICENSE(“GPL”);

加载/ 卸载该程序后通过命令dmesg可以看到

[ 6225.522208] My module worked!
[ 6226.520014] current jiffies is 1481630
[ 6227.520014] current jiffies is 1481880
[ 6228.520013] current jiffies is 1482130
[ 6229.520011] current jiffies is 1482380
[ 6229.770335] Unloading my module.
即每2次的jiffies之差为250

定时器的应用:以下是一个简单的延迟当前进程执行的程序,延迟是通过定时器来实现的;

#include< linux/module.h >
#include< linux/init.h >
#include< linux/sched.h >
#include < linux/timer.h >
#include < linux/kernel.h >
struct timer_list stimer; //定义定时器
int timeout = 10 * HZ;
static void time_handler(unsigned long data){ //定时器处理函数,执行该函数获取挂起进程的pid,唤醒该进程
struct task_struct *p = (struct task_struct *)data;//参数为挂起进程pid
wake_up_process(p);//唤醒进程
printk(“current jiffies is %ld\n”, jiffies); //打印当前jiffies
}
static int __init timer_init(void){ //定时器初始化过程
printk(“My module worked!\n”);
init_timer(&stimer);
stimer.data = (unsigned long)current; //将当前进程的pid作为参数传递
stimer.expires = jiffies + timeout; //设置到期时间
stimer.function = time_handler;
add_timer(&stimer);
printk(“current jiffies is %ld\n”, jiffies);
set_current_state(TASK_INTERRUPTIBLE);
schedule(); //挂起该进程
del_timer(&stimer); //删除定时器
return 0;
}
static void __exit timer_exit(void){
printk(“Unloading my module.\n”);
return;
}
module_init(timer_init);//加载模块
module_exit(timer_exit);//卸载模块
MODULE_AUTHOR(“fyf”);
MODULE_LICENSE(“GPL”);

运行结果:

[ 9850.099121] My module worked!
[ 9850.099127] current jiffies is 2387524
[ 9860.128017] current jiffies is 2390032
[ 9869.135805] Unloading my module.

打印结果与定时时间2500有一点差距,是因为打印时第一次的jiffies实在add_timer之后打印的,故不是定时器激发时的jiffies,第二次同理,所以结果不是确定的,但都于2500相差不多


使用时注意:
LINUX内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,其实现位于 <linux/timer.h> 和 kernel/timer.c 文件中。
被调度的函数肯定是异步执行的,它类似于一种“软件中断”,而且是处于非进程的上下文中,所以调度函数必须遵守以下规则:
1) 没有 current 指针、不允许访问用户空间。因为没有进程上下文,相关代码和被中断的进程没有任何联系。
2) 不能执行休眠(或可能引起休眠的函数)和调度。
3) 任何被访问的数据结构都应该针对并发访问进行保护,以防止竞争条件。

以上为原理相关的,为了不至于看不懂变化的使用代码,列举出使用的常见方法

方法一:

DEFINE_TIMER(timer_name, function_name, expires_value, data);

该宏会静态创建一个名叫 timer_name 内核定时器,并初始化其 function, expires, name 和 base 字段。

方法二:

struct timer_list mytimer;
setup_timer(&mytimer, (*function)(unsigned long), unsigned long data);
mytimer.expires = jiffies + 5*HZ;

方法三:

struct timer_list mytimer;
init_timer(&mytimer);    
mytimer ->timer.expires = jiffies + 5*HZ;
mytimer ->timer.data = (unsigned long) dev;
mytimer ->timer.function = &corkscrew_timer; /* timer handler */

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值