如何使用poll C函数在Linux中查看命名管道?

nik*_*kos 13 c linux

我正在尝试编写一个程序,我应该使用poll函数观察一些命名管道的末尾.每当poll返回> 0时,我都有一个for循环检查每个管道,我知道当管道从另一端的程序关闭时,我将获得POLLHUP或POLLIN | pollfd结构的revents字段中的POLLHUP.

我的问题是:当一个管道确实关闭并将POLLHUP返回给我时,下一个循环会发生什么?它会在下一个循环和后续循环中一次又一次地返回POLLHUP,还是在第一个POLLHUP之后将轮询函数忽略它?

Cir*_*四事件 5

最小的例子

来源如下。用法:

sudo mknod poll0.tmp p
sudo mknod poll1.tmp p
sudo chmod 666 poll*.tmp
./poll.out
Run Code Online (Sandbox Code Playgroud)

在另一个外壳上:

printf a > poll0.tmp
printf b > poll1.tmp
Run Code Online (Sandbox Code Playgroud)

输出:

loop
POLLIN i=0 n=1 buf=a
loop
POLLHUP i=0
loop
POLLIN i=1 n=1 buf=b
POLLHUP i=1
loop
Run Code Online (Sandbox Code Playgroud)

因此请注意如何poll在不循环的情况下等待读取。

冷却器示例:

(while true; do date; sleep 1; done) > poll0.tmp &
(while true; do date; sleep 2; done) > poll1.tmp &
Run Code Online (Sandbox Code Playgroud)

01每隔一秒和每两秒写入一次,这显示了如何poll()同时处理两个输入,而不会互相拖延。

来源:

民意调查.c

#define _XOPEN_SOURCE 700
#include <fcntl.h> /* creat, O_CREAT */
#include <poll.h> /* poll */
#include <stdio.h> /* printf, puts, snprintf */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include <unistd.h> /* read */

int main(void) {
    enum { N = 2 };
    char buf[1024], path[1024];
    int fd, i, n;
    short revents;
    struct pollfd pfds[N];

    for (i = 0; i < N; ++i) {
        snprintf(path, sizeof(path), "poll%d.tmp", i);
        /* O_NONBLOCK is required or else the open blocks
         * until the other side of the pipe opens. */
        fd = open(path, O_RDONLY | O_NONBLOCK);
        if (fd == -1) {
            perror("open");
            exit(EXIT_FAILURE);
        }
        pfds[i].fd = fd;
        /* Only events in this mask will be listened to.
         * However, there are also some events that are unmaskable,
         * notably POLLHUP when pipe closes! */
        pfds[i].events = POLLIN;
    }
    while (1) {
        puts("loop");
        i = poll(pfds, N, -1);
        if (i == -1) {
            perror("poll");
            exit(EXIT_FAILURE);
        }
        for (i = 0; i < N; ++i) {
            revents = pfds[i].revents;
            if (revents & POLLIN) {
                n = read(pfds[i].fd, buf, sizeof(buf));
                printf("POLLIN i=%d n=%d buf=%.*s\n", i, n, n, buf);
            }
            if (revents & POLLHUP) {
                printf("POLLHUP i=%d\n", i);

                /* This happens when the other side closed.
                 * This event is only cleared when we close the reader. */

                /* poll won't set POLLHUP anymore once all fds are closed.
                 * Any futher polls on this will give the POLLNVAL event instead. */
                close(pfds[i].fd);

                /* negative fds are ignored. So if we negate an FD,
                 * we can both turn if off for a while, and turn it on
                 * later on by re-nagating it. */
                pfds[i].fd *= -1;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编译:

gcc -o poll.out -std=c99 poll.c
Run Code Online (Sandbox Code Playgroud)

在 Ubuntu 14.04 中测试。

GitHub 上游.

回答原来的问题:

当一个管道确实关闭并向我返回 POLLHUP 时,下一个循环会发生什么?它是否会在下一个循环和任何后续循环中一次又一次地返回 POLLHUP,或者 poll 函数是否会在第一个 POLLHUP 之后忽略它?

删除行:

close(pfds[i].fd);
pfds[i].fd *= -1;
Run Code Online (Sandbox Code Playgroud)

你会看到它永远循环下去POLLHUP

仅删除:

close(pfds[i].fd);
Run Code Online (Sandbox Code Playgroud)

POLLNVAL相反,当它尝试使用关闭的 fd 时,您会得到: How to handle the Linux socket revents POLLERR, POLLHUP and POLLNVAL?


Jos*_*osh 3

它将继续在 revents 中设置 POLLHUP。
另请参阅http://linux.die.net/man/3/poll以供参考。