| 
 | 
 
定时器一个相关的数据结构(内核版本:Debian5@2.6.26): 
struct timer_list { 
    struct list_head entry; 
    unsigned long expires; 
 
    void (*function)(unsigned long); 
    unsigned long data; 
 
    struct tvec_t_base_s *base; 
#ifdef CONFIG_TIMER_STATS 
    void *start_site; 
    char start_comm[16]; 
    int start_pid; 
#endif 
};  
其中常用到的几个量为: 
expires --- 超时时间 
function --- 超时后执行的函数 
data --- 传入 function 函数的变量。 
 
 
定时器应用简单实例: 
#include <linux/init.h> 
#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/timer.h> 
 
MODULE_LICENSE("Dual BSD/GPL"); 
 
struct timer_list timer_test; 
 
void print_message(unsigned long parameter) 
{ 
    printk ("time over and the parameter is %lu\n", parameter); 
     
} 
static int time_init (void) 
{ 
    init_timer (&timer_test); 
    timer_test.expires = jiffies + 3*HZ; 
    timer_test.data = 888;    
     timer_test.function = print_message; 
    add_timer(&timer_test);     
        return 0; 
} 
 
static void time_exit (void)  
{ 
    del_timer(&timer_test); 
}  
Makefile 文件: 
 
obj-m := timer.o 
all: 
    @make -C /lib/modules/`uname -r`/build M=`pwd` modules 
  
 
 
加载后通过 demsg 可以看到输出(3秒后): 
[ 9702.790529] time over and the parameter is 888  |   
 
 
 
 |