定时器:
linux操作系统提供了一个内核定时器
内核定时器可在未来的某个特定时间点调度执行某个函数,完成指定任务
linux内核定时器提供了一种异步处理的机制
用户通过设置将来某一时刻的滴答值来实现定时功能
假设HZ的值为200,Linux定时器最短定时时间为5ms,小于该时间的定时可直接通过硬件定时器完成
定时器使用过程:
定时器接口函数介绍:
#include <linux/timer.h>
定时器结构体类型
struct timer_list
{
struct list_head entry;//链表节点,由内核管理
//定时器到期时间(指定一个时刻)
unsigned long expires;
// 定时器处理函数
void (*function)(unsigned long);
// 作为参数被传入定时器处理函数
unsigned long data;
......
};
初始化定时器
void init_timer(struct timer_list *timer);
添加定时器。定时器开始计时
void add_timer(struct timer_list * timer);
删除定时器,在定时器到期前禁止一个已注册定时器
int del_timer(struct timer_list * timer);
如果定时器函数正在执行则在函数执行完后返回(SMP)
int del_timer_sync(struct timer_list *timer);
更新定时器到期时间,并开启定时器
int mod_timer(struct timer_list *timer,unsigned long expires);
查看定时器是否正在被调度运行
int timer_pending(const struct timer_list*timer);
/*返回值为真表示正在被调度运行*/
实例代码:
驱动端:
include <linux/module.h>
include <linux/kernel.h>
include <linux/init.h>
include <linux/fs.h>
include <asm/uaccess.h>
include <linux/device.h>
include <linux/errno.h>
include <linux/timer.h>
include <linux/jiffies.h>
define DRIVER_NAME "timer_test"
define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)
define __debug(fmt, arg...) printk(KERN_DEBUG fmt, ##arg)
static int major = 0;
static int minor = 0;
struct class *timer_class;
struct device *timer_device;
struct timer_list timer;
static void timer_handler(unsigned long dat)
{
static int sec = 0;
sec++;
printk("<kernel>: deal timer handler.... sec = %d\n", sec); //打印测试信息
/*更新定时器到时时间,并开启定时器*/
mod_timer(&timer, jiffies+1*HZ);
return;
}
static int timer_test_open(struct inode inode, struct file file)
{
__debug("<kernel>: start timer test...\n");
/* return value: 1 if the timer is pending, 0 if not.*/
if(!timer_pending(&timer)){ //查看定时器是否正在被调度运行
timer.expires = jiffies + 1*HZ;
add_timer(&timer);
}
return 0;
}
static int timer_test_release(struct inode inode, struct file file)
{
del_timer_sync(&timer);
__debug("<kernel>: stop timer test...\n");
return 0;
}
static struct file_operations timer_test_fops = {
.owner = THIS_MODULE,
.open = timer_test_open,
.release = timer_test_release,
};
static int __init timer_test_init(void)
{
int retval;
//__debug("in key_eint_init\n");
init_timer(&timer); //初始化定时器
timer.data = 0L; //设定定时器参数
timer.function = timer_handler; //设定定时器服务函数
major = register_chrdev(major, DRIVER_NAME, &timer_test_fops);
if(major < 0){
err("register char device fail!");
retval = major;
goto error;
}
timer_class=class_create(THIS_MODULE,DRIVER_NAME);
if(IS_ERR(timer_class)){
err("class create failed!");
retval = PTR_ERR(timer_class);
goto error_class;
}
timer_device=device_create(timer_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);
if(IS_ERR(timer_device)){
err("device create failed!");
retval = PTR_ERR(timer_device);
goto error_device;
}
__debug("register timer test OK! Major = %d\n", major);
return 0;
error_device:
class_destroy(timer_class);
error_class:
unregister_chrdev(major, DRIVER_NAME);
error:
return retval;
}
static void __exit timer_test_exit(void)
{
unregister_chrdev(major, DRIVER_NAME);
device_destroy(timer_class,MKDEV(major, minor));
class_destroy(timer_class);
return;
}
module_init(timer_test_init);
module_exit(timer_test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Eric");
[点击并拖拽以移动]
应用层:
include <stdio.h>
include <stdlib.h>
include <fcntl.h>
include <unistd.h>
int main()
{
char *devname = "/dev/timer_test";
int fd;
unsigned int sec;
fd = open(devname, O_RDWR);
if(fd < 0){
printf("file open failed!\n");
return 1;
}
while(1);
close(fd);
}