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

DateTitle
Set color temperature of displays on i3
Male and female body proportion reference images3d
Extract lines from a file with sed
Change permissions of matching files recursively
Fix screen tearing on Debian 12 Xorg and i3
10/GUI 10 Finger Multitouch User Interface
Making cgit look nicer
Personal sane Vim defaultsvim
Cronjobs on Github with Github Actions
Development environments with Nix
Execute not blocking async shell command in C#
Calling assembly functions from Cc
Push to multiple origins at once in Git
Download list of YouTube files
Aerial photography of algae spotted on river Sava