I use signal(SIGINT,my_handler)
to point SIGINT
to my_handler
. After some time I want to reset it to whatever default handler it points to in general. How can I do that?
Signal dispositions Each signal has a current disposition, which determines how the process behaves when it is delivered the signal. Default action is to terminate the process.
The signal is ignored. The signal is queued, and will be processed as soon as the current handler returns.
In general, yes, execution continues after the handler returns.
sigaction() can be called with a NULL second argument to query the current signal handler. It can also be used to check whether a given signal is valid for the current machine by calling it with NULL second and third arguments.
Pass SIG_DFL
as the func
parameter to signal()
to reset default behaviour:
signal(SIGINT, SIG_DFL);
Today, the usage of sigaction is recommended.
Moreover, it allows you to reset automatically the signal handler to default one before your custom handler is called the first time.
SA_RESETHAND
If set, the disposition of the signal shall be reset to
SIG_DFL
and theSA_SIGINFO
flag shall be cleared on entry to the signal handler.Note:
SIGILL
andSIGTRAP
cannot be automatically reset when delivered; the system silently enforces this restriction.Otherwise, the disposition of the signal shall not be modified on entry to the signal handler.
In addition, if this flag is set,
sigaction()
may behave as if theSA_NODEFER
flag were also set.
#include <signal.h>
#include <stdio.h>
action.sa_handler = my_handler;
action.sa_flags = SA_RESETHAND;
if (sigaction(SIGINT, &action, NULL) == -1)
{
perror("Failed to install signal handler for SIGINT");
}
Refer to this post to see how to reset a signal handler to the default one if it's not a one-time handler using sigaction: https://stackoverflow.com/a/24804019/7044965
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