Capturing signals in C

You’ll need to use the signal.h library.

Here’s a working example in which I capture SIGINT and print a message to STDOUT:

#include<stdio.h>
#include<signal.h>

void sig_handler(int signo)
{
    if (signo == SIGINT)
        write(0, "Hello\n", 6);
}

int main(void)
{
    signal(SIGINT, sig_handler);
    // Just to testing purposes
    while(1)
        sleep(1);
    return 0;
}

Browse More Popular Posts

Leave a Comment