I use:
signal(SIGINT, CtrlHandler);
To add handler of SIGINT
event. But how can i delete this handler?
While in a command line such as MS-DOS, Linux, and Unix, Ctrl + C is used to send a SIGINT signal, which cancels or terminates the currently-running program. For example, if a script or program is frozen or stuck in an infinite loop, pressing Ctrl + C cancels that command and returns you to the command line.
Ctrl-C. Pressing this key causes the system to send an INT signal ( SIGINT ) to the running process. By default, this signal causes the process to immediately terminate.
signal() sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL, or the address of a programmer- defined function (a "signal handler"). If the signal signum is delivered to the process, then one of the following happens: * If the disposition is set to SIG_IGN, then the signal is ignored.
If you want to ignore the signal specified by the first argument (i.e., pretending that the signal never happens), use SIG_IGN for the second argument. In the above two lines, SIGINT and SIGALRM are ignored. If you want the system to use the default way to handle a signal, use SIG_DFL for the second argument.
Here is what you do:
signal(SIGINT, SIG_DFL);
That resets the signal handler back to whatever the default behavior was for that signal (including the default disposition if it hasn't been set). In the case of SIGINT, it's aborting your process without a core dump.
The manual for signal
explains why this works:
signal(signum, handler)
sets the disposition of the signalsignum
tohandler
, which is eitherSIG_IGN
,SIG_DFL
, or the address of a programmer-defined function (a "signal handler"). ... If the disposition is set toSIG_DFL
, then the default action associated with the signal occurs.
You can also find this information using the man
command. If you type man signal
on the command line and read through, you should see it.
This is very specific to the case in which you've replaced the system default signal handler. In some situations, what you want is to simply restore whatever handler was there in the first place. If you look at the definition of signal
it looks like this:
sighandler_t signal(int signum, sighandler_t handler);
So, it returns a sighandler_t
. The sighandler_t
that it returns represents the previous 'disposition' of the signal. So, another way to handle this is to simply save the value it returns and then restore that value when you want to remove your own handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With