gpt4 book ai didi

c - 如何在 Linux 中使用 poll C 函数来监视命名管道?

转载 作者:IT王子 更新时间:2023-10-29 00:27:57 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,我应该在其中监视一些命名管道的末端使用轮询功能。我有一个 for 循环来检查每个管道,只要 poll 返回 >0 并且我知道当管道从另一端的过程关闭时,我将得到 POLLHUP 或 POLLIN | pollfd 结构的 revents 字段中的 POLLHUP。

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

最佳答案

最小示例

来源如下。用法:

sudo mknod poll0.tmp p
sudo mknod poll1.tmp p
sudo chmod 666 poll*.tmp
./poll.out

在另一个外壳上:

printf a > poll0.tmp
printf b > poll1.tmp

输出:

loop
POLLIN i=0 n=1 buf=a
loop
POLLHUP i=0
loop
POLLIN i=1 n=1 buf=b
POLLHUP i=1
loop

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

更酷的例子:

(while true; do date; sleep 1; done) > poll0.tmp &
(while true; do date; sleep 2; done) > poll1.tmp &

0 每隔一秒写入一次,1 每两秒写入一次,这显示了 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;
}
}
}
}

编译:

gcc -o poll.out -std=c99 poll.c

在 Ubuntu 14.04 中测试。

GitHub upstream .

回答原来的问题:

when one pipe does indeed get closed and returns POLLHUP to me, what will happen on the next loop? Is it going to return POLLHUP again and again in the next and any subsequent loop or is poll function going to ignore it after the first POLLHUP?

删除行:

close(pfds[i].fd);
pfds[i].fd *= -1;

你会看到它在 POLLHUP 上永远循环。

只删除:

close(pfds[i].fd);

你得到的是 POLLNVAL,因为它试图使用一个关闭的 fd:How to handle the Linux socket revents POLLERR, POLLHUP and POLLNVAL?

关于c - 如何在 Linux 中使用 poll C 函数来监视命名管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com