在您的循环中,只需调用pause()
. 它会阻塞并仅在收到信号时返回,因此您可以运行代码,再次循环并重复。您需要一个信号处理程序来停止SIGALRM
终止您的程序,但它不必做任何事情,您可以将函数体留空。
例如:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
void timer_handler(int signum)
{
(void) signum; /* Avoids warning for unused argument */
}
int main(void)
{
struct sigaction sa;
sa.sa_handler = timer_handler;
sa.sa_mask = 0;
sa.sa_flags = 0;
sigaction(SIGALRM, &sa, NULL);
struct itimerval timer;
timer.it_value.tv_sec = 1;
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &timer, NULL);
while ( 1 ) {
pause();
printf("Timer expired - get GPS data.\n");
}
return 0;
}
产生输出:
paul@horus:~/src/sandbox$ ./alarm
Timer expired - get GPS data.
Timer expired - get GPS data.
Timer expired - get GPS data.
Timer expired - get GPS data.
Timer expired - get GPS data.
Timer expired - get GPS data.
Timer expired - get GPS data.
Timer expired - get GPS data.
^C
paul@horus:~/src/sandbox$
这是一个相当粗略的解决方案。如果代码有时运行时间可能比计时器间隔更长,那么事情就会变得不可靠,您有时可能会跳过一个信号。你可能关心也可能不关心这个。对于更复杂的方法,您可以阻止接收,SIGALRM
然后sigsuspend()
使用解除阻塞的信号掩码调用,因为知道解除阻塞和等待信号将是原子操作是安全的。这是该方法的示例:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
void timer_handler(int signum)
{
static const char msg[] = "Handler was called.\n";
write(STDIN_FILENO, msg, sizeof(msg) - 1);
(void) signum;
}
int main(void)
{
struct sigaction sa;
sa.sa_handler = timer_handler;
sa.sa_mask = 0;
sa.sa_flags = 0;
sigaction(SIGALRM, &sa, NULL);
struct itimerval timer;
timer.it_value.tv_sec = 1;
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &timer, NULL);
sigset_t old_mask, block_mask;
sigemptyset(&block_mask);
sigaddset(&block_mask, SIGALRM);
sigprocmask(SIG_BLOCK, &block_mask, &old_mask);
sleep(3); /* To demonstrate signal handler won't be
called until sigsuspend() is called, timer
is firing every second while we're sleeping */
while ( 1 ) {
sigsuspend(&old_mask);
printf("Timer expired - get GPS data.\n");
}
return 0;
}
输出:
paul@horus:~/src/sandbox$ ./alarm2
Handler was called.
Timer expired - get GPS data.
Handler was called.
Timer expired - get GPS data.
Handler was called.
Timer expired - get GPS data.
Handler was called.
Timer expired - get GPS data.
Handler was called.
Timer expired - get GPS data.
Handler was called.
Timer expired - get GPS data.
^C
paul@horus:~/src/sandbox$
为简洁起见,上述示例中省略了错误检查,但您的代码当然应该在每个系统调用中都包含它。