带有alarm()的recvfrom()超时

发布于 2024-11-05 23:13:07 字数 682 浏览 2 评论 0原文

我正在调试以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2024-11-12 23:13:07

siginterrupt(3) 联机帮助页不正确(版本Linux 联机帮助页集的 3.33 更正了错误)。 glibc 信号的默认行为是建立一个不中断系统调用的信号。您可以使用 strace 亲自查看这一点:

void handler(int unused) {}
int main(void)
{
  signal(SIGALRM, handler);
}

$ strace -e trace=rt_sigaction ./a.out
rt_sigaction(SIGALRM, 
             {0x4005b4, [ALRM], SA_RESTORER|SA_RESTART, 0x7fe732d3d490}, 
             {SIG_DFL, [], 0}, 8) = 0

注意其中的 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's signal is to establish a signal that does not interrupt system calls. You can see this for yourself with strace:

void handler(int unused) {}
int main(void)
{
  signal(SIGALRM, handler);
}

$ strace -e trace=rt_sigaction ./a.out
rt_sigaction(SIGALRM, 
             {0x4005b4, [ALRM], SA_RESTORER|SA_RESTART, 0x7fe732d3d490}, 
             {SIG_DFL, [], 0}, 8) = 0

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 using sigaction, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文