What is the Signal function (SIGINT)?

2019-07-05 21:06发布

What does this statement below do? If anyone can explain this function I would really appreciate it.

signal(SIGINT, SIG_DFL);

标签: c signals sigint
4条回答
Viruses.
2楼-- · 2019-07-05 21:20

It sets the default action for SIGINT as described in man page below;

From Linux signal man page;

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.

  • If the disposition is set to SIG_IGN, then the signal is ignored.
  • If the disposition is set to SIG_DFL, then the default action associated with the signal occurs.
查看更多
狗以群分
3楼-- · 2019-07-05 21:21

SIGINT is the interrupt signal (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

signal(SIGINT, SIG_DFL);

It will again change the default behaviour of the signal. ( which is to terminate a process )

查看更多
Deceive 欺骗
4楼-- · 2019-07-05 21:31

Set the handling of the SIGINT signal to its default.

If you are on a *nix system, try man signal to get answers like this. 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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-07-05 21:47

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);
}
查看更多
登录 后发表回答