Linux 核心定時器

來源:互聯網
上載者:User

定時器,有時也稱為動態定時器或核心定時器,是管理核心程式的時間的基礎
核心經常要延遲執行一些代碼,如下半部機制就是為了將工作推後執行
時鐘中斷由系統的定時硬體以周期性的時間間隔產生,這個間隔(即頻率)由核心根據HZ來確定
每當時鐘中斷髮生時,全域變數jiffies(unsigned long)就加1,因此jiffies記錄了自linux啟動後時鐘中斷髮生的次數。
核心定時器用於控制某個函數(定時器處理函數)在未來的某個特定時間執行。
核心定時器註冊的處理函數只執行一次--不是迴圈執行的。(定時器並不周期運行,它在逾時後就自行銷毀,動態
定時器不斷地建立和銷毀,而且它的運行次數也不受限制)

定時器的使用只須執行一些初始化工作,設定一個逾時時間,指定逾時發生後執行的函數,然後啟用定時器就可以了

1. 定時器結構timer_list
include/linux/timer.h
struct timer_list {

 struct list_head entry;//定時器鏈表的入口
 unsigned long expires;//定時器到期時間
 struct tvec_base *base;

 void (*function)(unsigned long);//定時器處理函數
 unsigned long data;//傳給定時器處理函數的長整形參數

 int slack;

 ...

};

2. 定時器的使用
1)定義一個定時器結構
struct timer_list timer;

2)初始化定時器
有很多介面函數可以初始化定時器
init_timer(struct timer_list* timer);
TIMER_INITIALIZER(_function, _expires, _data);
DEFINE_TIMER(_name, _function, _expires, _data);
通常可以這樣初始化定時器並賦值func,data
setup_timer(&timer, gpio_keys_timer, (unsigned long)data);

定時器處理函數
static void gpio_keys_timer(unsigned long data)
{
 schedule_work(&work);
}

3)增加定時器,並啟用定時器
void add_timer(struct timer_list* timer);
註冊核心定時器,並將定時器加入到核心動態定時器鏈表中

4)刪除定時器
int del_timer(struct timer_list *timer);
del_timer_sync(struct timer_list *timer);
del_timer_sync()是del_timer()的同步版,在刪除一個定時器時需等待其被處理完(不能在中斷上下文中使用)

5)修改定時器的expire,並啟動
int mod_timer(struct timer_list *timer, unsigned long expires);
mod_timer(&timer, jiffies + msecs_to_jiffies(50));//未考慮jiffies溢出問題
msecs_to_jiffies()用於將ms轉換成jiffies

/********************************************************/核心定時器例子#include <linux/init.h>#include <linux/module.h>#include <linux/timer.h>#include <linux/fs.h>#define TIMER_MAJOR 234#define DEVICE_NAME "timer_test"/**1. 定義timer結構*/struct timer_list timer;static void func_timer(unsigned long data){/**4. 修改定時器的逾時參數並重啟*/mod_timer(&timer, jiffies + HZ);printk("current jiffies is %ld\n", jiffies);}struct file_operations timer_ops = {.owner = THIS_MODULE,};static int __init timer_init(void){register_chrdev(TIMER_MAJOR, DEVICE_NAME, &timer_ops);/**2. 初始化定時器*/setup_timer(&timer, func_timer, 0);#if 0init_timer(&timer);timer.data = 0;timer.expires = jiffies + HZ;timer.function = func_timer;#endif/**3. 添加啟用計時器*/add_timer(&timer);printk("timer_init\n");return 0;}static void __exit timer_exit(void){/**4. 刪除定時器*/del_timer(&timer);unregister_chrdev(TIMER_MAJOR, DEVICE_NAME);}module_init(timer_init);module_exit(timer_exit);MODULE_LICENSE("GPL");/********************************************************/# insmod timer_test.ko timer_inittimer_initcurrent jiffies is 220614current jiffies is 220614current jiffies is 220714current jiffies is 220714

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.