简体   繁体   English

sighandler中的第二个信号呼叫-做什么用?

[英]Second signal call in sighandler - what for?

Recently I found some code which uses signal : 最近我发现一些使用signal代码:

  286 static void sighandler( int signum )
  287 {   
  288     alarmed = 1;
  289     signal( signum, sighandler );
  290 }
  291 
  292 void set_alarm( int seconds )
  293 {
  294     alarmed = 0;
  295     signal( SIGALRM, sighandler );
  296     alarm( seconds );
  297 }

I have some troubles to figure out why do I need to call signal 2 times, especially, why do I need to call signal in sighandler ? 我很难弄清楚为什么我需要两次调用signal ,特别是为什么我需要在sighandler调用signal I know what the above code does but dont understand why do I need to call signal 2 times. 我知道上面的代码做什么,但不明白为什么我需要两次调用signal

(1) Calling signal two or more times is not unusual. (1)两次或更多次呼叫signal并不罕见。 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. (2)较旧的unix系统,用于在调用处理程序后将信号处置重置为默认值。 This code is reestablishing the handler. 这段代码正在重新建立处理程序。

The GNU man (2) signal page has a couple of paragraphs devoted to this in the "portability" section. GNU man (2) signal在“可移植性”部分有专门针对此问题的几段。 Actually one of several reasons to use sigaction . 实际上是使用sigaction的几个原因之一。

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. 如果希望您的处理程序每​​次出现时都用于信号,则必须在处理程序中调用signal来恢复它。 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. 例如,如果您继续恢复SIGINT处理,则可能会失去中断和终止程序的能力。

The signal() function defines the handling of the next received signal only, after which the default handling is reinstated . signal()函数仅定义对下一个接收到的信号的处理, 之后将恢复默认处理 So it is necessary for the signal handler to call signal() if the program needs to continue handling signals using a non-default handler. 因此,如果程序需要使用非默认处理程序继续处理信号,则信号处理程序有必要调用signal()

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). 在某些系统中,如果通过函数调用signal() ,则在捕获到第一个信号后,信号处理程序将重置为默认值(如果这样定义,我还没有即时发现)。 That's the reason why signal() is called again in the signal handler. 这就是为什么在信号处理程序中再次调用signal()的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM