多路转接之poll和select
2015-03-29 21:03
211 查看
先看poll():
View Code
#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", 0644); mkfifo("fifo2", 0644); int fd1 = open("fifo1", O_RDWR); int fd2 = open("fifo2", O_RDWR); char buf[1000]; for(;;) { struct pollfd pfd[] = {{0, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}}; int ret = poll(pfd, 3, 1); if(ret > 0) { if(pfd[0].revents&POLLIN) { scanf(" %[^\n]", buf); printf("keypad:%s\n", buf); if(strcmp(buf, "quit") == 0) break; } if(pfd[1].revents&POLLIN) { int n = read(fd1, buf, sizeof(buf)); buf = '\0'; printf("fifo1:%s", buf); } if(pfd[2].revents&POLLIN) { int n = read(fd2, buf, sizeof(buf)); buf = '\0'; printf("fifo2:%s", buf); } } } close(fd1); close(fd2); unlink("fifo1"); unlink("fifo2"); return 0; }
View Code
相关文章推荐
- UNIX环境高级编程——I/O多路转接(select、pselect和poll)
- 高性能服务器——I/O多路转接的三种模式(select &poll& epoll)
- 【Nginx】I/O多路转接之select、poll、epoll
- I/O多路转接select/poll/epoll的对比
- 多路复用 I/O 多路转接 select / poll / epoll
- apue I/O多路转接,select与poll
- I/O多路转接复用机制---select,poll,epoll
- I/O多路转接(select、pselect和poll)使用
- I/O多路转接之select、poll、epoll
- I/O多路转接之select、poll、epoll
- 深入研究socket编程(2)——I/O多路转接(select、pselect和poll)
- UNIX环境高级编程——I/O多路转接(select、pselect和poll)
- 高级I/O多路转接之select poll epoll 区别
- UNIX环境高级编程——I/O多路转接(select、pselect和poll)
- Linux:I/O多路转接之select(有图有代码有真相!!!)
- I/O多路转接 ---- poll
- I/O多路转接之select
- 多路转接服务器之select
- 网路基础 — 浅析I/O多路转接之poll技术
- IO模型及多路复用IO(select、poll、epoll)