libevent的&安培;信号处理(特别是SIGCHLD) [英] libevent & signal handling (SIGCHLD specifically)

查看:254
本文介绍了libevent的&安培;信号处理(特别是SIGCHLD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续相关的问题,我发现多少有些
这里

This is somewhat of a followup to related question I found here.

在链接的问题,有一个使用 signalfd()并使用FD用的的libevent 。在这个问题的任择议定书没有列出他为何使用 signalfd()相对于libevent的信号处理设施。

In the linked question there is a mention of using signalfd() and using that fd with libevent. In that question the OP does not list why he is using signalfd() as opposed to the libevent signal handling facilities.

在这两种方法,你会处理信号处理程序之外的回调。

In both methods you would be processing the callback outside of the signal-handler.

本文档似乎警告在调度计时器信号事件回调。这看起来不正确(如我们将是一个信号处理程序上下文之外)。除了用于警告说,我不能看到一个好处与操作signalfd()

This documentation seems to warn about scheduling timer in the signal event callback. Which doesn't seem right (as we would be outside of a signal handler context). Aside for said warning I cant see a benefit to doing this with signalfd().

关于这两种方法之间或约警告的差不限输入

Any input about the difference between the two methods or about the warning

谢谢!

推荐答案

这libevent的源$ C ​​$ C(v2.0.19稳定)

from libevent's source code (v2.0.19-stable)

/ * signal.c

/* signal.c

这是信号处理实现中,我们使用的是后端
  没有更好的办法来做到的信号处理。它使用的sigaction()
  或信号()来设置一个信号处理程序,一个插口对来告诉
  事件时基

This is the signal-handling implementation we use for backends that don't have a better way to do signal handling. It uses sigaction() or signal() to set a signal handler, and a socket pair to tell the event base when

请注意,我说的活动基地:只有一个事件底座可以设置
  到同时使用这一点。由于历史原因和落后
  兼容性,如果添加一个事件一个信号event_base A,
  然后添加一个事件的信号(任何信号!),以event_base B,
  event_base B就得到有关的信号,但event_base一
  不会的。

Note that I said "the event base" : only one event base can be set up to use this at a time. For historical reasons and backward compatibility, if you add an event for a signal to event_base A, then add an event for a signal (any signal!) to event_base B, event_base B will get informed about the signal, but event_base A won't.

这将是整齐的改变将来的版本行为
  LIBEVENT。 kqueue的已经做远一些更明智。 我们可以
  在Linux上所有的后端使用做一个signalfd合理的事情。

  * /

It would be neat to change this behavior in some future version of Libevent. kqueue already does something far more sensible. We can make all backends on Linux do a reasonable thing using signalfd. */

所以现在的libevent使用的sigaction()(如果可用),并且,做不到这一点,信号()。

so right now libevent uses sigaction() if it's available, and, failing that, signal().

如果使用signalfd()你通常使用sigprocmask阻断信号,使得信号不会导致默认处理程序来执行。那么,您可以使用的libevent监控返回的文件句柄和处理来自正常同步code安全信号,而无需担心内存安全或阻断或中断其他系统调用。

if you use signalfd() you usually block the signal using sigprocmask so that the signals won't cause the default handlers to execute. then, you can monitor the returned file handle using libevent and handle the signal from normal synchronous code safely, without having to worry about memory safety or blocking or interrupting other syscalls.

有什么你可以放心地经典异步内部信号处理程序做的限制(即那些使用的sigaction注册)。见人信号异步信号安全功能。使用signalfd方法,这些限制都大大减少了。

there are restrictions on what you can safely do inside classic asynchronous signal handlers (i.e. those registered using sigaction). See the "Async-signal-safe functions" in man signal. Using the signalfd approach, these restrictions are greatly lessened.

有关注册计时器回调的警告很可能把那里作为一个问题,因为libevent的支持这么多平台,并至少有一人被报告的问题。注意,如果你选择/投票通过signalfd注册的文件句柄,那么你就不必担心这个限制反正。

the warning about registering a timer callback was likely put there as a concern because libevent supports so many platforms, and at least one person reported problems. note that if you are selecting/polling a file handle registered by signalfd then you don't have to worry about this restriction anyways.

编辑:我重读你的问题,并意识到我真的没有回答正确。登记signalfd然后使用在的libevent的好处是,因为它是在便携的费用要快。有计划(http://archives.seul.org/libevent/users/Mar-2010/msg00046.html),以包括signalfd到libevent的,但据我所知还没有发生过。

i re-read your question and realized I hadn't really answered it properly. the benefit of registering a signalfd and then using that in libevent is because it's faster at the expense of portability. there were plans (http://archives.seul.org/libevent/users/Mar-2010/msg00046.html) to include signalfd into libevent but AFAIK it hasn't happened yet.

此外,一旦你已经通知了SIGCHLD的你应该总是调用waitpid函数在一个循环,直到你得到ENOCHLD因为这两种方法将崩溃的信号多次出现。

also, once you've been notified of the SIGCHLD you should always call waitpid in a loop until you get ENOCHLD because both methods will collapse multiple occurences of the signal.

这篇关于libevent的&安培;信号处理(特别是SIGCHLD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆