1---
 2title: "Sending signals to C programs"
 3url: sending-signals-to-c-program.html
 4date: 2024-06-17T16:13:13+02:00
 5type: note
 6draft: false
 7tags: [c]
 8---
 9
10For simple and easy IPC to the C program we can use signals.
11https://www.man7.org/linux/man-pages/man7/signal.7.html
12
13Only two user defined signals are available (`SIGUSR1` and `SIGUSR2`) but you
14can hijack other ones even though this is really not advisable.
15
16```c
17// signal.c
18#include <stdio.h>
19#include <stdlib.h>
20#include <signal.h>
21#include <unistd.h>
22
23void handle_signal(int signal) {
24    printf("Signal received: %d\n", signal);
25}
26
27int main() {
28    signal(SIGUSR1, handle_signal); // is equal to 10
29    signal(SIGUSR2, handle_signal); // is equal to 12
30
31    while(1) {
32        sleep(1);
33    }
34
35    return 0;
36}
37```
38
39Compile with `gcc -o signal signal.c` and run the program with `./signal` and
40then from another terminal send the signal to the program with `kill -10
41$(pidof signal)` which should print out `Signal received: 10`.
42
43| Signal      | x86/ARM | Alpha/ | MIPS   | PARISC | Notes         |
44|-------------|---------|--------|--------|--------|---------------|
45| SIGHUP      | 1       | 1      | 1      | 1      |               |
46| SIGINT      | 2       | 2      | 2      | 2      |               |
47| SIGQUIT     | 3       | 3      | 3      | 3      |               |
48| SIGILL      | 4       | 4      | 4      | 4      |               |
49| SIGTRAP     | 5       | 5      | 5      | 5      |               |
50| SIGABRT     | 6       | 6      | 6      | 6      |               |
51| SIGIOT      | 6       | 6      | 6      | 6      |               |
52| SIGBUS      | 7       | 10     | 10     | 10     |               |
53| SIGEMT      | -       | 7      | 7      | -      |               |
54| SIGFPE      | 8       | 8      | 8      | 8      |               |
55| SIGKILL     | 9       | 9      | 9      | 9      |               |
56| **SIGUSR1** | **10**  | **30** | **16** | **16** |               |
57| SIGSEGV     | 11      | 11     | 11     | 11     |               |
58| **SIGUSR2** | **12**  | **31** | **17** | **17** |               |
59| SIGPIPE     | 13      | 13     | 13     | 13     |               |
60| SIGALRM     | 14      | 14     | 14     | 14     |               |
61| SIGTERM     | 15      | 15     | 15     | 15     |               |
62| SIGSTKFLT   | 16      | -      | -      | 7      |               |
63| SIGCHLD     | 17      | 20     | 18     | 18     |               |
64| SIGCLD      | -       | -      | 18     | -      |               |
65| SIGCONT     | 18      | 19     | 25     | 26     |               |
66| SIGSTOP     | 19      | 17     | 23     | 24     |               |
67| SIGTSTP     | 20      | 18     | 24     | 25     |               |
68| SIGTTIN     | 21      | 21     | 26     | 27     |               |
69| SIGTTOU     | 22      | 22     | 27     | 28     |               |
70| SIGURG      | 23      | 16     | 21     | 29     |               |
71| SIGXCPU     | 24      | 24     | 30     | 12     |               |
72| SIGXFSZ     | 25      | 25     | 31     | 30     |               |
73| SIGVTALRM   | 26      | 26     | 28     | 20     |               |
74| SIGPROF     | 27      | 27     | 29     | 21     |               |
75| SIGWINCH    | 28      | 28     | 20     | 23     |               |
76| SIGIO       | 29      | 23     | 22     | 22     |               |
77| SIGPOLL     |         |        |        |        | Same as SIGIO |
78| SIGPWR      | 30      | 29/-   | 19     | 19     |               |
79| SIGINFO     | -       | 29/-   | -      | -      |               |
80| SIGLOST     | -       | -/29   | -      | -      |               |
81| SIGSYS      | 31      | 12     | 12     | 31     |               |
82| SIGUNUSED   | 31      | -      | -      | 31     |               |
83