- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试在文件描述符上可以进行 I/O 时接收信号。该程序在不执行 I/O 时需要执行其他操作,因此不能使用 select(2)。
当我运行下面的示例代码时,它会尽可能快地从处理程序内部打印消息,即使标准输入上没有数据。更奇怪的是,siginfo_t 结构中报告的文件描述符因运行而异。我只为 stdin (fd 0) 设置它;为什么处理程序会报告任何其他值?有时我看到 0,有时我看到 1,大多数时候我看到 '?',它表示 0、1 或 2 以外的值。
这是在 OpenSUSE 12.3 和 Linux 内核 3.7.10-1.16 上,但我看到在 CentOS 6.4 及其库存内核上似乎出现了同样的问题。
我在处理程序中使用写入,因为 signal(7) 表示它是可重入的,因此在信号处理程序中使用是合法的。这也是我不打印sinfo->si_fd的值的原因; snprintf 不可重入。有一段时间我怀疑 stdio 库使用了 SIGIO,这就是为什么示例程序中的任何地方都没有 stdio 调用(除了可能在库函数 err(3) 中)。
感谢您花时间阅读我的代码。
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
int needRead = 0;
const unsigned int bufsize = 256;
void handler(int sig, siginfo_t *sinfo, void *value)
{
char *cp;
cp = "in handler. fd: ";
write(2, cp, strlen(cp));
switch(sinfo->si_fd) {
case 0: cp = "0\n"; break;
case 1: cp = "1\n"; break;
case 2: cp = "2\n"; break;
default: cp = "?\n"; break;
}
write(2, cp, strlen(cp));
needRead = 1;
}
int main(int argc, char *argv[])
{
struct sigaction act;
unsigned int counter = 0;
int flags;
char *outp = ".";
/* set up the signal handler for SIGIO */
act.sa_sigaction = handler;
act.sa_flags = 0;
act.sa_flags = SA_RESTART;
sigemptyset(&act.sa_mask);
if (sigaction(SIGIO, &act, NULL) == -1)
err(1, "attempt to set up handler for SIGIO failed");
/* arrange to get the signal */
if (fcntl(0, F_SETOWN, getpid()) == -1)
err(1, "fnctl to set F_SETOWN failed");
flags = fcntl(0, F_GETFL);
if (flags >= 0 && fcntl(0, F_SETFL, flags | O_ASYNC ) == -1)
err(1, "fnctl F_SETFL to set O_ASYNC failed");
while (1) {
char in_buf[bufsize];
int nc;
counter++;
write(STDERR_FILENO, outp, strlen(outp));
if (needRead) {
needRead = 0;
if ((nc = read(STDIN_FILENO, in_buf, bufsize)) == -1) {
err(1, "read from stdin failed");
} else {
outp = "Read '";
write(STDERR_FILENO, outp, strlen(outp));
write(STDERR_FILENO, in_buf, nc);
outp = "'\n";
write(STDERR_FILENO, outp, strlen(outp));
}
}
}
return 0;
}
最佳答案
啊,有趣。
简短的回答是,SIGIO 反复到达 stdin,因为 stdin 是可写,另外,您的 SIGIO 传递设置不正确。
为什么 si_fd 显然不可靠?
首先,需要在sa_flags
中指定SA_SIGINFO在你可以安全地使用 sa_sigaction
之前处理程序。
其次,你需要#define _GNU_SOURCE
并在 Linux 填写 si_fd
之前将 F_SETSIG 显式设置为 SIGIO (和 si_band
,就此而言)给你。有点傻,恕我直言,但事实就是如此。没有这个,值传递给 si_fd
正如您发现的那样,没有意义。
为什么要一遍又一遍地交付 SIGIO?
我猜你程序的标准输入是从你的调用 shell 继承的,我猜它是一个终端设备并且是可写的。正如 fd 0 会不断地 select(2) 可写一样,它也会不断地为您生成 SIGIO。
无论如何,si_band
持有答案。启用 F_SETSIG,#include <poll.h>
,并检查 si_band
对于 POLLIN、POLLOUT 等,以确定哪些 I/O 事件触发了信号。
真的,stdin 是可写的吗?
是的。试试这些进行比较:
$ [ -w /dev/stdin ] && echo Yes, stdin is writeable
Yes, stdin is writeable
# Endless SIGIOs
$ ./my-sigio-prog
^C
# No SIGIO
$ ./my-sigio-prog < /dev/null
# Two SIGIOs, after a delay. One for line-buffered "foo\n" and one for EOF
$ { sleep 3; echo foo; sleep 3; } | ./my-sigio-prog
关于c - SIGIO 到达文件描述符,我没有设置它,当没有 IO 是可能的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866754/
我想编写一个程序,在 SIGIO 上向线程池中的所有线程发出信号。我编写了以下代码,但似乎一个套接字一次只能由一个线程拥有,因此只会向线程池中的最后一个线程通知事件。有没有什么方法可以让我在不使用专用
我正在尝试接收串行数据,但我无法确定时间。 当接收到串行数据时,SIGIO 何时触发 - 在接收到数据的第一个字节时,还是在接收到特定字符(CR、NL...)时? 设置串行数据端口: /* Open
根据我的研究,您可以添加一个 epoll 文件描述符来轮询、选择或另一个 epoll,如果事件可用,它将返回 POLLIN。根据 epoll(7): Q3 Is the epoll file
这有点长...对于初学者,我使用的是 Linux 2.6.33,gcc 4.4.4。 我写了一个小程序,它创建一个命名管道并读取它,直到它看到一个特定的字符串,然后它摆脱 FIFO,并重新执行自己。
当文件描述符上可以进行 I/O 时,我尝试接收信号。程序在不执行 I/O 时需要执行其他操作,因此不宜使用 select(2)。 当我运行下面的示例代码时,即使标准输入上没有数据,它也会尽可能快地从处
我正在处理的 C++ 程序中遇到与 SIGIO 相关的问题。 平台:Linux 2.6.30,x86 Arch 场景:异步串行通信。 我关注了this async-communication exam
我编写了一个程序,该程序正在读取和写入一些 gpio 引脚,并使用数据报连接通过套接字接收一些数据。当收到 SIGIO 信号时,我想尝试从套接字读取数据。但是当我尝试这样做时,gpio 引脚上的读/写
在工作中,有人要求我在 Linux 上使用 C++ 为工业硬件实现一个新的 Controller 应用程序。一个关键特性是在使用 modbus 的设备之间使用 radio 调制解调器通信。我能够很好地
我正在实现一个客户端和服务器,它们使用sendto() 和recvfrom() 通过UDP 进行通信。我想在从服务器接收数据时为我的客户端使用 SIGPOLL 。我的问题是,在处理程序中,另一个信号到
我需要为 Linux 平台下的病人监护子系统创建一个 JNI c 库。我必须通过 USB 串行端口读取数据并写入命令。只要端口打开,设备就会以不同的频率和大小连续发送多种类型的数据。 在 Java 中
我正在尝试在文件描述符上可以进行 I/O 时接收信号。该程序在不执行 I/O 时需要执行其他操作,因此不能使用 select(2)。 当我运行下面的示例代码时,它会尽可能快地从处理程序内部打印消息,即
我正在学习 C 中的信号。我正在尝试编写一个符合 POSIX 和 ANSI 标准的程序。我希望我的程序在发生某些事情时发出信号,但 SIGIO 不是 POSIX 并且 SIGPOLL 在 POSIX
首先,当数据准备好读取时,我无法让串行设备生成 SIGIO。 我正在尝试编写一个简单的串行接口(interface),以使用 USB 转串行适配器与微型计算机进行通信。我想让这个接口(interfac
我正在使用 SIGIO 构建一个 UDP 非阻塞客户端和服务器套接字对,以促进服务器上数据的接收。这是我的情况。当客户端向我发送消息时,我会激活 SIGIO。然而,这些消息大约是。 3 个 1024
我在带有回调的异步模式下使用 ALSA (snd_async_add_pcm_handler())。每个 ALSA 的回调都是从 SIGIO 信号处理程序调用的。每个回调调用我的函数getCurren
我是一名优秀的程序员,十分优秀!