Linux内核中提供了timer使用的API,做一个简单的记要。
1. 包含的头文件:linux/timer.h
2. 数据类型:struct timer_list;
包含的主要成员:
a. data:传递到超时处理函数的参数,主要在多个定时器同时使用时,区别是哪个timer超时。
b. expires:定时器超时的时间,以linux的jiffies来衡量。
c. void (*function)(unsigned long):定时器超时处理函数。
- 1
- 2
- 3
- 4
- 5
3. 主要相关的API函数:
a. init_timer(struct timer_list*):定时器初始化函数;
b. add_timer(struct timer_list*):往系统添加定时器;
c. mod_timer(struct timer_list *, unsigned long jiffier_timerout):修改定时器的超时时间为jiffies_timerout;
d. timer_pending(struct timer_list *):定时器状态查询,如果在系统的定时器列表中则返回1,否则返回0;
e. del_timer(struct timer_list*):删除定时器。
- 1
- 2
- 3
- 4
- 5
- 6
4. 时间与jiffies的转换函数:
Linux系统中的jiffies类似于Windows里面的TickCount,它是定义在内核里面的一个全局变量,只是它的单位并不是秒或是毫秒。通常是250个jiffies为一秒,在内核里面可以直接使用宏定义:HZ。这里有几个时间和jiffies的相互转换函数:
unsigned int jiffies_to_msecs(unsigned long);
unsigned int jiffies_to_usecs(unsigned long);
unsigned long msecs_to_jiffies(unsigned int);
unsigned long usecs_to_jiffies(unsigned int);
- 1
- 2
- 3
- 4
- 5
- 6
5. 使用简例:
步骤:init_timer->[timer.expires=? & timer.function=?]->add_timer->[mod_timer]->del_timer.
- 1
- 2
#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>
struct timer_list timer;void timer_handler(unsigned long data) {printk(KERN_INFO"timer pending:%d\n", timer_pending(&timer));mod_timer(&timer, jiffies+msecs_to_jiffies(1000));printk(KERN_INFO"jiffies:%ld, data:%ld\n", jiffies, data);
}
int timer_init(void) {printk(KERN_INFO"%s jiffies:%ld\n", __func__, jiffies);printk(KERN_INFO"ji:%d,HZ:%d\n", jiffies_to_msecs(250), HZ);init_timer(&timer);timer.data = 45;timer.function = timer_handler;timer.expires = jiffies + HZ;add_timer(&timer);printk(KERN_INFO"timer pending:%d\n", timer_pending(&timer));return 0;
}void timer_exit(void) {printk(KERN_INFO"%s jiffies:%ld\n", __func__, jiffies);del_timer(&timer);
}module_init(timer_init);
module_exit(timer_exit);
MODULE_LICENSE("GPL");