註:原文標頭檔缺失,有時間再添加
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 --> intervalval --> 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()。
view plaincopy to clipboardprint?
- /*
- 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.cppCompiler : gcc 4.1.0 on Fedora Core 5Description : setitimer() set the interval to run functionSynopsis : #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>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#define rdtsc(low,high) __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
timer_t tt;
void handler (int sig, siginfo_t * extra, void *cruft)
{
static last_i=0;
unsigned int i, j;
rdtsc(i,j);
printf ("time:%u, %u, [%u] %uHZ ", j, i, i-last_i, (i-last_i)*10/1000000);
last_i = i;
}
int main ()
{
int i=0;
sigset_t sigset;
sigfillset (&sigset);
sigdelset (&sigset, SIGRTMIN);
sigprocmask (SIG_SETMASK, &sigset, NULL);
struct sigaction sa;
sigfillset (&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
if (sigaction (SIGRTMIN, &sa, NULL) < 0)
{
perror ("sigaction failed ");
exit (-1);
}
struct sigevent timer_event;
struct itimerspec timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 100 * 1000 * 1000;
timer.it_value = timer.it_interval;
timer_event.sigev_notify = SIGEV_SIGNAL;
timer_event.sigev_signo = SIGRTMIN;
timer_event.sigev_value.sival_ptr = (void *) &tt;
if (timer_create (CLOCK_REALTIME, &timer_event, &tt) < 0)
{
perror ("timer_create failed");
exit (-1);
}
if (timer_settime (tt, 0, &timer, NULL) < 0)
{
perror ("timer_settime failed");
exit (-1);
}
while (i++ < 10)
{
pause ();
}
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
http://blog.chinaunix.net/uid-23215128-id-2521378.html