Sending information with a Signal in Linux
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开发者_高级运维.
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);
精彩评论