FRI-OS
example/posix/processes-sigint.c

Shows how to install a custom signal handler

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
// A function that gets called if it is
// installed as a handler for a signal and
// that signal is recieved by the process
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");
// install our handler for the SIGINT signal
// and remember the previous handler (the default)
// SIGINT is sent to the process running on a terminal
// when Ctrl+C is pressed
sighandler_t old_handler = signal(SIGINT, handle_SIGINT);
// wait 10 seconds
// with the handler installed pressing Ctrl+C will cause
// the message to be printed but the process will not exit
for(int i=0; i!=10; ++i)
{
printf("%d\n", i);
sleep(1);
}
printf("Restoring the default handler\n");
// install the previous signal handler
signal(SIGINT, old_handler);
// wait again
// with the default handler when Ctrl+C is pressed
// the process will exit
for(int i=0; i!=10; ++i)
{
printf("%d\n", i);
sleep(1);
}
return 0;
}