定时器+工作队列 内核周期性任务的实现机制

1 /* 本程序结合了内核定时器 和 工作队列 用于实现 周期性任务
2 * 其工作原理是 在定时器处理函数中 做完相应的工作后 延后expires 并将定时器
3 * 再次添加到内核定时器链表 以便定时器能再次触发
4 */
5 #include<linux/kernel.h>
6 #include<linux/module.h>
7 #include<linux/init.h>
8 #include<linux/timer.h> /*timer*/
9 #include<asm/uaccess.h> /*jiffies*/
10
11 #define TIMER_OVER 3 /*调用时间间隔*/
12
13 MODULE_LICENSE("GPL");
14 MODULE_AUTHOR("wenhui");
15 MODULE_DESCRIPTION("Timer_worklist Module");
16
17 static struct timer_list xxx_timer; /*内核定时器*/
18 static struct work_struct xxx_work_q; /*工作队列(中断下半部方法中的1种)*/
19
20 static int counter = 0; /*进入中断次数统计*/
21
22 /*定时器中断服务程序*/
23 static void xxx_timer_function(int para)
24 {
25 //调度工作队列
26 if(schedule_work(&xxx_work_q) == 0){
27 printk("<0> cannot schedule work !!!\n");
28 }
29 counter++;
30 printk("<0> Timer Expired and para is %d s !!\n", para);
31 printk("<0> counter = %d\n\n",counter);
32 }
33
34 //注册一个内核定时器
35 static void xxx_timer_register(struct timer_list* ptimer, unsigned int timeo ver){
36 printk("<0> timer_register!\n");
37 init_timer(&xxx_timer);
38 xxx_timer.data = timeover;
39 xxx_timer.expires = jiffies + (5 * HZ);
40 xxx_timer.function = xxx_timer_function;
41 add_timer(&xxx_timer);
42 }
43
44 //定时器底半部处理函数
45 static void xxx_fectch_thread(struct work_struct *work)
46 {
47 printk("<0> xxx_fectch_thread\n");
48 xxx_timer_register(&xxx_timer, TIMER_OVER);
49 }
50
51 int __init xxx_init(void)
52 {
53 xxx_timer_register(&xxx_timer, TIMER_OVER);
54 //初始化工作队列并将工作队列与处理函数绑定
55 INIT_WORK(&xxx_work_q, xxx_fectch_thread);
56 printk("<0> xxx_init\n");
57 return 0;
58 }
59
60 void __exit xxx_exit()
61 {
62 del_timer(&xxx_timer);
63 printk("<0>xxx_exit!\n");
64 }
65
66 module_init(xxx_init);
67 module_exit(xxx_exit);
68


1 /* 本程序结合了内核定时器 和 工作队列 用于实现 周期性任务
2 * 其工作原理是 在定时器处理函数中 做完相应的工作后 延后expires 并将定时器
3 * 再次添加到内核定时器链表 以便定时器能再次触发
4 */
5 #include<linux/kernel.h>
6 #include<linux/module.h>
7 #include<linux/init.h>
8 #include<linux/timer.h> /*timer*/
9 #include<asm/uaccess.h> /*jiffies*/
10
11 #define TIMER_OVER 3 /*调用时间间隔*/
12
13 MODULE_LICENSE("GPL");
14 MODULE_AUTHOR("wenhui");
15 MODULE_DESCRIPTION("Timer_worklist Module");
16
17 static struct timer_list xxx_timer; /*内核定时器*/
18 static struct work_struct xxx_work_q; /*工作队列(中断下半部方法中的1种)*/
19
20 static int counter = 0; /*进入中断次数统计*/
21
22 /*定时器中断服务程序*/
23 static void xxx_timer_function(int para)
24 {
25 //调度工作队列
26 if(schedule_work(&xxx_work_q) == 0){
27 printk("<0> cannot schedule work !!!\n");
28 }
29 counter++;
30 printk("<0> Timer Expired and para is %d s !!\n", para);
31 printk("<0> counter = %d\n\n",counter);
32 }
33
34 //注册一个内核定时器
35 static void xxx_timer_register(struct timer_list* ptimer, unsigned int timeo ver){
36 printk("<0> timer_register!\n");
37 init_timer(&xxx_timer);
38 xxx_timer.data = timeover;
39 xxx_timer.expires = jiffies + (5 * HZ);
40 xxx_timer.function = xxx_timer_function;
41 add_timer(&xxx_timer);
42 }
43
44 //定时器底半部处理函数
45 static void xxx_fectch_thread(struct work_struct *work)
46 {
47 printk("<0> xxx_fectch_thread\n");
48 xxx_timer_register(&xxx_timer, TIMER_OVER);
49 }
50
51 int __init xxx_init(void)
52 {
53 xxx_timer_register(&xxx_timer, TIMER_OVER);
54 //初始化工作队列并将工作队列与处理函数绑定
55 INIT_WORK(&xxx_work_q, xxx_fectch_thread);
56 printk("<0> xxx_init\n");
57 return 0;
58 }
59
60 void __exit xxx_exit()
61 {
62 del_timer(&xxx_timer);
63 printk("<0>xxx_exit!\n");
64 }
65
66 module_init(xxx_init);
67 module_exit(xxx_exit);
68

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值