处理多个信号
我有一个关于处理信号的问题。假设如果我们收到 SIGINT
信号,我们应该打印“Received Signal”。如果在十秒内处理程序收到另一个信号,它应该打印“关闭”,然后以状态 1 退出。
我的代码如下:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handler(int);
void secondhandler(int);
void alrmhandler(int);
void alrmhandler(int alrmsig) {
alarm(0);
}
void secondhandler(int sig) {
/* after recieving second signal prints shutting down and exit */
printf("Shutting Down\n");
exit(1);
}
void handler(int sig) {
/* receive first SIGINT signal */
printf("Received Signal\n");
/* handle for the alarm function */
signal(SIGALRM, alrmhandler);
/* start 10s alarm */
alarm(10);
/* catch second SIGINT signal within 10s*/
signal(SIGINT, secondhandler);
}
int main(void) {
signal(SIGINT, handler);
printf("Hello World!\n");
for (;;) {
/* infinite loop */
}
return 0;
}
我尝试使用 Dev-C++ 编译它,但失败并显示 SIGALRM
未声明(在此函数中首次使用)。
无论如何,我想知道的是这段代码是否正确。我实际上对 alrmhandler()
有点不确定。我应该忽略 SIGALRM
吗?
I have a question about handling a signal. Assume that if we receive SIGINT
signal we should print "Received Signal". If within ten seconds the handler receives another signal it should print "Shutting Down" then exit with status 1.
I made my code like this:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handler(int);
void secondhandler(int);
void alrmhandler(int);
void alrmhandler(int alrmsig) {
alarm(0);
}
void secondhandler(int sig) {
/* after recieving second signal prints shutting down and exit */
printf("Shutting Down\n");
exit(1);
}
void handler(int sig) {
/* receive first SIGINT signal */
printf("Received Signal\n");
/* handle for the alarm function */
signal(SIGALRM, alrmhandler);
/* start 10s alarm */
alarm(10);
/* catch second SIGINT signal within 10s*/
signal(SIGINT, secondhandler);
}
int main(void) {
signal(SIGINT, handler);
printf("Hello World!\n");
for (;;) {
/* infinite loop */
}
return 0;
}
I tried to compile it with Dev-C++, but it failed with SIGALRM
was undeclared (first use in this function).
Anyway, what I want to know is if this code is right. I'm actually kinda not sure with the alrmhandler()
. Should I ignore the SIGALRM
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Windows 平台,则您能够发送的唯一信号是:SIGABRT、SIGFPE、SIGILL、SIGINT、SIGSEGV 或 SIGTERM。
If you are on a Windows platform, the only signals you will be able to send are : SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, or SIGTERM.
你写:
不完全正确。 printf() 不是 异步信号安全,因此不应从信号处理程序中调用,除非您非常确定这样做是安全的。在您提供的代码中执行此操作不安全。
一般来说,alarm() 技术容易出现竞争。您的十秒警报可能会在 secondaryhandler() 函数执行过程中到期。为了防止这种情况,您可以屏蔽信号,以使用更复杂的信号处理来补偿。函数。
有更优雅/灵活的方法来实现您想要的超时,但这可能是一个更适合 codereview.stackexchange.com 的问题。
You write:
Not entirely. printf() is not async-signal-safe, and so should not be called from within a signal handler unless you are very sure it is safe to do so. It is not safe to do so within the code you provide.
The alarm() technique is, generally, race-prone. Your ten second alarm might expire in the middle of your secondhandler() function. To guard against this, you might mask out signals to compensate with a more sophisticated signal manipulation function.
There are more elegant/flexible ways of implementing the timeout you desire, but that's perhaps a question better suited for codereview.stackexchange.com.