信号处理程序停止计时器用C [英] Signal Handler to stop Timer in C
问题描述
我想有一个信号处理程序停止计时,但不退出我的程序。我应该如何着手。我想StopTimer来处理信号,停止计时
I am trying to have a signal handler stop a timer without exiting my program. How should I go about. I want StopTimer to handle the signal to stop the timer
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#define INTERVAL 2 // number of seconds to go off
int main(int argc, char* argv[]) {
TimerSet(INTERVAL);
while(1)
{
// do stuff
}
return 0;
}
void TimerSet(int interval)
{
printf("starting timer\n");
struct itimerval it_val;
// interval value
it_val.it_value.tv_sec = interval;
it_val.it_interval = it_val.it_value;
// on SIGALRM, close window
if (signal(SIGALRM, TimerStop) == SIG_ERR)
{
perror("Unable to catch SIGALRM");
exit(1);
}
// set interval timer, returns SIGALRM on expiration
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1)
{
perror("error calling setitimer()");
exit(1);
}
}
void TimerStop(int signum)
{
printf("Timer ran out! Stopping timer\n");
exit(signum);
}
我试图setitimer函数时间间隔设置为0,但我不知道如何使用相同的计时器TimerStop信号处理函数中
I tried to set the setitimer interval to 0, but I am not sure how to use the same timer within the TimerStop signal handler function
推荐答案
只要设置 it_interval
零,你会得到一个一次性的定时器。你不需要做任何事情在你的处理程序。
Just set it_interval
to zero, and you'll get a one-shot timer. You don't need to do anything with it in your handler.
有关实例,在该
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#define INTERVAL 2 // number of seconds to go off
void TimerStop(int signum) {
printf("Timer ran out! Stopping timer\n");
}
void TimerSet(int interval) {
printf("starting timer\n");
struct itimerval it_val;
it_val.it_value.tv_sec = interval;
it_val.it_value.tv_usec = 0;
it_val.it_interval.tv_sec = 0;
it_val.it_interval.tv_usec = 0;
if (signal(SIGALRM, TimerStop) == SIG_ERR) {
perror("Unable to catch SIGALRM");
exit(1);
}
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
perror("error calling setitimer()");
exit(1);
}
}
int main(int argc, char *argv[]) {
TimerSet(INTERVAL);
while (1) {
// do stuff
}
return 0;
}
消息定时跑出来!停止计时器
将只出现一次,而你的定时器将停止,没有你做任何事情。
the message "Timer ran out! Stopping timer"
will appear only once, and your timer will stop without you doing anything.
请注意,您需要填写您的结构itimerval
,当前您的$ C $的 tv_usec
成员C不这样做。如果你不这样做, it_interval
是极不可能是零,你的计时器将永远不会停止。
Note that you need to fill in the tv_usec
members of your struct itimerval
, which your current code does not do. If you don't, it_interval
is highly unlikely to be zero, and your timer will never stop.
的printf()
,与其他标准的IO功能一起,是不是真的安全地从信号处理程序调用,虽然在这种特殊情况下也不会造成你任何问题,因为主要code只是坐在一个循环,并没有做任何事情。
printf()
, along with the other standard IO functions, is not really safe to call from a signal handler, although in this particular case it won't cause you any problems, since the main code is just sitting in a loop and not doing anything.
另外,你调用presume 信号()
的宗旨 - 的sigaction()
是设置处理程序推荐的方式。 setitimer函数()
也过时了,现在,和 timer_settime()
推荐。
Also, presume you're calling signal()
on purpose - sigaction()
is the recommended way for setting handlers. setitimer()
is also obsolete, now, and timer_settime()
is recommended.
这篇关于信号处理程序停止计时器用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!