【发布时间】:2011-04-12 11:32:02
【问题描述】:
如何从内核空间获取信号到用户空间?
【问题讨论】:
标签: linux linux-device-driver embedded-linux
如何从内核空间获取信号到用户空间?
【问题讨论】:
标签: linux linux-device-driver embedded-linux
要从内核获取信号到用户空间,请在您的用户空间和内核空间代码中使用以下代码,如下所示:
用户空间应用:
signal(SIGIO, &signal_handler_func);
fcntl(fd, F_SETOWN, getpid());
oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, oflags | FASYNC);
定义signal_handler_func函数:
void signal_handler_func (int sig)
{
//handle the action corresponding to the signal here
}
内核空间模块:
int ret = 0;
struct siginfo info;
memset(&info, 0, sizeof(struct siginfo));
info.si_signo = SIG_TEST;
info.si_code = SI_QUEUE;
info.si_int = 1234;
send_sig_info(SIG_TEST, &info, t);//send signal to user land
t 是用户应用程序的 PID。
【讨论】:
kill_proc_info() 而不是send_sig_info()。您可以使用for_each_process() 宏通过名称查找进程并获取它的PID。
使用内核API函数kill_proc_info(int sig, struct siginfo *info, pid_t pid)
注意 这实际上是一个糟糕的答案。这些函数确实向用户空间发送信号,但正确的方法是这样做,因为询问者的意图是使用此处记录的 fasync 字符设备方法:http://www.xml.com/ldd/chapter/book/ch05.html#t4
【讨论】:
有一种叫做 NetLink 接口的东西,它为内核进程和用户进程之间的通信提供了一组 API。它类似于套接字接口,通信是异步的,因此优于 IOCTL。
【讨论】: