Cogs and Levers A blog full of technical stuff

Unix IPC: Working with Signals

Incoming messages to your process can come in the form of a signal. Signals are standard message packets that the operating system will use to tell your process something about the environment.

This snippet will show you show to setup a signal handler in your program.

/** The signal handler */
void sigint_handler(int sig) {
	/* do something interesting with the sigint here */
}

int main(void) {
	struct sigaction sa;

	/* fill out the signal structure */
	sa.sa_handler = sigint_handler;
	sa.sa_flags   = 0;
	sigemptyset(&sa.sa_mask);

	/* assign it to the appropriate signal */
	sigaction(SIGINT, &sa, NULL);

	/* do other stuff here, sigint will be handled */
}

Further reading