Linux下實現(xiàn)定時器Timer的幾種方法

定時器Timer應用場景非常廣泛,在Linux下,有以下幾種方法:

1,使用sleep()和usleep()

其中sleep精度是1秒,usleep精度是1微妙,具體代碼就不寫了。使用這種方法缺點比較明顯,在Linux系統(tǒng)中,sleep類函數(shù)不能保證精度,尤其在系統(tǒng)負載比較大時,sleep一般都會有超時現(xiàn)象。

2,使用信號量SIGALRM + alarm()

這種方式的精度能達到1秒,其中利用了*nix系統(tǒng)的信號量機制,首先注冊信號量SIGALRM處理函數(shù),調用alarm(),設置定時長度,代碼如下:

#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方式雖然很好,但是無法首先低于1秒的精度。

3,使用RTC機制

RTC機制利用系統(tǒng)硬件提供的Real Time Clock機制,通過讀取RTC硬件/dev/rtc,通過ioctl()設置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,精度可調,而且非常高。

4,使用select()

這種方法在看APUE神書時候看到的,方法比較冷門,通過使用select(),來設置定時器;原理利用select()方法的第5個參數(shù),第一個參數(shù)設置為0,三個文件描述符集都設置為NULL,第5個參數(shù)為時間結構體,代碼如下:

#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;

}

這種方法精度能夠達到微妙級別,網上有很多基于select()的多線程定時器,說明select()穩(wěn)定性還是非常好。

總結:如果對系統(tǒng)要求比較低,可以考慮使用簡單的sleep(),畢竟一行代碼就能解決;如果系統(tǒng)對精度要求比較高,則可以考慮RTC機制和select()機制。

————————————————

版權聲明:本文為CSDN博主「lxmky」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權協(xié)議,轉載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/lxmky/java/article/details/7669296

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容