Sending signals to C programs

note, Jun 17, 2024, on Mitja Felicijan's blog

For simple and easy IPC to the C program we can use signals. https://www.man7.org/linux/man-pages/man7/signal.7.html

Only two user defined signals are available (SIGUSR1 and SIGUSR2) but you can hijack other ones even though this is really not advisable.

// signal.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void handle_signal(int signal) {
    printf("Signal received: %d\n", signal);
}

int main() {
    signal(SIGUSR1, handle_signal); // is equal to 10
    signal(SIGUSR2, handle_signal); // is equal to 12

    while(1) {
        sleep(1);
    }

    return 0;
}

Compile with gcc -o signal signal.c and run the program with ./signal and then from another terminal send the signal to the program with kill -10 $(pidof signal) which should print out Signal received: 10.

Signalx86/ARMAlpha/MIPSPARISCNotes
SIGHUP1111
SIGINT2222
SIGQUIT3333
SIGILL4444
SIGTRAP5555
SIGABRT6666
SIGIOT6666
SIGBUS7101010
SIGEMT-77-
SIGFPE8888
SIGKILL9999
SIGUSR110301616
SIGSEGV11111111
SIGUSR212311717
SIGPIPE13131313
SIGALRM14141414
SIGTERM15151515
SIGSTKFLT16--7
SIGCHLD17201818
SIGCLD--18-
SIGCONT18192526
SIGSTOP19172324
SIGTSTP20182425
SIGTTIN21212627
SIGTTOU22222728
SIGURG23162129
SIGXCPU24243012
SIGXFSZ25253130
SIGVTALRM26262820
SIGPROF27272921
SIGWINCH28282023
SIGIO29232222
SIGPOLLSame as SIGIO
SIGPWR3029/-1919
SIGINFO-29/---
SIGLOST--/29--
SIGSYS31121231
SIGUNUSED31--31


Other notes