18

I need to send a signal to a process and when the process receives this signal it does some things, how is this best achieved in C?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Samantha Catania
  • 5,116
  • 5
  • 39
  • 69

3 Answers3

27

The way to send a signal to a process is kill(pid, signal); However, you should be aware that signals are not a robust means of inter-process communication except for parent-to-direct-child messages due to inherent race conditions. Pipes, files, directories, named semaphores, sockets, shared memory, etc. all provide greatly superior approaches to inter-process communication.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
4

If you happen to be on one of the Unix variants, the following man pages will help:

man 2 kill
man 2 signal
man 2 sigvec
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
wildplasser
  • 43,142
  • 8
  • 66
  • 109
0

kill + fork runnable POSIX example

Time for some fun:

#define _XOPEN_SOURCE 700
#include <assert.h>
#include <signal.h>
#include <stdbool.h> /* false */
#include <stdio.h> /* perror */
#include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE */
#include <sys/wait.h> /* wait, sleep */
#include <unistd.h> /* fork, write */

void signal_handler(int sig) {
    char s1[] = "SIGUSR1\n";
    char s2[] = "SIGUSR2\n";
    if (sig == SIGUSR1) {
        write(STDOUT_FILENO, s1, sizeof(s1));
    } else if (sig == SIGUSR2) {
        write(STDOUT_FILENO, s2, sizeof(s2));
    }
    signal(sig, signal_handler);
}

int main() {
    pid_t pid;

    signal(SIGUSR1, signal_handler);
    signal(SIGUSR2, signal_handler);
    pid = fork();
    if (pid == -1) {
        perror("fork");
        assert(false);
    } else {
        if (pid == 0) {
            while (1);
            exit(EXIT_SUCCESS);
        }
        while (1) {
            kill(pid, SIGUSR1);
            sleep(1);
            kill(pid, SIGUSR2);
            sleep(1);
        }
    }
    return EXIT_SUCCESS;
}

Compile and run:

gcc -std=c99 signal_fork.c
./a.out

Outcome:

SIGUSR1
SIGUSR2
SIGUSR1
SIGUSR2
....

But beware that there are many complexities when dealing with signals:

Tested in Ubuntu 17.10, GitHub upstream.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985