我是靠谱客的博主 标致未来,这篇文章主要介绍史上最简单的Linux 内核Timer使用入门,现在分享给大家,希望可以做个参考。

1 使用流程:

初始化,设置到期时间,设置到期时执行的函数,然后加入到系统的队列中。

复制代码
1
2
3
4
5
6
7
static struct timer_list my_timer; init_timer(&my_timer); my_timer.expires= jiffies + 2 * HZ; my_timer.function = my_timer_func; my_timer.data=0; add_timer(&my_timer);

在到期函数中,继续修改到期时间就可以循环执行了

static void my_timer_func(unsigned long unused) {
pr_info(“timer expires , now do sthn”);
if (!timer_pending(&my_timer)) {
mod_timer(&my_timer, jiffies + 2 * HZ);
}
}

2 全部代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <linux/init.h> /* Needed for the macros */ #include <linux/kernel.h> /* Needed for pr_info() */ #include <linux/module.h> /* Needed by all modules */ #include <linux/timer.h> static struct timer_list my_timer; static void my_timer_func(unsigned long unused) { pr_info("timer expires , now do sthn"); if (!timer_pending(&my_timer)) { mod_timer(&my_timer, jiffies + 2 * HZ); } } static __init int my_init(void) { init_timer(&my_timer); my_timer.function = my_timer_func; my_timer.expires= jiffies + 2 * HZ; my_timer.data=0; add_timer(&my_timer); return 0; } static void __exit my_exit(void) { pr_info("a3 cleanup_modulen"); del_timer(&my_timer); } module_init(my_init); module_exit(my_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Andy"); MODULE_DESCRIPTION("andy one-key driver"); MODULE_ALIAS("one-key");

timer_pending((&my_timer) 用来判断my_timer是否已经加入内核的等待队列正在等待调度.

最后

以上就是标致未来最近收集整理的关于史上最简单的Linux 内核Timer使用入门的全部内容,更多相关史上最简单的Linux内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部