问题描述
在 sigaction
联机帮助页中是这样写的:
In sigaction
manpage it's written :
sa_sigaction
还指定要与 signum
关联的操作.这个函数接收信号编号作为它的第一个参数,一个指向 siginfo_t
作为第二个参数的指针和指向 ucon 的指针text_t(转换为 void*
)作为它的第三个参数.
sa_sigaction
also specifies the action to be associated with signum
.This function receives the signal number as its first argument, apointer to a siginfo_t
as its second argument and a pointer to a ucon-text_t (cast to void*
) as its third argument.
所以我们可以将参数传递给信号处理程序(通过 void*
),但是我找不到路.有没有办法把它放在任何地方?
So we can pass arguments to the signal handler (throught void*
), but Ican't find the way.Is there no way to put it anywhere?
示例:
struct ping_val
{
int data1;
int data2;
};
void ping(int sig, siginfo_t *siginf, void *ptr)
{
// ....
}
int main()
{
struct sigaction sa_ping;
ping_val foo;
foo.data1 = 3;
foo.data2 = 4;
sa_ping.sa_sigaction = ping;
sigemptyset(&sa_ping.sa_mask);
sa_ping.sa_flags = 0;
sigaction(SIGALRM, &sa_ping, NULL);
// ....
}
我在哪里可以在 ping 的参数中传递 foo
结构值(在 struct *
中进行强制转换)???
Where I can pass foo
structure value in argument in ping (with a cast in struct *
)???
推荐答案
你写的:
所以我们可以[通过 void* ucontext_t 参数] 将参数传递给信号处理程序,但我找不到方法.
找不到这样的方法 — ucontext_t
参数由系统本身设置并引用 "发送信号时中断的接收线程的上下文."
There is no such way to be found — the ucontext_t
argument is set by the system itself and refers to "the receiving thread's context that was interrupted when the signal was delivered."
(具体来说,根据规范至少是对中断上下文的引用,以及对该上下文的特定于机器的表示,以及当前上下文的堆栈及其阻塞信号集.)
(Specifically, by spec it contains at least a reference to that interrupted context, and to a machine-specific representation of that context, and also the current context's stack and its set of blocked signals.)
您可能不需要任何这些.如果您希望您的程序在收到信号时采取一些行动和/或修改其状态,那么您可以在信号处理程序中安全使用的策略数量有限(volatile sigatomic_t
标志,自管道,特定于平台的接口),但是您的主循环当然可以根据刚刚收到的 SIGFOO 的知识做您想做的任何事情.
You probably don't need any of this. If you want your program to take some action and/or modify its state on receipt of a signal, you've a limited number of tactics that can be safely used in a signal handler (volatile sigatomic_t
flags, self-pipes, platform-specific interfaces), but then your main loop can of course do whatever you want with the knowledge that SIGFOO was just received.
这篇关于sigaction : 使用“void (*sa_sigaction)(int, siginfo_t *, void *);"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!