带有alarm()的recvfrom()超时
我正在调试以下代码:
signal(SIGALRM, testt);
alarm(1);
result = recvfrom( listening_socket, buf, maxlen, 0, &from, &fromlen );
printf("stoped\n");
正如 man 3 siginterrupt
所描述的,警报应该中断系统调用,但在我的情况下却没有。警报处理程序被调用,但 recvfrom
并未中断。
但是,当使用 signal(2) 函数指定新的信号处理程序时,系统调用默认会被中断。
如果我在设置警报处理程序后添加 siginterrupt(SIGALRM, 1);
,则 recvfrom
会按预期中断。
我缺少什么?我的代码有什么问题吗?
注意:用 sigaction
替换 signal
不是我想要的。
I'm debugging the following code:
signal(SIGALRM, testt);
alarm(1);
result = recvfrom( listening_socket, buf, maxlen, 0, &from, &fromlen );
printf("stoped\n");
As described by man 3 siginterrupt
the alarm should interrupt the system call, but in my case it doesn't. Alarm handler is invoked, but recvfrom
is not interrupted.
However, when a new signal handler is specified with the signal(2) function, the system call is interrupted by default.
If I add siginterrupt(SIGALRM, 1);
after setting the alarm handler then recvfrom
is interrupted as expected.
What am I missing? What's wrong with my code?
NOTES: Replacing signal
with sigaction
is not what I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
siginterrupt(3)
联机帮助页不正确(版本Linux 联机帮助页集的 3.33 更正了错误)。 glibc 信号的默认行为是建立一个不中断系统调用的信号。您可以使用strace
亲自查看这一点:⟶
注意其中的 SA_RESTART。您可以继续做您正在做的事情 - 在建立处理程序后调用
siginterrupt(SIGALRM, 1)
- 或者您可以切换到使用sigaction
,这将让您首先按照自己想要的方式设置标志。你说你不想这样做(为什么?),但尽管如此,我还是建议这样做。The
siginterrupt(3)
manpage was incorrect (version 3.33 of the Linux manpage set corrects the error). The default behavior of glibc'ssignal
is to establish a signal that does not interrupt system calls. You can see this for yourself withstrace
:⟶
Note the SA_RESTART in there. You can either keep doing what you're doing -- call
siginterrupt(SIGALRM, 1)
after establishing the handler -- or you can switch to usingsigaction
, which will let you set the flags the way you want in the first place. You said you didn't want to do that (why?) but nonetheless that is what I would recommend.