Signal queuing in C

前端 未结 4 1900
南方客
南方客 2020-12-03 00:07

I have a simple program under Linux which sends SIGUSR1 signal to its child process in a cycle. But when I send e.g. 10 signals, sometimes happens, that the child received o

相关标签:
4条回答
  • 2020-12-03 00:18

    What happens is the following:

    1. First signal received, namely SIGUSR1, handler is called and is running
    2. Second signal received, since handler from nr1 is still running, the signal nr2 gets pending and blocked.
    3. Third signal received, since handler from nr1 is still running, the signal 3 gets discarded.
    4. Fourth, fifth...etc signal of the same type as the signal nr1 are discarded.

    Once signal handler is done with signal nr1, it will process signal nr2, and then signal handler will process the SIGUSR2.

    Basically, pending signals of the same type are not queued, but discarded. And no, there is no easy way to "burst" send signals that way. One always assumes that there can be several signals that are discarded, and tries to let the handler do the work of cleaning and finding out what to do (such as reaping children, if all children die at the same time).

    0 讨论(0)
  • 2020-12-03 00:19

    If multiple signals of the same type are sent and not handled, they aren't queued. Say the program masks SIGUSR1, calls kill(getpid(), SIGUSR1) 10 times and unmasks SIGUSR1. It will receive SIGUSR1 just once.

    0 讨论(0)
  • 2020-12-03 00:34

    Your problem is probably that SIGUSR2 is a signal that is delivered right away, while other signals are blocked or queued (in status pending).

    Here's how you can check for pending signals: http://www.gnu.org/s/libc/manual/html_node/Checking-for-Pending-Signals.html

    0 讨论(0)
  • 2020-12-03 00:37

    So doing simultaneous I/O of many files with SIGIO seems possible only if one uses flag SA_NODEFER for struct sigaction sa_flags field and never blocks signals.

    So then, one could get interrupt from inside a signal handler and create new thread for each individual signal being handled. That gets complicated :) So no wonder why no one seems to use SIGIO.

    0 讨论(0)
提交回复
热议问题