内核定时器struct timer_list


1
#include<linux/module.h> 2 #include<linux/timer.h> 3 #include <linux/fs.h> 4 MODULE_LICENSE("GPL"); 5 6 struct timer_list mytimer; 7 void mytimer_func(unsigned long data) 8 { 9 static int count=0; 10 printk("---------%d",data); 11 } 12 int __init mytimer_init(void) 13 { 14 printk("timer init! "); 15 16 init_timer(&mytimer); 17 mytimer.expires=jiffies+HZ*5; 18 mytimer.function=mytimer_func; 19 mytimer.data=100; 20 add_timer(&mytimer); 21 return 0; 22 } 23 void __exit mytimer_exit(void) 24 { 25 del_timer(&mytimer); 26 printk("timer exit! "); 27 } 28 module_init(mytimer_init); 29 module_exit(mytimer_exit);
struct timer_list {
    /*
     * All fields that change during normal runtime grouped to the
     * same cacheline
     */
    struct list_head entry;
    unsigned long expires;
    struct tvec_base *base;

    void (*function)(unsigned long);
    unsigned long data;

    int slack;

#ifdef CONFIG_TIMER_STATS
    int start_pid;
    void *start_site;
    char start_comm[16];
#endif
#ifdef CONFIG_LOCKDEP
    struct lockdep_map lockdep_map;
#endif
};

编程时候需要用到struct timer_list 结构体。

expires 需要设置,如果需要5秒之后定时器自动调用函数,则:
mytimer.expires=jiffies+HZ*5;
5秒之后自动调用.function 指向的函数。

另外给一个不错的链接:http://blog.sina.com.cn/s/blog_6a1837e90100oarr.html
原文地址:https://www.cnblogs.com/coversky/p/7808855.html