原文出处:点击打开链接
使用定时器的目的无非是为了周期性的执行某一任务,或者是到了一个指定时间去执行某一个任务。要达到这一目的,一般有两个常见的比较有效的方法。一个是用linux内部的三个定时器,另一个是用sleep, usleep函数让进程睡眠一段时间,使用alarm定时发出一个信号,还有那就是用gettimeofday, difftime等自己来计算时间间隔,然后时间到了就执行某一任务,但是这种方法效率低,所以不常用。
1
2
3
4
5
6
7
8
9
10
11
12
|
NAME
alarm - set an alarm
clock
for
delivery of a
signal
SYNOPSIS
#include
unsigned
int
alarm(unsigned
int
seconds);
DESCRIPTION
alarm arranges
for
a SIGALRM
signal
to be delivered to the process in
seconds seconds.
If seconds is zero, no
new
alarm is scheduled.
In any event any previously set alarm is cancelled.
[code=c]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1 cat timer.c
2 #include
3 #include
4 #include 5 #include <
signal
.h>
6
7
void
func()
8 {
9
printf
(
"2 s reached.\n"
);
10 }
11
12
int
main()
13 {
14
signal
(SIGALRM,func);
15 alarm(2);
16
while
(1);
17
return
0;
18 }
19
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
1 #include
2 #include <
signal
.h>
3 #include 4
5
/*
6 *******************************************************************************************************
7 ** Function name: main()
8 ** Descriptions : Demo for timer.
9 ** Input : NONE
10 ** Output : NONE
11 ** Created by : Chenxibing
12 ** Created Date : 2005-12-29
13 **-----------------------------------------------------------------------------------------------------
14 ** Modified by :
15 ** Modified Date:
16 **-----------------------------------------------------------------------------------------------------
17 *******************************************************************************************************
18 */
19
int
limit = 10;
20
/* signal process */
21
void
timeout_info(
int
signo)
22 {
23
if
(limit == 0)
24 {
25
printf
(
"Sorry, time limit reached.\n"
);
26
return
;
27 }
28
printf
(
"only %d senconds left.\n"
, limit--);
29 }
30
31
/* init sigaction */
32
void
init_sigaction(
void
)
33 {
34
struct
sigaction act;
35
36 act.sa_handler = timeout_info;
37 act.sa_flags = 0;
38 sigemptyset(&act.sa_mask);
39 sigaction(SIGPROF, &act, NULL);
40 }
41
42
/* init */
43
void
init_time(
void
)
44 {
45
struct
itimerval val;
46
47 val.it_value.tv_sec = 1;
48 val.it_value.tv_usec = 0;
49 val.it_interval = val.it_value;
50 setitimer(ITIMER_PROF, &val, NULL);
51 }
52
53
54
int
main(
void
)
55 {
56 init_sigaction();
57 init_time();
58
printf
(
"You have only 10 seconds for thinking.\n"
);
59
60
while
(1);
61
return
0;
62 }
63
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include
#include
#include
#include
static
char
msg[] =
"I received a msg.\n"
;
int
len;
void
show_msg(
int
signo)
{
write(STDERR_FILENO, msg, len);
}
int
main()
{
struct
sigaction act;
union
sigval tsval;
act.sa_handler = show_msg;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(50, &act, NULL);
len =
strlen
(msg);
while
( 1 )
{
sleep(2);
/*睡眠2秒*/
/*向主进程发送信号,实际上是自己给自己发信号*/
sigqueue(getpid(), 50, tsval);
}
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include
#include
#include
#include
#include
static
char
msg[] =
"I received a msg.\n"
;
int
len;
static
time_t
lasttime;
void
show_msg(
int
signo)
{
write(STDERR_FILENO, msg, len);
}
int
main()
{
struct
sigaction act;
union
sigval tsval;
act.sa_handler = show_msg;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(50, &act, NULL);
len =
strlen
(msg);
time
(&lasttime);
while
( 1 )
{
time_t
nowtime;
/*获取当前时间*/
time
(&nowtime);
/*和上一次的时间做比较,如果大于等于2秒,则立刻发送信号*/
if
(nowtime - lasttime >= 2)
{
/*向主进程发送信号,实际上是自己给自己发信号*/
sigqueue(getpid(), 50, tsval);
lasttime = nowtime;
}
}
return
0;
}
|