Skip to content

Instantly share code, notes, and snippets.

@nfarring
Created August 24, 2011 21:52
Show Gist options
  • Save nfarring/1169363 to your computer and use it in GitHub Desktop.
Save nfarring/1169363 to your computer and use it in GitHub Desktop.
Register a signal handler for SIGINT.
#include <stdlib.h>
#include <signal.h>
/*
* Gracefully exits when Ctrl+C is pressed.
*/
void sigint_handler(int signum) {
exit(0);
}
struct sigaction act;
memset((void *)&act, 0, sizeof(act));
act.sa_handler = sigint_handler;
if (sigaction(SIGINT, &act, NULL) == -1) {
perror("sigaction");
exit(-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment