epoll函数分析
来源:互联网 发布:巨人网络总部地址 编辑:程序博客网 时间:2024/06/06 16:25
阻塞IO和进程的多路复用。
相比select的优点:
epoll();
1.高效
2.监控无上限
epoll的使用:
epoll_create/epoll_creatl(创建监听池)
epoll_ctl(添加监听事件)
epoll_wait(等待事件发生)
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/epoll.h> int main(){ int fd1,fd2; int efd; struct epoll_event event; struct epoll_event *events; char c; //创建fifo mkfifo("/tmp/fifo1",0666); mkfifo("/tmp/fifo2",0666); fd1 = open("/tmp/fifo1",O_RDONLY); fd2 = open("/tmp/fifo2",O_RDONLY); //创建监听池 efd = epoll_create1(0); //构造监听事件,加入监听池 event.events = EPOLLIN|EPOLLET; event.data.fd = fd1; epoll_ctl(efd,EPOLL_CTL_ADD,fd1,&event); event.events = EPOLLIN|EPOLLET; event.data.fd = fd2; epoll_ctl(efd,EPOLL_CTL_ADD,fd2,&event); int n = 0; events = calloc(100,sizeof(event)); n = epoll_wait(efd,events,100,-1); int i = 0; for(i = 0;i<n;i++) { if(events[i].events&EPOLLIN) { read(events[i].data.fd,&c,1); printf("file %d can be read\n",events[i].data.fd); } if(events[i].events&EPOLLOUT) { //处理 } if(events[i].events&EPOLLERR) { //处理 } } free(events); close(fd1); close(fd2); }
测试1:ew1.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/epoll.h>int main(){ int fd; char c='c'; fd = open("/tmp/fifo1",O_WRONLY); write(fd,&c,1); close(fd); return 0;}
测试1:ew2.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/epoll.h>int main(){ int fd; char c='c'; fd = open("/tmp/fifo2",O_WRONLY); write(fd,&c,1); close(fd); return 0;}
运行epoll 程序进入阻塞状态,然后运行ew1,ew2
打印并退出阻塞。
阅读全文
1 0
- epoll函数分析
- epoll源码分析---sys_epoll_ctl()函数
- epoll源码分析---sys_epoll_create()函数
- epoll源码分析---sys_epoll_wait()函数
- epoll源码分析---sys_epoll_create()函数
- epoll源码分析---sys_epoll_ctl()函数
- socket编程epoll函数--epoll实现过程分析
- epoll分析
- epoll 分析
- epoll函数
- epoll函数
- epoll函数
- epoll函数
- epoll 函数
- linux epoll模型源码分析 一 函数实现
- libevent源码分析--epoll中的几个函数 epoll_init epoll_add epoll_dispatch
- poll epoll 分析
- EPOLL源码分析
- 【DP】TEST20170506
- eclipse中用maven 插件把项目打包成jar
- const对象和const成员函数
- CSS3——pointer-events
- Android实战技巧之四十:Android5.1.1源代码编译与烧写
- epoll函数分析
- POJ1845 Sumdiv【快速模幂+素因子分解+等比数列+二分法】
- RIP动态路由配置
- OPENGL—DDA画直线
- 计算机的主要构成和工作步骤
- iOS UI学习之路05
- 96. Unique Binary Search Trees
- Python中sort和sorted快排
- 在tomcat中设置JDK,一个电脑上装两个JDK