linux內核使用timer_list 結構體當作定時器。#include "linux/timer.h"
#include "linux/module.h"
MODULE_LICENSE("GPL");
//不加這句話,雖然不影響功能,但“有時候”程序執行時會打印錯誤,類似 Disabling lock debugging
//due to kernel taint 之類的話
struct timer_list tm;
static int num;
static void func()
{
num++;
mod_timer(&tm,jiffies+1*HZ);
//timer一旦超時,就會執行fuc函數,然后永遠的休眠,
//所以如果沒有這mod_timer,hello world 只會執行一次,也就是timer第一次超時時執行的那次。
//mod_timer可以激活timer。如果你沒有add_timer(),激活也沒用
printk("hello,world n ,%d",num);
}
static int timer_init(void)
{
init_timer(&tm); //初始化定時器,必須在所有下面復制操作前進行定時器初始化
tm.expires = jiffies +1*HZ; //超時1秒,執行function
tm.function = func; //超時后執行的函數
add_timer(&tm); //將定時器加入定時器等待隊列中
return 0;
}
static void timer_destory(void)
{
del_timer(&tm);
printk("remove timern");
}
評論