多路转接之poll和select
先看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的更多相关文章
- I/O多路转接之poll 函数
poll 一.poll()函数: 这个函数是某些Unix系统提供的用于执行与select()函数同等功能的函数,自认为poll和select大同小异,下面是这个函数的声明: #include < ...
- UNIX环境高级编程——I/O多路转接(select、pselect和poll)
I/O多路转接:先构造一张有关描述符的列表,然后调用一个函数,直到这些描述符中的一个已准备好进行I/O时,该函数才返回.在返回时,它告诉进程哪些描述符已准备好可以进行I/O. poll.pselect ...
- 高级I/O之I/O多路转接——pool、select
当从一个描述符读,然后又写到另一个描述符时,可以在下列形式的循环中使用阻塞I/O: ) if (write(STDOUT_FILENO, buf, n) != n) err_sys("wri ...
- IO多路转接select和poll
select IO多路复用的设置方法与信号的屏蔽有点相似: 信号屏蔽需要先设定一个信号集, 初始化信号集, 添加需要屏蔽的信号, 然后用sigprocmask设置 IO多路转接需要先设定一个文件描述符 ...
- 【Nginx】I/O多路转接之select、poll、epoll
当需要读两个以上的I/O的时候,如果使用阻塞式的I/O,那么可能长时间的阻塞在一个描述符上面,另外的描述符虽然有数据但是不能读出来,这样实时性不能满足要求,大概的解决方案有以下几种: 1.使用多进程或 ...
- select函数与I/O多路转接
select函数与I/O多路转接 相作大家都写过读写IO操作的代码,例如从socket中读取数据可以使用如下的代码: while( (n = read(socketfd, buf, BUFSIZE) ...
- Linux下I/O多路转接之select --fd_set
fd_set 你终于还是来了,能看到这个标题进来的,我想,你一定是和我遇到了一样的问题,一样的疑惑,接下来几个小时,我一定竭尽全力,写出我想说的,希望也正是你所需要的: 关于Linux下I/O多路转接 ...
- select 与 I/O多路转接
参考博客:http://blog.sina.com.cn/s/blog_607072980102uxcw.html I/0多路转接: 描述符表示某个I/O.构造一张有关描述符的数据表,调用select ...
- 多路转接模型之poll
poll系统调用和select类似.也是在指定时间内轮询一定数量的文件描写叙述符,以測试当中是否有就绪者.poll和select效率差点儿相同,仅仅是其使用接口相对简单些,poll不在局限于1024个 ...
随机推荐
- 多媒体编程基础之RGB和YUV
一.概念 1.什么是RGB? 对一种颜色进行编码的方法统称为“颜色空间”或“色域”.用最简单的话说,世界上任何一种颜色的“颜色空间”都可定义成一个固定的数字或变量.RGB(红.绿.蓝)只是众多颜色空间 ...
- python分析log
最近做的一个项目,系统log下会生成如下的log(部分): [-- ::] Processing File transfer configured from ship to shore.... [-- ...
- MFC六大核心机制之一:MFC程序的初始化
很多做软件开发的人都有一种对事情刨根问底的精神,例如我们一直在用的MFC,很方便,不用学太多原理性的知识就可以做出各种窗口程序,但喜欢钻研的朋友肯定想知道,到底微软帮我们做了些什么,让我们在它的框架下 ...
- Arch Linux 安装记录
Arch Linux 安装记录 基本上参考wiki上的新手指南,使用arch 2014.6.1 iso安装 设置网络 有线网络 Arch Linux 默认开启DHCP. 静态ip 首先关闭DHCP:s ...
- 有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎
有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 presto,hive,drill,calcite,sparksq
- sql语句小练习一
create database aaa go use aaa go create table student( sno varchar(3), sname varchar(4) not null ...
- C puzzles详解【34-37题】
第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...
- s3c6410_uboot中的代码重定位(nand->sdram)
本文仅探讨s3c6410从nand flash启动u-boot时的代码重定位过程 参考: 1)<USER'S MANUAL-S3C6410X>第二章 MEMORY MAP 第八章 NAND ...
- Winform TreeView 单选
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { //通过鼠标或者键盘触发事件,防止修改节点的Checke ...
- [Java]_函数传参的疑惑与思考
问题来源于leetcode上的两道题 Path Sum I && II,分别写了两个dfs. void dfs(TreeNode node , int sum , ArrayList& ...