(十二)linux内核定时器
目录
- (一)内核定时器介绍
- (二)内核定时器相关接口
- (三)使用步骤
- (四)实例代码
(一)内核定时器介绍
内核定时器并不是用来简单的定时操作,而是在定时时间后,触发事件的操作,类似定时器中断,是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,内核中采用的定时器以jiffies为单位。 单位秒=jiffies/HZ
几个重要跟时间有关的名词或变数
HZ:Linux核心每隔固定周期会发出timer interrupt (IRQ 0),HZ是用来定义每一秒有几次timer interrupts。举例来说,HZ为1000,代表每秒有1000次timer interrupts。
Tick:Tick是HZ的倒数,意即timer interrupt每发生一次中断的时间。如HZ为250时,tick为4毫秒(millisecond)。
Jiffies:Jiffies为Linux核心变数(unsigned long),它被用来记录系统自开机以来,已经过了多少tick。每发生一次timer interrupt,Jiffies变数会被加一。值得注意的是,Jiffies于系统开机时,并非初始化成零,而是被设为-300*HZ ,类似Linux系统中time(日历时间)
(二)内核定时器相关接口
和定时器先关的数据结构:
struct timer_list {unsigned long expires; //未来时间点,即超时时间void (*function)(unsigned long);//超时回调函数unsigned long data; //传递给回调函数的数据,也就是定时器数据
};
初始化定时器相关的数据结构
1. 静态定以定时器数据结构:
#define DEFINE_TIMER(_name, _function, _expires, _data) \struct timer_list _name = \TIMER_INITIALIZER(_function, _expires, _data)2. 动态初始化:-----手动对变量进行初始化
#define init_timer(timer) \do { \static struct lock_class_key __key; \init_timer_key((timer), #timer, &__key); \} while (0)
可延时定时:
#define init_timer_deferrable(timer) \do { \static struct lock_class_key __key; \init_timer_deferrable_key((timer), #timer, &__key); \} while (0)
设置定时时间
#define setup_timer(timer, fn, data) \
do { \static struct lock_class_key __key; \setup_timer_key((timer), #timer, &__key, (fn), (data));\
} while (0)
向内核添加定时器:
/*** add_timer - start a timer* @timer: the timer to be added** The kernel will do a ->function(->data) callback from the* timer interrupt at the ->expires point in the future. The* current time is 'jiffies'.** The timer's ->expires, ->function (and if the handler uses it, ->data)* fields must be set prior calling this function.** Timers with an ->expires field in the past will be executed in the next* timer tick.*/
void add_timer(struct timer_list *timer)
{BUG_ON(timer_pending(timer));mod_timer(timer, timer->expires);
}
从内核删除定时器:
/*** del_timer - deactive a timer.* @timer: the timer to be deactivated** del_timer() deactivates a timer - this works on both active and inactive* timers.** The function returns whether it has deactivated a pending timer or not.* (ie. del_timer() of an inactive timer returns 0, del_timer() of an* active timer returns 1.)*/
int del_timer(struct timer_list *timer)
修改定时时间:
/*** mod_timer - modify a timer's timeout* @timer: the timer to be modified* @expires: new timeout in jiffies** mod_timer() is a more efficient way to update the expire field of an* active timer (if the timer is inactive it will be activated)** mod_timer(timer, expires) is equivalent to:** del_timer(timer); timer->expires = expires; add_timer(timer);** Note that if there are multiple unserialized concurrent users of the* same timer, then mod_timer() is the only safe way to modify the timeout,* since add_timer() cannot modify an already running timer.** The function returns whether it has modified a pending timer or not.* (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an* active timer returns 1.)*/
int mod_timer(struct timer_list *timer, unsigned long expires)
jiffies和时间的转换:
extern unsigned int jiffies_to_msecs(const unsigned long j);
extern unsigned int jiffies_to_usecs(const unsigned long j);
extern unsigned long msecs_to_jiffies(const unsigned int m);
extern unsigned long usecs_to_jiffies(const unsigned int u);
(三)使用步骤
1、向内核添加定时器
setup_timer();设置定时器add_timer();.
2、解绑定时器
int del_timer(struct timer_list *timer)
(四)实例代码
#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/module.h>struct timer_list timer;
void function(unsigned long data)
{static int count=0;printk("this is timer test:%d\n",count++);mod_timer(&timer,jiffies+1*HZ);
}
static int __init timer_module_init(void)
{timer.expires = jiffies+5*HZ;setup_timer(&timer,function,0);add_timer(&timer);return 0;
}
static void __exit timer_module_cleanup(void)
{del_timer(&timer);
}module_init(timer_module_init);
module_exit(timer_module_cleanup);
MODULE_LICENSE("GPL");
本文章仅供学习交流用禁止用作商业用途,文中内容来水枂编辑,如需转载请告知,谢谢合作
微信公众号:zhjj0729
微博:文艺to青年
(十二)linux内核定时器相关推荐
- (十二)Linux内核驱动之poll和select
使用非阻塞 I/O 的应用程序常常使用 poll, select, 每个允许一个进程来决定它是否可读或者写一个或多个文件而不阻塞. 这些调用也可阻塞进程直到任何一个给定集合的文件描述符可用来读或写. ...
- linux内核定时器实验
文章目录 一.linux时间管理和内核定时器简介 1.内核时间管理简介 2.内核定时器简介 1.init_timer 函数 2.add_timer 函数 3.del_timer 函数 4.del_ti ...
- Linux 内核定时器实验————复习到这
目录 Linux 时间管理和内核定时器简介 内核时间管理简介 内核定时器简介 Linux 内核短延时函数 硬件原理图分析 实验程序编写 修改设备树文件 定时器驱动程序编写 编写测试APP 运行测试 编 ...
- linux内核定时器编程
1.linux内核定时器基本结构和函数 1)struct timer_list 一个struct timer_list对应了一个定时器. #include <linux/timer.h> ...
- 如何使用 Linux 内核定时器
1. Linux内核定时器介绍 1.1 内核时间管理 内核中很多函数需要时间管理,比如周期性的调度程序.延时程序.定时器.硬件定时器提供时钟源,时钟源频率可以设置,设置好后就周期性的产生定时中断,系统 ...
- linux 内核定时器(低精度) — 外部看门狗程序
文章目录 1 内核定时器介绍 2 定时器数据结构及函数 3 外部看门狗驱动使用定时器函数 1 内核定时器 Linux 内核中有大量的函数需要时间管理,比如周期性的调度程序.延时程序等.硬件定时器 提供 ...
- Linux 驱动开发 三十四:Linux 内核定时器原理
参考文档: <Cortex -A7 MPCore Technical Reference Manual> 中 Chapter 9:Generic Timer. <ARM ® Arch ...
- Linux 内核定时器使用 二 高精度定时器 hrtimer 的用例
之前介绍了timer_list内核定时器,它的精度在毫秒级别,再高一点它就无能为力了,所幸内核提供了高精度定时器 hrtimer. 源文件在linux/kernel/hrtimer.c中.接口简单.下 ...
- linux 内核定时器 3.11 版本,Linux内核定时器简单使用
因为项目需要,我这里简单列一个在内核中使用timer的方法.这篇笔记不谈详细原理(以后的Linux内核设备驱动原理里讲),只讲快速使用 Timer使用原则 Timer是Linux内核的一种软中断,被调 ...
最新文章
- android 找不到类文件,Android Studio单元测试找不到类文件!
- 指针空间的申请与释放
- Kubernetes — 设计理念
- madplay播放器移植
- 英文词频统计预备,组合数据类型练习
- how to come in an investnent bank team
- boost::detail::lowest_bit的测试程序
- 关于uint8_t/uint16_t/uint32_t/uint_fast16_t
- 选择题微型计算机系统包括,全国计算机一级选择题专项训练及答案2016
- 基于python实现将一个文件夹中的图片移动到另一个文件夹
- 【vue】路由Router基础详解,带你快速入门~
- javaScript内置对象简介
- WebReBuild年会流水记
- 分布式系统的一些基本概念
- mysql关系范式试题_数据库范式练习题
- 08系统装iss_安全信息系统| ISS | 第1部分
- 安装kalibr踩坑5:E: Package ‘python-software-properties‘ has no installation candidate
- android studio调用python_Android Studio调用python运行thensorflow模型--CLE方案实现
- 太阳、地球、月球公转与自转
- 通过google地图来查看台湾街景
热门文章
- 类选择器遍历赋值_利用反射实现配置表数据到类对象数据的转换
- Linux7/Redhat7/Centos7 安装Oracle 12C_监听配置及DBCA安装数据库_05
- Java问题集锦--The type StringEscapeUtils is deprecated
- java实现删除指定指定目录下面指定某种类型的文件
- Oracle 表空间常用sql
- php请求接口两次,php curl post请求执行一次curl_exce 请求的接口确执行两次
- python函数传值还是地址_Python传值与传址
- qt整个窗口上绘制矩形与在窗口的子控件上绘制矩形
- 使用gitlab初次上传代码
- C语言 野指针 - C语言零基础入门教程