先看poll():

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h> #define oops(x, n) { perror(x); exit(n); } int main()
{
mkfifo("fifo1", );
mkfifo("fifo2", ); int fd1 = open("fifo1", O_RDWR);
int fd2 = open("fifo2", O_RDWR);
char buf[];
for(;;)
{
struct pollfd pfd[] = {{, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}};
int ret = poll(pfd, , );
if(ret > )
{
if(pfd[].revents&POLLIN)
{
scanf(" %[^\n]", buf);
printf("keypad:%s\n", buf);
if(strcmp(buf, "quit") == ) break;
}
if(pfd[].revents&POLLIN)
{
int n = read(fd1, buf, sizeof(buf));
buf[n] = '\0';
printf("fifo1:%s", buf);
}
if(pfd[].revents&POLLIN)
{
int n = read(fd2, buf, sizeof(buf));
buf[n] = '\0';
printf("fifo2:%s", buf);
}
} }
close(fd1);
close(fd2);
unlink("fifo1");
unlink("fifo2"); return ;
}

再来说说select():

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h> #define oops(x, n) { perror(x); exit(n); } int main()
{
mkfifo("fifo1", );
mkfifo("fifo2", ); int fd1 = open("fifo1", O_RDWR);
int fd2 = open("fifo2", O_RDWR);
char buf[];
for(;;)
{
struct pollfd pfd[] = {{, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}};
int ret = poll(pfd, , );
if(ret > )
{
if(pfd[].revents&POLLIN)
{
scanf(" %[^\n]", buf);
printf("keypad:%s\n", buf);
if(strcmp(buf, "quit") == ) break;
}
if(pfd[].revents&POLLIN)
{
int n = read(fd1, buf, sizeof(buf));
buf[n] = '\0';
printf("fifo1:%s", buf);
}
if(pfd[].revents&POLLIN)
{
int n = read(fd2, buf, sizeof(buf));
buf[n] = '\0';
printf("fifo2:%s", buf);
}
} }
close(fd1);
close(fd2);
unlink("fifo1");
unlink("fifo2"); return ;
}

多路转接之poll和select的更多相关文章

  1. I/O多路转接之poll 函数

    poll 一.poll()函数: 这个函数是某些Unix系统提供的用于执行与select()函数同等功能的函数,自认为poll和select大同小异,下面是这个函数的声明: #include < ...

  2. UNIX环境高级编程——I/O多路转接(select、pselect和poll)

    I/O多路转接:先构造一张有关描述符的列表,然后调用一个函数,直到这些描述符中的一个已准备好进行I/O时,该函数才返回.在返回时,它告诉进程哪些描述符已准备好可以进行I/O. poll.pselect ...

  3. 高级I/O之I/O多路转接——pool、select

    当从一个描述符读,然后又写到另一个描述符时,可以在下列形式的循环中使用阻塞I/O: ) if (write(STDOUT_FILENO, buf, n) != n) err_sys("wri ...

  4. IO多路转接select和poll

    select IO多路复用的设置方法与信号的屏蔽有点相似: 信号屏蔽需要先设定一个信号集, 初始化信号集, 添加需要屏蔽的信号, 然后用sigprocmask设置 IO多路转接需要先设定一个文件描述符 ...

  5. 【Nginx】I/O多路转接之select、poll、epoll

    当需要读两个以上的I/O的时候,如果使用阻塞式的I/O,那么可能长时间的阻塞在一个描述符上面,另外的描述符虽然有数据但是不能读出来,这样实时性不能满足要求,大概的解决方案有以下几种: 1.使用多进程或 ...

  6. select函数与I/O多路转接

    select函数与I/O多路转接 相作大家都写过读写IO操作的代码,例如从socket中读取数据可以使用如下的代码: while( (n = read(socketfd, buf, BUFSIZE) ...

  7. Linux下I/O多路转接之select --fd_set

    fd_set 你终于还是来了,能看到这个标题进来的,我想,你一定是和我遇到了一样的问题,一样的疑惑,接下来几个小时,我一定竭尽全力,写出我想说的,希望也正是你所需要的: 关于Linux下I/O多路转接 ...

  8. select 与 I/O多路转接

    参考博客:http://blog.sina.com.cn/s/blog_607072980102uxcw.html I/0多路转接: 描述符表示某个I/O.构造一张有关描述符的数据表,调用select ...

  9. 多路转接模型之poll

    poll系统调用和select类似.也是在指定时间内轮询一定数量的文件描写叙述符,以測试当中是否有就绪者.poll和select效率差点儿相同,仅仅是其使用接口相对简单些,poll不在局限于1024个 ...

随机推荐

  1. Laxcus大数据管理系统2.0(14)- 后记

    后记 Laxcus最早源于一个失败的搜索引擎项目,项目最后虽然终止了,但是项目中的部分技术,包括FIXP协议.Diffuse/Converge算法.以及很多新的数据处理理念却得以保留下来,这些成为后来 ...

  2. Start:at cnblogs firstDay

    C#旨在设计成为一种"简单.现代.通用",以及面向对象的程序设计语言,此种语言的实现,应提供对于以下软件工程要素的支持:强类型检查.数组维度检查.未初始化的变量引用检测.自动垃圾收 ...

  3. 用Java开发代理服务器

    基础知识 不管以哪种方式应用代理服务器,其监控HTTP传输的过程总是如下: 步骤一:内部的浏览器发送请求给代理服务器.请求的第一行包含了目标URL. 步骤二:代理服务器读取该URL,并把请求转发给合适 ...

  4. mongodb分组,的两种方式,先记一下

    using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using NationalUnion.AdGalle ...

  5. MFC读取XML文件并解析

    现在经常会对XML文件进行操作,怎么在MFC下去读和解析XML文件呢?直接上代码: 首先得等在stdafx.h中加入这句,以引入MSXML命名空间 #import <msxml3.dll> ...

  6. ionic 不同view的數據交互

    angular中通過service factory 等服務來對不同的控制器進行數據交互 ,ionic 也一樣... var app = angular.module('ionicApp', ['ion ...

  7. Windows phone 8 学习笔记(9) 集成(转)

    本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用提供的相关支持:锁定屏幕展示了我们可以对锁定屏幕提供背景图像 ...

  8. EnCase v7 could not recognize Chinese character folder names / file names on Linux Platform

    Last week my friend brought me an evidence file duplicated from a Linux server, which distribution i ...

  9. JS兼容getElementsByClassName

    getElementsByClassName是通过class来获取DOM,但是IE8及以下不能兼容.这里做了一下兼容性. HTML: <div class="pox"> ...

  10. sublime text2支持ng

    这里面记录了sublime text3的一些破解和sublime text2支持ng的方法. http://weblogs.asp.net/dwahlin/archive/2013/08/30/usi ...