在Linux中通过信号发送信息 [英] Sending information with a Signal in Linux
本文介绍了在Linux中通过信号发送信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当从一个进程向另一个进程发送信号时,我还想发送一个 long 类型的值.那可能吗?我正在使用SIGUSR1.
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;
};
建立处理程序
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
sa.sa_flags = SA_SIGINFO; /* Important. */
sigaction(SIGUSR1, &sa, NULL);
使用SA_SIGINFO建立的信号处理程序
static void
handler(int sig, siginfo_t *si, void *ucontext)
{
si->si_value; /* This is what you're looking for. */
}
发送整数
union sigval sv;
sv.sival_int = 42;
sigqueue(pid, SIGUSR1, sv);
这篇关于在Linux中通过信号发送信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文