一、Linux 內(nèi)核定時(shí)器介紹
??在 Linux 內(nèi)核中很多函數(shù)是基于定時(shí)器進(jìn)行驅(qū)動(dòng)的,但是內(nèi)核定時(shí)器的精度并不高,所以不能作為高精度定時(shí)器使用。并且內(nèi)核定時(shí)器的運(yùn)行沒有周期性,到達(dá)計(jì)時(shí)終點(diǎn)后會(huì)自動(dòng)關(guān)閉。如果要實(shí)現(xiàn)周期性定時(shí),就要在定時(shí)處理函數(shù)中重新開啟定時(shí)器。
??Linux 內(nèi)核中使用 timer_list 結(jié)構(gòu)體表示內(nèi)核定時(shí)器,該結(jié)構(gòu)體定義在“內(nèi)核源碼/include/linux/timer.h”文件中,具體內(nèi)容如下所示:
struct timer_list {
/*
* All fields that change during normal runtime grouped to the
* same cacheline
*/
struct hlist_node entry;
unsigned long expires;
void (*function)(struct timer_list *);
u32 flags;
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
ANDROID_KABI_RESERVE(1);
ANDROID_KABI_RESERVE(2);
};
??使用以下宏對(duì) timer_list 結(jié)構(gòu)體進(jìn)行定義,_name 為定義的結(jié)構(gòu)體名稱,_function為定時(shí)處理函數(shù),該宏同樣定義在文件 “內(nèi)核源碼/include/linux/timer.h”文件中,如下所示:
#define DEFINE_TIMER(_name, _function) \
struct timer_list _name = \
__TIMER_INITIALIZER(_function, 0)
??例如可以使用以下代碼對(duì)定時(shí)器和相應(yīng)的定時(shí)處理函數(shù)進(jìn)行定義
DEFINE_TIMER(timer_test,function_test);//定義一個(gè)定時(shí)器
??定時(shí)器定義完成之后還需要通過一系列的 API 函數(shù)來初始化此定時(shí)器,部分函數(shù)說明如下文章來源:http://www.zghlxwxcb.cn/news/detail-714185.html
函數(shù) | 作用 |
---|---|
void add_timer(struct timer_list *timer) | 向 Linux 內(nèi)核注冊定時(shí)器,使用add_timer 函數(shù)向內(nèi)核注冊定時(shí)器以后,定時(shí)器就會(huì)開始運(yùn)行 |
int del_timer(struct timer_list * timer) | 刪除一個(gè)定時(shí)器 |
int mod_timer(struct timer_list *timer,unsigned long expires) | 修改定時(shí)值,如果定時(shí)器還沒 |
??在使用 add_timer()函數(shù)向 Linux 內(nèi)核注冊定時(shí)器之前,還需要設(shè)置定時(shí)時(shí)間,定時(shí)時(shí)間由 timer_list 結(jié)構(gòu)體中的 expires 參數(shù)所確定,單位為節(jié)拍數(shù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-714185.html
二、代碼示例
2.1、程序示例
#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>
static void function_test(struct timer_list *t);//定義function_test定時(shí)功能函數(shù)
DEFINE_TIMER(timer_test,function_test);//定義一個(gè)定時(shí)器
static void function_test(struct timer_list *t)
{
printk("this is function test \n");
mod_timer(&timer_test,jiffies_64 + msecs_to_jiffies(5000));//使用mod_timer函數(shù)將定時(shí)時(shí)間設(shè)置為五秒后
}
static int __init timer_mod_init(void) //驅(qū)動(dòng)入口函數(shù)
{
timer_test.expires = jiffies_64 + msecs_to_jiffies(5000);//將定時(shí)時(shí)間設(shè)置為五秒后
add_timer(&timer_test);//添加一個(gè)定時(shí)器
return 0;
}
static void __exit timer_mod_exit(void) //驅(qū)動(dòng)出口函數(shù)
{
del_timer(&timer_test);//刪除一個(gè)定時(shí)器
printk("module exit \n");
}
module_init(timer_mod_init);
module_exit(timer_mod_exit);
2.2、Linux 內(nèi)核定時(shí)器使用API要點(diǎn)
DEFINE_TIMER(timer_test,function_test);//定義一個(gè)定時(shí)器
add_timer(&timer_test);//添加一個(gè)定時(shí)器
static void function_test(struct timer_list *t)
{
printk("this is function test \n");
mod_timer(&timer_test,jiffies_64 + msecs_to_jiffies(5000));//使用mod_timer函數(shù)將定時(shí)時(shí)間設(shè)置為五秒后
}
到了這里,關(guān)于Linux 內(nèi)核定時(shí)器(高級(jí)字符設(shè)備五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!