Sometimes an example is worth more than a thousand words: This code in c++ exposes a very simple signal handler.
void gracefullShutdown(int sigNum) {
// cleanup or do wathever you need to do in case of received signal(s)
// Terminate this executable
exit(sigNum);
}
int main(int argc, char* argv[]) {
signal(SIGINT , gracefullShutdown);
signal(SIGTERM , gracefullShutdown);
std::cout << "starting..." << std::endl;
while(1);
exit(EXIT_SUCCESS);
}
When you press the Ctrl+C during execution of this program, the linux kernel will send a SIGINT signal to this program, normally it would terminate, but in this precise case it leaves you the possibility to do something before terminating.