Shows how to install a custom signal handler
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
void handle_SIGINT(int)
{
printf("Received the INT signal\n");
}
int main(int argc, const char* argv[])
{
printf("Installing handler for the INT signal\n");
sighandler_t old_handler = signal(SIGINT, handle_SIGINT);
for(int i=0; i!=10; ++i)
{
printf("%d\n", i);
sleep(1);
}
printf("Restoring the default handler\n");
signal(SIGINT, old_handler);
for(int i=0; i!=10; ++i)
{
printf("%d\n", i);
sleep(1);
}
return 0;
}