先看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. Digest [information value filtering]

    http://oak.cs.ucla.edu/~cho/papers/cho-filter.pdf 最近对评估方法进行研究,这篇paper是一种对IR方法进行总结,不错的角度,也比较全面

  2. SVN与TortoiseSVN实战:补丁详解

    硬广:<SVN与TortoiseSVN实战>系列已经写了五篇,第二篇<SVN与TortoiseSVN实战:标签与分支>和第三篇<SVN与TortoiseSVN实战:Tor ...

  3. Windows USN Journal Parsing

    What is "USN Journal"? It is "Update Sequence Number Journal". It records change ...

  4. javascript ES5 Object对象

    原文:http://javascript.ruanyifeng.com/stdlib/object.html 目录 概述 Object对象的方法 Object() Object.keys(),Obje ...

  5. JS常用小技巧

    iframe子窗口调用父窗口方法 parent.functionName(); 二.iframe 父窗口和子窗口相互的调用方法 1.IE中使用方法: 父窗口调用子窗口:iframe_ID.iframe ...

  6. .NET 中的DateTime

    DateTime简介 DateTime是.NET中的时间类型,可以通过DateTime完成诸如获取当前的系统时间等操作.DateTime在.NET中是一个结构体,而并不是一个类. 如上图所示,这个图标 ...

  7. POJ C程序设计进阶 编程题#1:分配病房

    编程题#1:分配病房 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 某个科 ...

  8. 标签栏控制器(UITabBarController)

    一.标签栏控制器基础 1.绪论 a.一个标签栏是一个独立的界面对象,它经常与一个视图控制器(UITabBarController是UIViewController的子类)一起使用. b.一个标签栏项目 ...

  9. Cookie与Session的一些总结

    一.Cookie: Cookie主要存储一些不敏感的数据,只能存储字符串类型 执行过程: (1)第一次请求: 客户端将数据(比如用户名)以请求报文的形式请求服务器端响应, 服务器端得到数据(用户名), ...

  10. 通过 XML HTTP 把文本文件载入 HTML 元素

    新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="01-通 ...