Linux内核定时器--timer_list

定时器:

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);
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eric_Xi_BJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值