4

我一直在尝试从一组命名管道中进行轮询一段时间,并且我不断收到 POLLNVAL 对任何命名管道文件描述符的即时响应。在找到这篇关于 OS X 中的错误轮询的博客文章后,我很确定这是 OS X 中的一个错误。

我已经计划将我的代码切换为使用 UDP 套接字,但我想要求 SO 对此进行验证 a) 以便我确定它真的坏了,并且 b) 用于文档目的。

这是我编写的代码的精简版本(尽管上面链接中的代码经过我测试,拼写得很好):

#includes
...
....
#

static const char* first_fifo_path = "/tmp/fifo1";
static const char* second_fifo_path = "/tmp/fifo2";

int setup_read_fifo(const char* path){
  int fifo_fd = -1;

  if( mkfifo(path, S_IRWXU | S_IRWXG | S_IRWXO) )
    perror("error calling mkfifo()... already exists?\n");

  if((fifo_fd = open(path, O_RDONLY | O_NDELAY)) < 0)
    perror("error calling open()");

  return fifo_fd;
}

void do_poll(int fd1, int fd2){
  char inbuf[1024];
  int num_fds = 2;
  struct pollfd fds[num_fds];
  int timeout_msecs = 500;

  fds[0].fd = fd1;
  fds[1].fd = fd2;
  fds[0].events = POLLIN;
  fds[1].events = POLLIN;

  int ret;
  while((ret = poll(fds, num_fds, timeout_msecs)) >= 0){
    if(ret < 0){
      printf("Error occured when polling\n");
      printf("ret %d, errno %d\n", ret, errno);
      printf("revents =  %xh : %xh \n\n", fds[0].revents, fds[1].revents);
    }

   if(ret == 0){
      printf("Timeout Occurred\n");
      continue;
    }                                                                   

    for(int i = 0; i< num_fds; i++){
      if(int event = fds[i].revents){

        if(event & POLLHUP)
          printf("Pollhup\n");
        if(event & POLLERR)
          printf("POLLERR\n");
        if(event & POLLNVAL)
          printf("POLLNVAL\n");

        if(event & POLLIN){
          read(fds[i].fd, inbuf, sizeof(inbuf));
          printf("Received: %s", inbuf);
        }
      }
    }
  }
}

int main (int argc, char * const argv[]) {
  do_poll(setup_read_fifo(first_fifo_path), setup_read_fifo(second_fifo_path));
  return 0;
}

这输出:

$ ./执行
轮询
轮询
轮询
轮询
轮询
轮询
轮询
轮询
轮询
...

令人作呕。

还有其他人遇到这个吗?这是一个真正的错误,对吧?

4

3 回答 3

4

这似乎是一个真正的错误。它在 Linux 和 OpenBSD 上按预期工作,但正如您在 OS X 上描述的那样失败。

于 2009-02-26T19:19:17.023 回答
4

OSX 10.4.1,我可以确认行为。相同的代码在 Linux 上工作正常(只要超时消息正常)。所有的证据,包括这个 - http://www.virtualbox.de/changeset/12347 - 都表明存在真正的问题。

于 2009-02-26T19:28:33.363 回答
2

是的,已知的错误。我认为投票中断只是从 10.4 开始,我们不得不在 Fink 中处理它。Glib 的 configure.in 对此进行了测试,因此您可以确定您没有想象到它。(嗯,不完全是这个,glib 测试设备上的轮询,而不是 fifos。)

于 2009-03-11T20:14:19.697 回答