You have to be careful about what you’re doing in a signal handler.
Most library functions are considered signal-unsafe, which means that
you shouldn’t call them from a signal handler unless you can guarantee
that the signal did not interrupt a signal-unsafe function. In
particular, if the Ctrl-C interrupts a printf() call, calling printf()
again in the signal handler has a reasonable chance of corrupting some
data structures, possibly causing a crash.
Raphaël HUCK wrote:
Yes you can. First, define your handler and a sigaction structure :
void handler(int signal)
case SIGINT : printf(“SIGINT received\n”); do_what_you_want(); break;
default : printf(“Other signal received\n”);
struct sigaction action;
And then specify action to be associated with SIGINT :
action.sa_handler = handler;
sigaction(SIGINT, &action, NULL);
Hope that helps !
is there a possibility to prevent a process from termination when a user
presses CTRL-C? Or is it possible to catch this user abortion request and
program my own abortion-handler which is invoked by pressing CTRL-C,
the process actually aborts?