Linux下實(shí)現(xiàn)定時(shí)器Timer的幾種方法總結(jié)
定時(shí)器Timer應(yīng)用場(chǎng)景非常廣泛,在Linux下,有以下幾種方法:
1,使用sleep()和usleep()
其中sleep精度是1秒,usleep精度是1微妙,具體代碼就不寫(xiě)了。使用這種方法缺點(diǎn)比較明顯,在Linux系統(tǒng)中,sleep類(lèi)函數(shù)不能保證精度,尤其在系統(tǒng)負(fù)載比較大時(shí),sleep一般都會(huì)有超時(shí)現(xiàn)象。
2,使用信號(hào)量SIGALRM + alarm()
這種方式的精度能達(dá)到1秒,其中利用了*nix系統(tǒng)的信號(hào)量機(jī)制,首先注冊(cè)信號(hào)量SIGALRM處理函數(shù),調(diào)用alarm(),設(shè)置定時(shí)長(zhǎng)度,代碼如下:
#include <stdio.h> #include <signal.h> void timer(int sig) { if(SIGALRM == sig) { printf("timer\n"); alarm(1); //we contimue set the timer } return ; } int main() { signal(SIGALRM, timer); //relate the signal and function alarm(1); //trigger the timer getchar(); return 0; }
alarm方式雖然很好,但是無(wú)法首先低于1秒的精度。
3,使用RTC機(jī)制
RTC機(jī)制利用系統(tǒng)硬件提供的Real Time Clock機(jī)制,通過(guò)讀取RTC硬件/dev/rtc,通過(guò)ioctl()設(shè)置RTC頻率,代碼如下:
#include <stdio.h> #include <linux/rtc.h> #include <sys/ioctl.h> #include <sys/time.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> int main(int argc, char* argv[]) { unsigned long i = 0; unsigned long data = 0; int retval = 0; int fd = open ("/dev/rtc", O_RDONLY); if(fd < 0) { perror("open"); exit(errno); } /*Set the freq as 4Hz*/ if(ioctl(fd, RTC_IRQP_SET, 1) < 0) { perror("ioctl(RTC_IRQP_SET)"); close(fd); exit(errno); } /* Enable periodic interrupts */ if(ioctl(fd, RTC_PIE_ON, 0) < 0) { perror("ioctl(RTC_PIE_ON)"); close(fd); exit(errno); } for(i = 0; i < 100; i++) { if(read(fd, &data, sizeof(unsigned long)) < 0) { perror("read"); close(fd); exit(errno); } printf("timer\n"); } /* Disable periodic interrupts */ ioctl(fd, RTC_PIE_OFF, 0); close(fd); return 0; }
這種方式比較方便,利用了系統(tǒng)硬件提供的RTC,精度可調(diào),而且非常高。
4,使用select()
這種方法在看APUE神書(shū)時(shí)候看到的,方法比較冷門(mén),通過(guò)使用select(),來(lái)設(shè)置定時(shí)器;原理利用select()方法的第5個(gè)參數(shù),第一個(gè)參數(shù)設(shè)置為0,三個(gè)文件描述符集都設(shè)置為NULL,第5個(gè)參數(shù)為時(shí)間結(jié)構(gòu)體,代碼如下:
#include <sys/time.h> #include <sys/select.h> #include <time.h> #include <stdio.h> /*seconds: the seconds; mseconds: the micro seconds*/ void setTimer(int seconds, int mseconds) { struct timeval temp; temp.tv_sec = seconds; temp.tv_usec = mseconds; select(0, NULL, NULL, NULL, &temp); printf("timer\n"); return ; } int main() { int i; for(i = 0 ; i < 100; i++) setTimer(1, 0); return 0; }
這種方法精度能夠達(dá)到微妙級(jí)別,網(wǎng)上有很多基于select()的多線(xiàn)程定時(shí)器,說(shuō)明select()穩(wěn)定性還是非常好。
總結(jié):如果對(duì)系統(tǒng)要求比較低,可以考慮使用簡(jiǎn)單的sleep(),畢竟一行代碼就能解決;如果系統(tǒng)對(duì)精度要求比較高,則可以考慮RTC機(jī)制和select()機(jī)制。
以上就是小編為大家?guī)?lái)的Linux下實(shí)現(xiàn)定時(shí)器Timer的幾種方法總結(jié)全部?jī)?nèi)容了,希望大家多多支持腳本之家~
- Linux中date命令轉(zhuǎn)換日期提示date: illegal time format問(wèn)題解決
- linux中uptime命令的用法詳細(xì)解析
- linux下用time(NULL)函數(shù)和localtime()獲取當(dāng)前時(shí)間的方法
- linux或windows環(huán)境下pytorch的安裝與檢查驗(yàn)證(解決runtimeerror問(wèn)題)
- Linux下通過(guò)gettimeofday函數(shù)獲取程序段執(zhí)行時(shí)間【推薦】
- 利用linux的timerfd_create實(shí)現(xiàn)計(jì)時(shí)器示例分享
- 詳解Linux time 命令的使用
相關(guān)文章
linux中$符號(hào)的基礎(chǔ)用法總結(jié)
這篇文章主要給大家介紹了關(guān)于linux中$符號(hào)的基礎(chǔ)用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用linux系統(tǒng)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Linux Apache+Proftpd構(gòu)建虛擬主機(jī)時(shí)要注意的幾個(gè)安全問(wèn)題
Linux下Apache+Proftpd構(gòu)建虛擬主機(jī)時(shí)要注意的幾個(gè)安全問(wèn)題,大家可以參考下,有其它未完整的地方,大家可以補(bǔ)充下。2009-08-08linux下如何把進(jìn)程/線(xiàn)程綁定到特定cpu核上運(yùn)行
這篇文章主要介紹了linux下如何把進(jìn)程/線(xiàn)程綁定到特定cpu核上運(yùn)行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Apache運(yùn)行模式之standalone和inetd模式介紹
這篇文章主要介紹了Apache運(yùn)行模式之standalone和inetd模式介紹,本文講解了ServerType這個(gè)配置參數(shù),ServerType這個(gè)配置選項(xiàng)指定如何運(yùn)行Apache,需要的朋友可以參考下2015-06-06Windows系統(tǒng)下Apache服務(wù)器無(wú)法啟動(dòng)的問(wèn)題解決
這篇文章主要介紹了Windows系統(tǒng)下Apache服務(wù)器無(wú)法啟動(dòng)的問(wèn)題解決,大多數(shù)情況下還是端口被占用的問(wèn)題,需要的朋友可以參考下2015-07-07禁止網(wǎng)站顯示文件目錄列表的2個(gè)方法(htaccess)
這篇文章主要介紹了禁止網(wǎng)站顯示文件目錄列表的2個(gè)方法,需要的朋友可以參考下2016-04-04