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);

转载于:https://www.cnblogs.com/chenchenluo/p/3436541.html

fifo 上使用 select -- 转相关推荐

  1. linux select 异步,Linux上用select()实现异步的Echo服务器

    #相关代码, [四号程序员] http://www.coder4.com /* ?* main.cc ?* ?* ?Created on: 2009-11-30 ?* ? ? ?Author: lih ...

  2. IO:select、poll、epoll

    一.什么是IO? 我们都知道unix世界里,一切皆文件,而文件是什么呢?文件就是一串二进制流,不管socket,还是FIFO.管道.终端,对我们来说,一切都是文件,一切都是流.在信息交换的过程中,我们 ...

  3. Linux系统编程——I/O多路复用select、poll、epoll

    参考:https://segmentfault.com/a/1190000003063859 Linux下的I/O复用与epoll详解:https://www.cnblogs.com/lojunren ...

  4. Mybatis(史上最详+扩展插件工具使用)

    Mybatis-9.28 环境: JDK1.8 Mysql 5.7 maven 3.6.1 IDEA 回顾: JDBC Mysql Java基础 Maven Junit SSM框架:配置文件的. 最好 ...

  5. 网络编程(三):Linux 网络IO模型、select、pool、epoll 内核设计

    Linux网络IO模型 同步和异步,阻塞和非阻塞 同步和异步 关注的是调用方是否主动获取结果 同步: 同步的意思就是调用方需要主动等待结果的返回 异步: 异步的意思就是不需要主动等待结果的返回,而是通 ...

  6. select,epoll,poll比较

    select,poll,epoll简介 select select本质上是通过设置或者检查存放fd标志位的数据结构来进行下一步处理.这样所带来的缺点是: 1 单个进程可监视的fd数量被限制 2 需要维 ...

  7. mysql 一周一月_mysql查询当天、本周,本月,上一个月的数据

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...

  8. Linq之select子句

    在Linq中select子句用来指定查询结果的类型和表现形式.Linq查询要么以select子句结尾,要么以group子句结尾. 1 List<UserBaseInfo> users = ...

  9. mysql源码如何解析where字句_MySQL解析器源码分析--对select语句中子查询处理逻辑的分析(一)...

    背景 一个最简单的select语句包含select子句.from子句.where子句等,这些子句都不包含子查询(subselect),也没有union操作.而复杂的select语句包含select子句 ...

最新文章

  1. oracle 并接去掉字符串,ORACLE删除字符-TRIM字符截取-substr查找字符-instr
  2. 009_Redis的事物
  3. 给文物安全信息化支个招
  4. 【SDK接入篇】【1】Unity的internal 与 gradle打包
  5. 【面试】c++单例模式
  6. 根据url提取网站域名的方法小结
  7. Java案例:压缩与解压缩文件
  8. linux循环控制结构,Linux Shell 之 Shell 基本控制结构(二)(循环结构)
  9. thuwc9102划水记
  10. win32汇编动态链接库的编写及使用
  11. 普元EOS开发总结——前端框架
  12. java px pt转化_pt和px的转换
  13. 计算机软件工程课程设计感想,软件课程设计心得体会.docx
  14. Android Emulator has terminated
  15. Kubernetes架构基础知识
  16. java的书写规范_java书写规范以及技巧
  17. The Development of Han Chinese’s Cuisine and Diet
  18. 第三届太原理工大学程序设计竞赛新生赛(未完结)
  19. Android studio运行出现Unable to determine application id: com.android.tools.idea.run.ApkProvisionExcepti
  20. mysql赋予用户权限

热门文章

  1. 二叉树的前序遍历,中序遍历,后序遍历学习 (原)
  2. Ubuntu安装软件失败
  3. bzoj 2157 旅行
  4. HttpSession常见问题
  5. 《LeetCode力扣练习》第96题 不同的二叉搜索树 Java
  6. R语言常用包分类总结
  7. 决策树模型回归可视化分析_Excel进行线性回归模型分析的操作——「杏花开生物医药统计」...
  8. 计算机应用基础东师,2018年东师计算机应用基础.doc
  9. 电脑键盘数字键失灵_C4D/3dmax/MaYa三维设计——高档机械键盘推荐·红轴(200元左右预算)...
  10. oracle asm clsecho,ASM磁盘组一点管理