linux定時器setitimer

setitimer()爲Linux的API,並非C語言的Standard Library,setitimer()有兩個功能,一是指定一段時間後,才執行某個function,二是每間格一段時間就執行某個function,以下程序demo如何使用setitimer()。

 

view plaincopy to clipboardprint?
01./*
02.
03. 
04.
05.Filename    : timer.cpp
06.
07.Compiler    : gcc 4.1.0 on Fedora Core 5
08.
09.Description : setitimer() set the interval to run function
10.
11.Synopsis    : #include <sys/time.h>
12.
13.              int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
14.
15.              struct itimerval {
16.
17.                struct timerval it_interval;
18.
19.                struct timerval it_value;
20.
21.              };
22.
23. 
24.
25.              struct timeval {
26.
27.                long tv_sec;
28.
29.                long tv_usec;
30.
31.              }            
32.
33.Release     : 11/25/2006
34.
35.*/ 
36.
37.#include <stdio.h>    // for printf() 
38.
39.#include <unistd.h>   // for pause() 
40.
41.#include <signal.h>   // for signal() 
42.
43.#include <string.h>   // for memset() 
44.
45.#include <sys/time.h> // struct itimeral. setitimer() 
46. 
47. 
48. 
49.void printMsg(int); 
50. 
51. 
52. 
53.int main() { 
54. 
55.  // Get system call result to determine successful or failed 
56. 
57.  int res = 0; 
58. 
59.  // Register printMsg to SIGALRM 
60. 
61.  signal(SIGALRM, printMsg); 
62. 
63.   
64. 
65.  struct itimerval tick; 
66. 
67.  // Initialize struct 
68. 
69.  memset(&tick, 0, sizeof(tick)); 
70. 
71.  // Timeout to run function first time 
72. 
73.  tick.it_value.tv_sec = 1;  // sec 
74. 
75.  tick.it_value.tv_usec = 0; // micro sec. 
76. 
77.  // Interval time to run function 
78. 
79.  tick.it_interval.tv_sec = 1; 
80. 
81.  tick.it_interval.tv_usec = 0; 
82. 
83.  // Set timer, ITIMER_REAL : real-time to decrease timer, 
84. 
85.  //                          send SIGALRM when timeout 
86. 
87.  res = setitimer(ITIMER_REAL, &tick, NULL); 
88. 
89.  if (res) { 
90. 
91.    printf("Set timer failed!!/n"); 
92. 
93.  } 
94. 
95. 
96. 
97.  // Always sleep to catch SIGALRM signal 
98. 
99.  while(1) { 
100. 
101.    pause(); 
102. 
103.  } 
104. 
105. 
106. 
107.  return 0;   
108. 
109.} 
110. 
111. 
112. 
113.void printMsg(int num) { 
114. 
115.  printf("%s","Hello World!!/n"); 
116. 
117.} 
 
當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只會執行一次而已。


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/fanwenbo/archive/2008/07/13/2645362.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章