内核定时器 用法与实法 init_timer timer

2017-01-13 08:17:23来源:CSDN作者:u012385733人点击

第七城市

内核定时器的实现,依赖时钟滴答中断来实现, 实时性比较好


open_softirq(TIMER_SOFTIRQ, run_timer_softirq);// 时钟中断


init_timer();    //初始 timer_list 的结构的一些变量

add_timer() //将timer 加入内核timer列表中,等待处理


以下为例子源码,仅供参考

#include <linux/module.h>#include <linux/timer.h>#include <linux/kernel.h>#include <linux/slab.h>#include <linux/jiffies.h>#define TIMER_T 2char test[] = {"this is first timer/n"};struct timer_list *timer[TIMER_T]={NULL};static void timer_func(unsigned long data){	char *str = (char *)data;	printk(KERN_EMERG "%s/n", str);}static int __init timer_k_init(void){	int i = 0;		for(i=0; i<TIMER_T; i++)	{		timer[i] = (struct timer_list *)kmalloc(sizeof(struct timer_list), GFP_KERNEL);		if(timer[i] == NULL)		{			printk(KERN_EMERG "malloc is failed/n");			return ENOMEM;		}	}	for(i=0; i < TIMER_T; i++)	{		init_timer(timer[i]);		timer[i]->expires = jiffies + HZ * (i + 3);		timer[i]->data = (unsigned long)test;		timer[i]->function = timer_func;		add_timer(timer[i]);	}	return 0;}static void __exit timer_k_exit(void){	int i = 0;	for(i=0; i<TIMER_T; i++)	{		del_timer(timer[i]);	}}module_init(timer_k_init);module_exit(timer_k_exit);

Makefile:

CUR=$(shell pwd)KERNEL_DIR=/lib/modules/$(shell uname -r)/buildobj-m=timer_k.oall:	make -C $(KERNEL_DIR) M=$(CUR) modulesclean:	rm *.ko	rm *.o


第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台