在Linux實現一個定時器,不像Win32下那樣直觀。在Win32調用SetTimer就行了,在Linux下則沒有相應函數可以直接調用。定時器作為一個常用的功能,在Linux當然也有相應實現。下面我們看看幾種常用的方法。
要實現定時器功能,最土的辦法實現莫過于用sleep/usleep來實現了。當然,它會阻塞當前線程,除了處理定時功能外,什么活也干不了。當然要解決這個問題不難,創建一個單獨的線程來負責定時器,其它線程負責正常的任務就行了。
要實現定時器功能,最簡單的辦法就是ALarm信號。這種方法簡單,也相應的缺陷:用信號實現效率較低;最小精度為1秒,無法實現高精度的定義器。簡單示例:
#include <stdio.h> #include <signal.h> static void timer(int sig) { if(sig == SIGALRM) { printf("timer\n"); } return; } int main(int argc, char* argv[]) { signal(SIGALRM, timer); alarm(1); getchar(); return 0; } |
(setitimer和alarm有類似的功能,也是通過信號來實現)
最優雅的方法是使用RTC機制。利用select函數,你可以用單線程實現定時器,同時還可以處理其它任務。簡單示例:
#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> 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, 4) < 0) { perror("ioctl(RTC_IRQP_SET)"); close(fd); exit(errno); } /*Set the freq as 4Hz*/ if(ioctl(fd, RTC_IRQP_SET, 4) < 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; } |