What is the Signal function (SIGINT)?
Asked Answered
M

4

21

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

signal(SIGINT, SIG_DFL);
Machos answered 30/4, 2013 at 7:7 Comment(1)
Did you read the man page?Quentin
W
13

SIGINT is the interrupt signal and is raised when you press Ctrl+C. Its default behavior is to terminate the process. The SIGINT signal can be dis-positioned, which means one can change the default behavior (by calling sighandler, or setting it SIG_IGN).

Now once the action is changed and you want to set it again the default behavior of this signal then you should write

signal(SIGINT, SIG_DFL);

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

Wartburg answered 26/3, 2017 at 10:41 Comment(0)
E
5

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.

Equivalence answered 30/4, 2013 at 7:14 Comment(0)
F
5

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);
}
Fingertip answered 30/4, 2013 at 7:29 Comment(2)
Will the above program ever let control flow to the last line? Looks like it will be trapped in the loop.Autopilot
When the custom handler returns, will it then invoke the default handler, or not?Autopilot
D
2

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.
Dejection answered 30/4, 2013 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.