新聞中心
摘要:本文介紹了在Linux系統(tǒng)中實現(xiàn)20ms高精度時序的方法。首先介紹了系統(tǒng)時鐘的原理和實現(xiàn)方式,然后介紹了內(nèi)核定時器和用戶態(tài)定時器的使用方法,最后介紹了使用Linux RT實現(xiàn)高精度定時器的方法。

水富網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)2013年至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
一、引言
在嵌入式系統(tǒng)和實時控制系統(tǒng)中,高精度時序是非常重要的。Linux系統(tǒng)中提供了多種實現(xiàn)高精度時序的方法,本文將介紹如何在Linux系統(tǒng)中實現(xiàn)20ms高精度時序。
二、系統(tǒng)時鐘原理
Linux系統(tǒng)中使用的是搶占式多任務(wù)操作系統(tǒng),其時間管理的核心是系統(tǒng)時鐘。系統(tǒng)時鐘的工作原理如下:
1.時鐘中斷:系統(tǒng)時鐘會定時向CPU發(fā)送一個中斷信號,告訴CPU現(xiàn)在的時間。
2.硬件時鐘:系統(tǒng)時鐘采用定時器模塊實現(xiàn),一般是在CPU上集成的硬件定時器。
3.計數(shù)器:時鐘中斷發(fā)生時,系統(tǒng)時鐘會讀取定時器模塊中的計數(shù)器,記錄下當前時間。
4.時鐘頻率:時鐘頻率是指CPU每秒鐘能產(chǎn)生多少時鐘中斷,一般為10ms或1ms。
三、內(nèi)核定時器
內(nèi)核定時器是Linux內(nèi)核提供的一組定時器函數(shù),它可以在內(nèi)核中創(chuàng)建周期性或非周期性的定時器。內(nèi)核定時器主要有以下幾個接口:
1.init_timer(struct timer_list *timer); // 初始化定時器
2.add_timer(struct timer_list *timer); // 啟動定時器
3.mod_timer(struct timer_list *timer, unsigned long expires); // 修改定時器
4.del_timer(struct timer_list *timer); // 刪除定時器
示例代碼:
#include
static struct timer_list mytimer;
static void myfunction(unsigned long data) {
// 定時器回調(diào)函數(shù)
}
int init_module(void) {
// 初始化定時器
init_timer(&mytimer);
// 設(shè)置定時器回調(diào)函數(shù)
mytimer.function = myfunction;
// 設(shè)置定時器超時時間
mytimer.expires = jiffies + 20*HZ/1000;
// 啟動定時器
add_timer(&mytimer);
return 0;
}
四、用戶態(tài)定時器
用戶態(tài)定時器是通過使用POSIX定時器接口實現(xiàn)的,可以在用戶態(tài)使用。它主要有以下幾個接口:
1.timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid); // 創(chuàng)建定時器
2.timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value); // 設(shè)置定時器
3.timer_delete(timer_t timerid); // 刪除定時器
示例代碼:
#include
#include
void myfunction(int signo) {
// 定時器回調(diào)函數(shù)
}
int mn(int argc, char *argv[]) {
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
// 設(shè)置定時器回調(diào)函數(shù)
sev.sigev_notify_function = myfunction;
sev.sigev_notify = SIGEV_SIGNAL;
// 創(chuàng)建定時器
timer_create(CLOCK_REALTIME, &sev, &timerid);
// 設(shè)置定時器超時時間
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 20230000;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 20230000;
// 啟動定時器
timer_settime(timerid, 0, &its, NULL);
// 主循環(huán)
while(1) {
// 等待定時器回調(diào)函數(shù)執(zhí)行完畢
}
// 刪除定時器
timer_delete(timerid);
return 0;
}
五、RT高精度定時器
RT是一個開源的實時操作系統(tǒng)擴展,它提供了一組高精度的定時器函數(shù),可以實現(xiàn)微秒級別的高精度計時。RT主要有以下幾個接口:
1.rt_task_delete(RT_TASK *task); // 刪除任務(wù)
2.rt_task_create(RT_TASK *task, const char *name, int stack_size, int priority, int mode); // 創(chuàng)建實時任務(wù)
3.rt_timer_start(RTTIMER *timer, void (*function)(union sigval), void *arg, RTIME timer_value); // 啟動定時器
4.rt_timer_stop(RTTIMER *timer); // 停止定時器
示例代碼:
#include
#include
#include
#include
RT_TASK mytask;
RTTIMER mytimer;
void myfunction(union sigval arg) {
// 定時器回調(diào)函數(shù)
}
int mn(int argc, char *argv[]) {
RTIME period;
// 創(chuàng)建實時任務(wù)
rt_task_create(&mytask, “mytask”, 0, 99, 0);
// 啟動實時任務(wù)
rt_task_start(&mytask, mytask_function, NULL);
// 啟動定時器
period = nano2count(20230000);
rt_timer_start(&mytimer, myfunction, NULL, period);
// 主循環(huán)
while(1) {
// 等待定時器回調(diào)函數(shù)執(zhí)行完畢
}
// 刪除定時器
rt_timer_stop(&mytimer);
// 刪除實時任務(wù)
rt_task_delete(&mytask);
return 0;
}
六、
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián)為您提供網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù)!
如何在linux系統(tǒng)上設(shè)置定時執(zhí)行任務(wù)
你可巧遲灶以通過編寫一個 SHELL 腳本程序(在編程過程孝扮旦睜中會用到 Linux 系統(tǒng)的 at 命令),就可以實現(xiàn)在指定的時間執(zhí)行指定的任務(wù)。
20ms精確定時 linux的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于20ms精確定時 linux,Linux系統(tǒng)20ms高精度時序?qū)崿F(xiàn)方法,如何在linux系統(tǒng)上設(shè)置定時執(zhí)行任務(wù)的信息別忘了在本站進行查找喔。
香港服務(wù)器選創(chuàng)新互聯(lián),香港虛擬主機被稱為香港虛擬空間/香港網(wǎng)站空間,或者簡稱香港主機/香港空間。香港虛擬主機特點是免備案空間開通就用, 創(chuàng)新互聯(lián)香港主機精選cn2+bgp線路訪問快、穩(wěn)定!
當前標題:Linux系統(tǒng)20ms高精度時序?qū)崿F(xiàn)方法(20ms精確定時linux)
URL鏈接:http://m.5511xx.com/article/coccejc.html


咨詢
建站咨詢
