timer的使用介绍
时间:2010-07-02 来源:cjjchong
Linux提供了兩種基本的Timer機制可以使用:
•alarm
•setitimer ● alarm #include
<unistd.h> unsigned
int
alarm
(unsigned
int
seconds
);這 是一個簡單的設定Timer介面。當呼叫了alarm( n )後,等待n秒後,就會觸發一次的SIGALRM的signal,所以必須要在呼叫alarm前,先設好SIGALRM的handler function才行。而當乎呼alarm(0)時,則表示停止當前的timer處理,不要發出SIGALRM的signal。 Return value : 返回上一次呼叫alarm的剩餘秒數。若未設定alarm,則返回0。 Example : 第一次等待1秒後觸發Timer,之後每隔2秒觸發一次。 #include <iostream>
#include <unistd.h>
#include <signal.h> using
namespace
std
; void
my_alarm_handler
(
int
a
)
{ cerr
<<
"my_alarm_handler"
<<
endl
; alarm
(
2
);
//重新設定
} int
main
()
{ signal
(
SIGALRM
,
my_alarm_handler
); alarm
(
1
);
while
(
1
){}
return
0
;
} --------------------------------------------------------------------------------------
● setitimer #include
<sys/time.h> #define
ITIMER_REAL
0 #define
ITIMER_VIRTUAL
1 #define
ITIMER_PROF
2
int
getitimer
(int
which
, struct
itimerval
*value
); int
setitimer
(int
which
, const
struct
itimerval
*value
,
struct
itimerval
*ovalue
);setitimer與getitimer提供了三種類別的Timer使用: •ITIMER_REAL : 以系統真實的時間來計算,觸發時會送出SIGALRM 。
•ITIMER_VIRTUAL : 只計算process真正在執行的時間(在User Mode的處理),觸發時會送出SIGVTALRM 。
•ITIMER_PROF : 計算該process在User Mode與Kernel Mode的處理時間,觸發時送出SIGPROF 。
透 過第一個參數which指定要使用哪一種Timer (ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF )。setitimer是用來設定該種Timer的觸發時間為多少。getitimer則是取得上一次Timer設定的時間。設定的內容是一個系統內建的 struct itimerval: struct
itimerval
{
struct
timeval it_interval
;
/* next value : 下一次觸發所需的時間*/
struct
timeval it_value
;
/* current value : 目前距離觸發時間點 剩餘的時間*/
};
struct
timeval
{
long
tv_sec
;
/* seconds */
long
tv_usec
;
/* microseconds */
}; setitimer由第二個參數value 設定觸發的時間。第三個參數ovalue 用來取得上一次 setitimer設定的itimerval值(此參數可以為NULL)。值得注意的是,根據itimerval裡變數的意義,當it_interval設定為0時,Timer只會觸發一次。而it_value設定為0時,代表Timer結束。 Return value : 如果成功則return 0,失敗則return -1。 Example : 第一次等待1秒後觸發Timer,之後每隔2秒觸發一次。 #include <iostream>
#include <sys/time.h>
#include <signal.h> using
namespace
std
; void
my_alarm_handler
(
int
a
)
{ cerr
<<
"test "
<<
endl
;
}
int
main
(){
struct
itimerval t
; t
.
it_interval
.
tv_usec
=
0
; t
.
it_interval
.
tv_sec
=
2
; t
.
it_value
.
tv_usec
=
0
; t
.
it_value
.
tv_sec
=
1
;
if
(
setitimer
(
ITIMER_REAL
,
&
t
,
NULL
)
<
0
){ cerr
<<
"settimer error."
<<
endl
;
return
-
1
;
} signal
(
SIGALRM
,
my_alarm_handler
);
while
(
1
){ sleep
(
2
);
}
return
0
;
} ● 根據以上,可知Linux內建的Timer還是有點簡陋,而且setitimer同一時間只能處理3個Timer,如果應用程式需要多個Timer的話,這個Linux內建的Timer可能就不敷需求了!
•setitimer ● alarm #include
<unistd.h> unsigned
int
alarm
(unsigned
int
seconds
);這 是一個簡單的設定Timer介面。當呼叫了alarm( n )後,等待n秒後,就會觸發一次的SIGALRM的signal,所以必須要在呼叫alarm前,先設好SIGALRM的handler function才行。而當乎呼alarm(0)時,則表示停止當前的timer處理,不要發出SIGALRM的signal。 Return value : 返回上一次呼叫alarm的剩餘秒數。若未設定alarm,則返回0。 Example : 第一次等待1秒後觸發Timer,之後每隔2秒觸發一次。 #include <iostream>
#include <unistd.h>
#include <signal.h> using
namespace
std
; void
my_alarm_handler
(
int
a
)
{ cerr
<<
"my_alarm_handler"
<<
endl
; alarm
(
2
);
//重新設定
} int
main
()
{ signal
(
SIGALRM
,
my_alarm_handler
); alarm
(
1
);
while
(
1
){}
return
0
;
} --------------------------------------------------------------------------------------
● setitimer #include
<sys/time.h> #define
ITIMER_REAL
0 #define
ITIMER_VIRTUAL
1 #define
ITIMER_PROF
2
int
getitimer
(int
which
, struct
itimerval
*value
); int
setitimer
(int
which
, const
struct
itimerval
*value
,
struct
itimerval
*ovalue
);setitimer與getitimer提供了三種類別的Timer使用: •ITIMER_REAL : 以系統真實的時間來計算,觸發時會送出SIGALRM 。
•ITIMER_VIRTUAL : 只計算process真正在執行的時間(在User Mode的處理),觸發時會送出SIGVTALRM 。
•ITIMER_PROF : 計算該process在User Mode與Kernel Mode的處理時間,觸發時送出SIGPROF 。
透 過第一個參數which指定要使用哪一種Timer (ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF )。setitimer是用來設定該種Timer的觸發時間為多少。getitimer則是取得上一次Timer設定的時間。設定的內容是一個系統內建的 struct itimerval: struct
itimerval
{
struct
timeval it_interval
;
/* next value : 下一次觸發所需的時間*/
struct
timeval it_value
;
/* current value : 目前距離觸發時間點 剩餘的時間*/
};
struct
timeval
{
long
tv_sec
;
/* seconds */
long
tv_usec
;
/* microseconds */
}; setitimer由第二個參數value 設定觸發的時間。第三個參數ovalue 用來取得上一次 setitimer設定的itimerval值(此參數可以為NULL)。值得注意的是,根據itimerval裡變數的意義,當it_interval設定為0時,Timer只會觸發一次。而it_value設定為0時,代表Timer結束。 Return value : 如果成功則return 0,失敗則return -1。 Example : 第一次等待1秒後觸發Timer,之後每隔2秒觸發一次。 #include <iostream>
#include <sys/time.h>
#include <signal.h> using
namespace
std
; void
my_alarm_handler
(
int
a
)
{ cerr
<<
"test "
<<
endl
;
}
int
main
(){
struct
itimerval t
; t
.
it_interval
.
tv_usec
=
0
; t
.
it_interval
.
tv_sec
=
2
; t
.
it_value
.
tv_usec
=
0
; t
.
it_value
.
tv_sec
=
1
;
if
(
setitimer
(
ITIMER_REAL
,
&
t
,
NULL
)
<
0
){ cerr
<<
"settimer error."
<<
endl
;
return
-
1
;
} signal
(
SIGALRM
,
my_alarm_handler
);
while
(
1
){ sleep
(
2
);
}
return
0
;
} ● 根據以上,可知Linux內建的Timer還是有點簡陋,而且setitimer同一時間只能處理3個Timer,如果應用程式需要多個Timer的話,這個Linux內建的Timer可能就不敷需求了!
相关阅读 更多 +
排行榜 更多 +

<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />
飞行射击 下载
<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />
飞行射击 下载
<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />
飞行射击 下载- 4 <img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />下载73.78MB · 98℃
1970-01-01
- 5 <img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />下载43.7 MB · 95℃
1970-01-01
- 6 <img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />下载110.69MB · 91℃
1970-01-01