注:原文頭文件缺失,有時間再添加
Linux下的定時器有兩種,以下分別介紹:
1、alarm
如果不要求很精確的話,用alarm()和signal()就夠了
unsigned int alarm(unsigned int seconds)
函數說明: alarm()用來設置信號SIGALRM在經過參數seconds指定的秒數後傳送給目前的進程。如果參數seconds爲0,則之前設置的鬧鐘會被取消,並將剩下的時間返回。
返回值: 返回之前鬧鐘的剩餘秒數,如果之前未設鬧鐘則返回0。
alarm()執行後,進程將繼續執行,在後期(alarm以後)的執行過程中將會在seconds秒後收到信號SIGALRM並執行其處理函數。
#include #include #include void sigalrm_fn(int sig) { printf("alarm!\n"); alarm(2); return; } int main(void) { signal(SIGALRM, sigalrm_fn); alarm(1); while(1) pause(); } |
2、setitimer()
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));
setitimer()比alarm功能強大,支持3種類型的定時器:
ITIMER_REAL : 以系統真實的時間來計算,它送出SIGALRM信號。
ITIMER_VIRTUAL : -以該進程在用戶態下花費的時間來計算,它送出SIGVTALRM信號。
ITIMER_PROF : 以該進程在用戶態下和內核態下所費的時間來計算,它送出SIGPROF信號。
setitimer()第一個參數which指定定時器類型(上面三種之一);第二個參數是結構itimerval的一個實例;第三個參數可不做處理。
setitimer()調用成功返回0,否則返回-1。
下面是關於setitimer調用的一個簡單示範,在該例子中,每隔一秒發出一個SIGALRM,每隔0.5秒發出一個SIGVTALRM信號:
#include #include #include #include #include #include int sec; void sigroutine(int signo){ switch (signo){ case SIGALRM: printf("Catch a signal -- SIGALRM \n"); signal(SIGALRM, sigroutine); break; case SIGVTALRM: printf("Catch a signal -- SIGVTALRM \n"); signal(SIGVTALRM, sigroutine); break; } return; } int main() { struct itimerval value, ovalue, value2; //(1) sec = 5; printf("process id is %d\n", getpid()); signal(SIGALRM, sigroutine); signal(SIGVTALRM, sigroutine); value.it_value.tv_sec = 1; value.it_value.tv_usec = 0; value.it_interval.tv_sec = 1; value.it_interval.tv_usec = 0; setitimer(ITIMER_REAL, &value, &ovalue); //(2) value2.it_value.tv_sec = 0; value2.it_value.tv_usec = 500000; value2.it_interval.tv_sec = 0; value2.it_interval.tv_usec = 500000; setitimer(ITIMER_VIRTUAL, &value2, &ovalue); for(;;) ; } |
(1) struct itimerval
struct itimerval { struct timeval it_interval; /* timer interval */ struct timeval it_value; /* current value */ }; itimerval: i --> interval val --> value |
itimerval結構中的it_value是減少的時間,當這個值爲0的時候就發出相應的信號了. 然後再將it_value設置爲it_interval值.
(2) setitimer()
setitimer()爲其所在進程設置一個定時器,如果itimerval.it_interval不爲0(it_interval的兩個域都不爲0),則該定時器將持續有效(每隔一段時間就會發送一個信號)
注意:Linux信號機制基本上是從Unix系統中繼承過來的。早期Unix系統中的信號機制比較簡單和原始,後來在實踐中暴露出一些問題,因此,把那些建立在早期機制上的信號叫做"不可靠信號",信號值小於 SIGRTMIN(SIGRTMIN=32,SIGRTMAX=63)的信號都是不可靠信號。這就是"不可靠信號"的來源。它的主要問題是:進程每次處理信號後,就將對信號的響應設置爲默認動作。在某些情況下,將導致對信號的錯誤處理;因此,用戶如果不希望這樣的操作,那麼就要在信號處理函數結尾再一次調用signal(),重新安裝該信號。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
setitimer()爲Linux的API,並非C語言的Standard Library,setitimer()有兩個功能,一是指定一段時間後,才執行某個function,二是每間格一段時間就執行某個function,以下程序demo如何使用setitimer()。
- /*
- Filename : timer.cpp
- Compiler : gcc 4.1.0 on Fedora Core 5
- Description : setitimer() set the interval to run function
- Synopsis : #include <sys/time.h>
- int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
- struct itimerval {
- struct timerval it_interval;
- struct timerval it_value;
- };
- struct timeval {
- long tv_sec;
- long tv_usec;
- }
- Release : 11/25/2006
- */
- #include <stdio.h> // for printf()
- #include <unistd.h> // for pause()
- #include <signal.h> // for signal()
- #include <string.h> // for memset()
- #include <sys/time.h> // struct itimeral. setitimer()
- void printMsg(int);
- int main() {
- // Get system call result to determine successful or failed
- int res = 0;
- // Register printMsg to SIGALRM
- signal(SIGALRM, printMsg);
- struct itimerval tick;
- // Initialize struct
- memset(&tick, 0, sizeof(tick));
- // Timeout to run function first time
- tick.it_value.tv_sec = 1; // sec
- tick.it_value.tv_usec = 0; // micro sec.
- // Interval time to run function
- tick.it_interval.tv_sec = 1;
- tick.it_interval.tv_usec = 0;
- // Set timer, ITIMER_REAL : real-time to decrease timer,
- // send SIGALRM when timeout
- res = setitimer(ITIMER_REAL, &tick, NULL);
- if (res) {
- printf("Set timer failed!!\n");
- }
- // Always sleep to catch SIGALRM signal
- while(1) {
- pause();
- }
- return 0;
- }
- void printMsg(int num) {
- printf("%s","Hello World!!\n");
- }
/*
Filename : timer.cpp
Compiler : gcc 4.1.0 on Fedora Core 5
Description : setitimer() set the interval to run function
Synopsis : #include <sys/time.h>
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
struct itimerval {
struct timerval it_interval;
struct timerval it_value;
};
struct timeval {
long tv_sec;
long tv_usec;
}
Release : 11/25/2006
*/
#include <stdio.h> // for printf()
#include <unistd.h> // for pause()
#include <signal.h> // for signal()
#include <string.h> // for memset()
#include <sys/time.h> // struct itimeral. setitimer()
void printMsg(int);
int main() {
// Get system call result to determine successful or failed
int res = 0;
// Register printMsg to SIGALRM
signal(SIGALRM, printMsg);
struct itimerval tick;
// Initialize struct
memset(&tick, 0, sizeof(tick));
// Timeout to run function first time
tick.it_value.tv_sec = 1; // sec
tick.it_value.tv_usec = 0; // micro sec.
// Interval time to run function
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
// Set timer, ITIMER_REAL : real-time to decrease timer,
// send SIGALRM when timeout
res = setitimer(ITIMER_REAL, &tick, NULL);
if (res) {
printf("Set timer failed!!\n");
}
// Always sleep to catch SIGALRM signal
while(1) {
pause();
}
return 0;
}
void printMsg(int num) {
printf("%s","Hello World!!\n");
}
當setitimer()所執行的timer時間到了,會呼叫SIGALRM signal,所以在第30行用signal()將要執行的function指定給SIGALRM。 在第43行呼叫setitimer()設定timer,但setitimer()第二個參數是sturct,負責設定timeout時間,所以第36行到第 40行設定此struct。itimerval.it_value設定第一次執行function所延遲的秒數, itimerval.it_interval設定以後每幾秒執行function,所以若只想延遲一段時間執行function,只要設定 itimerval.it_value即可,若要設定間格一段時間就執行function,則it_value和it_interval都要設定,否則 funtion的第一次無法執行,就別說以後的間隔執行了。 第36行和第39行的tv_sec爲sec,第37行和40行爲micro sec(0.001 sec)。 第43行的第一個參數ITIMER_REAL,表示以real-time方式減少timer,在timeout時會送出SIGALRM signal。第三個參數會存放舊的timeout值,如果不需要的話,指定NULL即可。 第47 行的pause(),命令系統進入sleep狀態,等待任何signal,一定要用while(1)無窮循環執行pause(),如此才能一直接收 SIGALRM signal以間隔執行function,若拿掉while(1),則function只會執行一次而已。
---------------------------------------------------------------------------------------------------------------------------------------------------------
linux下還有一種高精度的定時器,那就是posix_timer.我記得以前看代碼的時候CLOCK_REALTIME的定時器似乎用的就是rdtsc指令,不過現在不確定了,先放到一邊。原理上來說,可以在變頻的時候也使用rdtsc指令,因爲CPU的頻率我們也是知道的,變頻的時候內核也是知道的。
下面是我的timer_create的例子,編譯的時候要加上rt庫,這是linux的realtime庫:
gcc -o test test.c
#include <stdio.h> #define rdtsc(low,high) __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high)) timer_t tt; void handler (int sig, siginfo_t * extra, void *cruft) int main () sigfillset (&sigset); struct sigaction sa; if (sigaction (SIGRTMIN, &sa, NULL) < 0) struct sigevent timer_event; timer.it_interval.tv_sec = 0; timer_event.sigev_notify = SIGEV_SIGNAL; if (timer_create (CLOCK_REALTIME, &timer_event, &tt) < 0) if (timer_settime (tt, 0, &timer, NULL) < 0) while (i++ < 10) return 0; |
輸出結果:
time:166081, 1934350847, [1934350847] 2163HZ
time:166081, 2120528291, [186177444] 1861HZ
time:166081, 2306679576, [186151285] 1861HZ
time:166081, 2494695630, [188016054] 1880HZ
time:166081, 2680865389, [186169759] 1861HZ
time:166081, 2867018473, [186153084] 1861HZ
time:166081, 3053152230, [186133757] 1861HZ
time:166081, 3239309935, [186157705] 1861HZ
time:166081, 3425467261, [186157326] 1861HZ
time:166081, 3611639266, [186172005] 1861HZ