Second signal call in sighandler - what for?

后端 未结 3 449
温柔的废话
温柔的废话 2021-01-22 02:47

Recently I found some code which uses signal:

  286 static void sighandler( int signum )
  287 {   
  288     alarmed = 1;
  289     signal( signum,         


        
相关标签:
3条回答
  • 2021-01-22 02:59

    There are systems where, if signal() is called with a function, the signal handler is reset to default after the first signal is caught (I haven't found out on the fly if it's defined to be so). That's the reason why signal() is called again in the signal handler.

    0 讨论(0)
  • 2021-01-22 03:05

    Handling Signals

    The call to signal establishes signal handling for only one occurrence of a signal. Before the signal-handling function is called, the library resets the signal so that the default action is performed if the same signal occurs again. Resetting signal handling helps to prevent an infinite loop if, for example, an action performed in the signal handler raises the same signal again. If you want your handler to be used for a signal each time it occurs, you must call signal within the handler to reinstate it. You should be cautious in reinstating signal handling. For example, if you continually reinstate SIGINT handling, you may lose the ability to interrupt and terminate your program.

    The signal() function defines the handling of the next received signal only, after which the default handling is reinstated. So it is necessary for the signal handler to call signal() if the program needs to continue handling signals using a non-default handler.

    0 讨论(0)
  • 2021-01-22 03:20

    (1) Calling signal two or more times is not unusual. It's just setting up two handlers for 2 different signals.

    (2) Older unix systems used to reset a signals disposition to default after a handler was invoked. This code is reestablishing the handler.

    The GNU man (2) signal page has a couple of paragraphs devoted to this in the "portability" section. Actually one of several reasons to use sigaction.

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