Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending information with a Signal in Linux

Tags:

c

linux

signals

When sending a signal from one process to another, I also want to send a value of type long. Is that possible? I am using SIGUSR1.

like image 583
MetallicPriest Avatar asked Sep 01 '11 11:09

MetallicPriest


1 Answers

Sure you can, but you'll have to send it with sigqueue(2) instead of kill(2). And you can send an int or a sival_ptr.

union sigval {
    int   sival_int;
    void *sival_ptr;
};

Establish the handler

struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
sa.sa_flags = SA_SIGINFO; /* Important. */

sigaction(SIGUSR1, &sa, NULL);

The handler for a signal established using SA_SIGINFO

static void
handler(int sig, siginfo_t *si, void *ucontext)
{
    si->si_value; /* This is what you're looking for. */
}

Sending an integer

union sigval sv;
sv.sival_int = 42;

sigqueue(pid, SIGUSR1, sv);
like image 146
cnicutar Avatar answered Sep 19 '22 01:09

cnicutar