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函数将定时时间设置为五秒后
}	
相关推荐
mc23566 分钟前
Linux实用操作
linux·运维·服务器
半梦半醒*29 分钟前
k8s——pod详解2
linux·运维·docker·容器·kubernetes·负载均衡
vvw&1 小时前
如何使用 Nodemon 自动重启 Node.js 应用
linux·运维·服务器·node.js
Bdygsl1 小时前
Linux(3)—— 权限操作
linux
奔跑吧邓邓子1 小时前
【C语言实战(75)】C语言内存探秘:泄漏检测与分析实战
linux·c语言·windows·内存·开发实战·泄露检测
HIT_Weston2 小时前
16、【Ubuntu】【VSCode】VSCode 断联问题分析:问题解决
linux·vscode·ubuntu
被遗忘的旋律.2 小时前
Linux驱动开发笔记(十九)——IIC(AP3216C驱动+MPU6050驱动)
linux·驱动开发·笔记
Dreamboat-L3 小时前
使用VMware安装centos的详细流程(保姆级教程)
linux·运维·centos
蓦然回首的风度3 小时前
【运维记录】Centos 7 基础命令缺失
linux·运维·centos
kblj55554 小时前
学习Linux——网络基础管理
linux·网络·学习