简体   繁体   English

什么是信号函数 (SIGINT)?

[英]What is the Signal function (SIGINT)?

What does this statement below do?下面的这个语句有什么作用? If anyone can explain this function I would really appreciate it.如果有人能解释这个功能,我将不胜感激。

signal(SIGINT, SIG_DFL);

SIGINT is the interrupt signal (ctrl+C). SIGINT 是中断信号(ctrl+C)。 Its default behaviour is to terminate the process.它的默认行为是终止进程。 SIGINT signal can be dis-positioned, means one can change the default behaviour ( By calling sighandler, or setting it SIG_IGN ) Now once the action is changes and you want to set it again the default behaviour of this signal then you should write SIGINT 信号可以被处置,这意味着可以改变默认行为(通过调用 sighandler,或设置它 SIG_IGN )现在一旦动作发生变化并且你想再次将它设置为这个信号的默认行为,那么你应该写

signal(SIGINT, SIG_DFL);信号(SIGINT,SIG_DFL);

It will again change the default behaviour of the signal.它将再次更改信号的默认行为。 ( which is to terminate a process ) (即终止进程)

Set the handling of the SIGINT signal to its default.将 SIGINT 信号的处理设置为其默认值。

If you are on a *nix system, try man signal to get answers like this.如果您使用的是 *nix 系统,请尝试使用man signal来获得这样的答案。 That (and maybe checking some of the pages listed under "See Also") will also tell you what signals are.那(也许检查“另请参阅”下列出的一些页面)也会告诉您什么是信号。

As for what the defaults are - it's going to be one of "ignore it", "terminate the program", or "cause the program to dump core".至于默认值是什么——它将是“忽略它”、“终止程序”或“导致程序转储核心”之一。 Which it is depends on the specific signal, and I don't remember the default for SIGINT, sorry.它取决于特定信号,我不记得 SIGINT 的默认值,抱歉。

The line you wrote changes the signal handler of the interrupt signal back to the default您写的行将中断信号的信号处理程序更改回默认值

void myInterruptHandler (int signum) { 
    printf("You pressed ctrl+c, but I don't care\n");
 }

int main(){
  sighandler_t oldHandler = signal(SIGINT, myInterruptHandler);
  while(true){
    printf("Ctrl + C can't kill me!!\n");
    sleep(1000);
  }
  //Change back to the old handler
  signal(SIGINT, oldHandler);
  //alternatively:  signal(SIGINT, SIG_DFL);
}

It sets the default action for SIGINT as described in man page below;它为 SIGINT 设置默认操作,如下面的手册页所述;

From Linux signal man page;来自 Linux 信号手册页;

sighandler_t signal(int signum, sighandler_t handler);

signal() function sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL or the address of a programmer-defined function. signal() 函数将信号 signum 的处置设置为 handler,它可以是 SIG_IGN、SIG_DFL 或程序员定义的函数的地址。

  • If the disposition is set to SIG_IGN, then the signal is ignored.如果处置设置为 SIG_IGN,则忽略该信号。
  • If the disposition is set to SIG_DFL, then the default action associated with the signal occurs.如果处置设置为 SIG_DFL,则发生与信号关联的默认操作。

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

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