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