內核定時器的例子

#include
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/timer.h>


#include <linux/kernel.h>


struct timer_list stimer; //定義定時器
static void time_handler(unsigned long data){ //定時器處理函數
mod_timer(&stimer, jiffies + HZ);
printk("current jiffies is %ld\n", jiffies);
}
static int __init timer_init(void){ //定時器初始化過程
printk("My module worked!\n");
init_timer(&stimer);
stimer.data = 0;
stimer.expires = jiffies + HZ; //設置到期時間
stimer.function = time_handler;
add_timer(&stimer);
return 0;
}
static void __exit timer_exit(void){
printk("Unloading my module.\n");
del_timer(&stimer);//刪除定時器
return;
}
module_init(timer_init);//加載模塊
module_exit(timer_exit);//卸載模塊


MODULE_AUTHOR("fyf");


MODULE_LICENSE("GPL");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章