zoukankan      html  css  js  c++  java
  • fifo 上使用 select -- 转

    http://www.outflux.net/blog/archives/2008/03/09/using-select-on-a-fifo/

    The right way to handle on-going input from file descriptors is to use select(). All readable events are flagged (one such event is “end of file”, which is indicated by a 0-sized read()). For example, if we’re reading from file descriptor fd:

      fd_set rfds;
      int rc;
    
      FD_ZERO(&rfds);
      FD_SET(fd, &rfds);
    
      tv.tv_sec = 1;
      tv.tv_usec = 0;
    
      rc = select(fd + 1, &rfds, NULL, NULL, &tv);
      if (rc > 0) {
        char buf[80];
        ssize_t got = read(fd, buf, sizeof(buf));
        if (got < 0) {
          perror("read");
          return 1;
        }
        else if (got == 0) {
          printf("EOF\n");
          return 1;
        }
        else {
          printf("read bytes: %d\n", got);
        }
      }
    

    When dealing with sockets, the above loop is sane — EOF means the other end hung up and you’ll never get more data from the file descriptor. In the case of a FIFO, however, “0 length read” means there are no more FIFO writers — but more could attach later and continue feeding in data! The problem with this is that select misbehaves and marks the file descriptor as “EOF” forever. Only the initial select() call blocks until there is something to read — once it hits EOF, select will always immediately return, defeating the purpose of select().

    One solution is to re-open the FIFO, but you might miss writers between your 0-length read() and the next open().

    The seemingly correct solution is rather simple: the FIFO reader should open the FIFO as a writer also. In this case, select() never thinks all the writers have vanished, so there is never an EOF condition, and the reader never misses any writers. So, instead of O_RDONLY, use:

    fd = open(FIFO_PATHNAME, O_RDWR | O_NONBLOCK);
    
  • 相关阅读:
    WordCount C语言实现求文本的字符数,单词数,行数
    iOS 数据持久化
    Apple store加急审核
    iOSTableViewCell不等高的几种方法
    Xcode 插件(时间就是生命)
    iOS UI组件汇总
    iOS之获取经纬度并通过反向地理编码获取详细地址
    iOS 开发常用宏
    iOS让你的app一直在后台活着(运行)
    OC动画——基础动画CABasicAnimation使用
  • 原文地址:https://www.cnblogs.com/chenchenluo/p/3436541.html
Copyright © 2011-2022 走看看