Sending information with a Signal in Linux

我们两清 提交于 2020-01-01 05:41:05

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/7269709/sending-information-with-a-signal-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!