C语言 SIGIO 到达文件描述符我没有设置它,当没有 IO 是不可能的



当文件描述符上可以进行 I/O 时,我正在尝试接收信号。 程序在不执行 I/O 时需要做其他事情, 所以使用 select(2) 不是一个选项。

当我运行下面的示例代码时,它会尽可能快地从处理程序内部打印消息,即使 stdin 上没有数据也是如此。 更奇怪的是,siginfo_t结构中报告的文件描述符因运行而异。 我只为标准输入 (fd 0) 设置它;为什么处理程序会报告任何其他值? 有时我看到 0,有时,我看到 1,大多数时候我看到"?",它表示 0、1 或 2 以外的值。

这是在OpenSUSE 12.3,Linux内核3.7.10-1.16上,但我看到CentOS 6.4及其库存内核似乎存在相同的问题。

我在处理程序中使用 write,因为 signal(7) 说它是可重入的,因此在信号处理程序中使用是合法的。 这也是为什么我不打印信息>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 = "0n"; break;
        case 1: cp = "1n"; break;
        case 2: cp = "2n"; 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并明确地F_SETSIG SIGIO,然后Linux才会为您填写si_fd(以及si_band)。 有点傻,恕我直言,但事实就是如此。 如果没有这个,传递给si_fd的值就没有意义,正如你所发现的。

为什么SIGIO被一遍又一遍地交付?

我猜你的程序的 stdin 是从你的调用 shell 继承而来的,我猜它是一个终端设备并且是可写的。 正如 fd 0 会持续选择(2) 可写一样,它也会不断为你生成 SIGIO。

无论如何,si_band掌握答案。 启用F_SETSIG、#include <poll.h>和检查 POLLIN、POLLOUT 等si_band,以确定哪些 I/O 事件使信号跳闸。

真的,标准是可写的吗?

是的。 尝试以下比较:

$ [ -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 "foon" and one for EOF
$ { sleep 3; echo foo; sleep 3; } | ./my-sigio-prog

相关内容

最新更新