Linux 内核定时器(高级字符设备五)

一、Linux 内核定时器介绍

在 Linux 内核中很多函数是基于定时器进行驱动的,但是内核定时器的精度并不高,所以不能作为高精度定时器使用。并且内核定时器的运行没有周期性,到达计时终点后会自动关闭。如果要实现周期性定时,就要在定时处理函数中重新开启定时器。

Linux 内核中使用 timer_list 结构体表示内核定时器,该结构体定义在"内核源码/include/linux/timer.h"文件中,具体内容如下所示:

c 复制代码
struct timer_list {
	/*
	 * All fields that change during normal runtime grouped to the
	 * same cacheline
	 */
	struct hlist_node	entry;
	unsigned long		expires;
	void			(*function)(struct timer_list *);
	u32			flags;

#ifdef CONFIG_LOCKDEP
	struct lockdep_map	lockdep_map;
#endif

	ANDROID_KABI_RESERVE(1);
	ANDROID_KABI_RESERVE(2);
};

使用以下宏对 timer_list 结构体进行定义,_name 为定义的结构体名称,_function为定时处理函数,该宏同样定义在文件 "内核源码/include/linux/timer.h"文件中,如下所示:

c 复制代码
#define DEFINE_TIMER(_name, _function)				\
	struct timer_list _name =				\
		__TIMER_INITIALIZER(_function, 0)

例如可以使用以下代码对定时器和相应的定时处理函数进行定义

c 复制代码
DEFINE_TIMER(timer_test,function_test);//定义一个定时器

定时器定义完成之后还需要通过一系列的 API 函数来初始化此定时器,部分函数说明如下

函数 作用
void add_timer(struct timer_list *timer) 向 Linux 内核注册定时器,使用add_timer 函数向内核注册定时器以后,定时器就会开始运行
int del_timer(struct timer_list * timer) 删除一个定时器
int mod_timer(struct timer_list *timer,unsigned long expires) 修改定时值,如果定时器还没

在使用 add_timer()函数向 Linux 内核注册定时器之前,还需要设置定时时间,定时时间由 timer_list 结构体中的 expires 参数所确定,单位为节拍数。

二、代码示例

2.1、程序示例

c 复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>

static void function_test(struct timer_list *t);//定义function_test定时功能函数
DEFINE_TIMER(timer_test,function_test);//定义一个定时器
static void function_test(struct timer_list *t)
{
	printk("this is function test \n");
	mod_timer(&timer_test,jiffies_64 + msecs_to_jiffies(5000));//使用mod_timer函数将定时时间设置为五秒后
}	
static int __init timer_mod_init(void) //驱动入口函数
{
	timer_test.expires = jiffies_64 + msecs_to_jiffies(5000);//将定时时间设置为五秒后
	add_timer(&timer_test);//添加一个定时器
	return 0;
}

static void __exit timer_mod_exit(void) //驱动出口函数
{
	del_timer(&timer_test);//删除一个定时器
	printk("module exit \n");
}
module_init(timer_mod_init);
module_exit(timer_mod_exit);

2.2、Linux 内核定时器使用API要点

c 复制代码
DEFINE_TIMER(timer_test,function_test);//定义一个定时器
c 复制代码
add_timer(&timer_test);//添加一个定时器
c 复制代码
static void function_test(struct timer_list *t)
{
	printk("this is function test \n");
	mod_timer(&timer_test,jiffies_64 + msecs_to_jiffies(5000));//使用mod_timer函数将定时时间设置为五秒后
}	
相关推荐
CodeWithMe36 分钟前
[ Vim ] 常用命令 and 配置
linux·编辑器·vim
DC_BLOG40 分钟前
Linux-GlusterFS进阶分布式卷
linux·运维·服务器·分布式
cookies_s_s1 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
丁劲犇2 小时前
碳基生物的悲歌-DeepSeek思考实现Linux动态库递归收集工具
linux·递归·deepseek·ldd
zhouwu_linux2 小时前
MT7628基于原厂的SDK包, 修改ra1网卡的MAC方法。
linux·运维·macos
2401_897930062 小时前
linux系统如何配置host.docker.internal
linux·docker·eureka
诶尔法Alpha2 小时前
Linux上使用dify构建RAG
linux·运维·服务器
熬夜苦读学习3 小时前
Linux文件系统
linux·运维·服务器·开发语言·后端
沐千熏3 小时前
Liunx(CentOS-6-x86_64)系统安装MySql(5.6.50)
linux·mysql·centos
黑牛先生4 小时前
【Linux】匿名管道
linux·运维·服务器