How to reset SIGINT to default after pointing it some user-defined handler for some time?

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 the SA_SIGINFO flag shall be cleared on entry to the signal handler.

Note: SIGILL and SIGTRAP 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 the SA_NODEFER flag were also set.

Defining a one time signal handler

#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

Tags:

C++

Signals