在sighandler第二信号呼叫 - 什么? [英] Second signal call in sighandler - what for?

查看:82
本文介绍了在sighandler第二信号呼叫 - 什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我发现了一些code,它使用信号

Recently I found some code which uses 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 }

我有一些麻烦搞清楚为什么我需要调用信号 2倍,尤其是,为什么我需要调用信号 sighandler ?我知道上面code做什么,但不明白为什么我需要调用信号 2倍。

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? I know what the above code does but dont understand why do I need to call signal 2 times.

推荐答案

处理信号

到信号的呼叫建立信号处理只有一个的信号的发生。信号处理函数被调用之前,的库中,以便进行默认操作,如果再次发生相同的信号复位信号。复位信号处理有助于prevent无限循环,如果,例如,在信号处理程序中执行的动作再次将相同的信号。如果你想被用于每次发生时的信号处理程序,您必须在处理程序中调用信号恢复它。你应该在复原的信号处理谨慎。例如,如果你不断地恢复SIGINT处理,你可能会失去中断和终止程序的能力。

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.

这篇关于在sighandler第二信号呼叫 - 什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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