当前位置:网站首页>select/poll/epoll
select/poll/epoll
2022-06-28 17:45:00 【MrPeng1991】
1. IO系列——io模型初探
2.IO系列——select/poll/epoll
select/poll/epoll都是IO多路复用机制,可以同时监控多个描述符,当某个描述符就绪(读或写就绪),则立刻通知相应程序进行读或写操作。本质上select/poll/epoll都是同步I/O,即读写是阻塞的。
1. select
int select (int maxfd, fd_set *readfds,
fd_set *writefds,
fd_set *exceptfds,
struct timeval *timeout);
- maxfd:代表要监控的最大文件描述符fd+1
- writefds:监控可写fd
- readfds:监控可读fd
- exceptfds:监控异常fd
- timeout:超时时长
- NULL,代表没有设置超时,则会一直阻塞直到文件描述符上的事件触发
- 0,代表不等待,立即返回,用于检测文件描述符状态
- 正整数,代表当指定时间没有事件触发,则超时返回
select函数监控3类文件描述符,调用select函数后会阻塞,直到描述符fd准备就绪(有数据可读、可写、异常)或者超时,函数便返回。 当select函数返回后,可通过遍历描述符集合,找到就绪的描述符。
select缺点
- 文件描述符个数受限:单进程能够监控的文件描述符的数量存在最大限制,在Linux上一般为1024,可以通过修改宏定义增大上限,但同样存在效率低的弱势;
- 性能衰减严重:IO随着监控的描述符数量增长,其性能会线性下降;
2.poll
int poll (struct pollfd *fds, unsigned int nfds, int timeout);
//pollfd表示监视的描述符集合,如下
struct pollfd {
int fd; //文件描述符
short events; //监视的请求事件
short revents; //已发生的事件
};
pollfd结构包含了要监视的event和发生的event,并且pollfd并没有最大数量限制。 和select函数一样,当poll函数返回后,可以通过遍历描述符集合,找到就绪的描述符。
poll缺点:
从上面看select和poll都需要在返回后,通过遍历文件描述符来获取已经就绪的socket。同时连接的大量客户端在同一时刻可能只有很少的处于就绪状态,因此随着监视的描述符数量的增长,其性能会线性下降。
3 epoll
epoll是在内核2.6中提出的,是select和poll的增强版。相对于select和poll来说,epoll更加灵活,没有描述符数量限制。epoll使用一个文件描述符管理多个描述符,将用户空间的文件描述符的事件存放到内核的一个事件表中,这样在用户空间和内核空间的copy只需一次。epoll机制是Linux最高效的I/O复用机制,在一处等待多个文件句柄的I/O事件。
select/poll都只有一个方法,epoll操作过程有3个方法,分别是epoll_create(), epoll_ctl(),epoll_wait()
3.1 epoll_create
用于创建一个epoll的句柄,size是指监听的描述符个数, 现在内核支持动态扩展,该值的意义仅仅是初次分配的fd个数,后面空间不够时会动态扩容。 当创建完epoll句柄后,占用一个fd值
ls /proc/<pid>/fd/ //可通过终端执行,看到该fd
使用完epoll后,必须调用close()关闭,否则可能导致fd被耗尽。
3.2 epoll_ctl
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
用于对需要监听的文件描述符(fd)执行op操作,比如将fd加入到epoll句柄。
epfd:是epoll_create()的返回值;
op:表示op操作,用三个宏来表示,分别代表添加、删除和修改对fd的监听事件;
- EPOLL_CTL_ADD(添加)
- EPOLL_CTL_DEL(删除)
- EPOLL_CTL_MOD(修改)
fd:需要监听的文件描述符;
epoll_event:需要监听的事件,struct epoll_event结构如下:
struct epoll_event {
__uint32_t events; /* Epoll事件 */
epoll_data_t data; /*用户可用数据*/
};
events可取值:(表示对应的文件描述符的操作)
- EPOLLIN :可读(包括对端SOCKET正常关闭);
- EPOLLOUT:可写;
- EPOLLERR:错误;
- EPOLLHUP:中断;
- EPOLLPRI:高优先级的可读(这里应该表示有带外数据到来);
- EPOLLET: 将EPOLL设为边缘触发模式,这是相对于水平触发来说的。
- EPOLLONESHOT:只监听一次事件,当监听完这次事件之后就不再监听该事件
3.3 epoll_wait
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);
等待事件的上报
- epfd:等待epfd上的io事件,最多返回maxevents个事件;
- events:用来从内核得到事件的集合;
- maxevents:events数量,该maxevents值不能大于创建epoll_create()时的size;
- timeout:超时时间(毫秒,0会立即返回)。
该函数返回需要处理的事件数目,如返回0表示已超时。
4. 对比
在 select/poll中,进程只有在调用一定的方法后,内核才对所有监视的文件描述符进行扫描,而epoll事先通过epoll_ctl()来注册一个文件描述符,一旦基于某个文件描述符就绪时,内核会采用类似callback的回调机制,迅速激活这个文件描述符,当进程调用epoll_wait() 时便得到通知。(此处去掉了遍历文件描述符,而是通过监听回调的的机制。这正是epoll的魅力所在。)
epoll优势
- 监视的描述符数量不受限制,所支持的FD上限是最大可以打开文件的数目,具体数目可以
cat /proc/sys/fs/file-max查看,一般来说这个数目和系统内存关系很大,以3G的手机来说这个值为20-30万。 - IO性能不会随着监视fd的数量增长而下降。epoll不同于select和poll轮询的方式,而是通过每个fd定义的回调函数来实现的,只有就绪的fd才会执行回调函数。
如果没有大量的空闲或者死亡连接,epoll的效率并不会比select/poll高很多。但当遇到大量的空闲连接的场景下,epoll的效率大大高于select/poll。
边栏推荐
- Architecture autonomy service: building data-driven architecture insight
- GCC getting started manual
- Database mysql statement final review CTGU
- Apifox 介绍
- 6 - Dictionary
- halcon知识:矩阵专题【01】
- Applet graduation project is based on wechat property maintenance application property applet graduation project opening report function reference
- oracle cdc 但是使用的服务名没有sid 该怎么配置呢?
- Ding! Techo day Tencent technology open day arrived as scheduled!
- Win10 compiles curl library source code with cmake3.22 and vs2019 and calls
猜你喜欢

2022起重机械指挥考试题库模拟考试平台操作

Node foundation ~ node level

58技术沙龙第三十一期|Flutter动态化专题沙龙

Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence

Unity about oculus quest2 basic development based on XR interaction toolkit 003- capture function - making a VR bowling game

The fourth largest operator cannot be a "catfish"

2022 operation of simulated examination platform of hoisting machinery command examination question bank

MySQL十种锁,一篇文章带你全解析

IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021

单片机修改网络硬件驱动(phy芯片更换)
随机推荐
Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference
Database mysql statement final review CTGU
moco挡板制作及运行成功
问下 flink sql cdc. 能同步多张表然后sink到一份表中么 同步的表通过 joi
请问大智慧上开户安全吗
2022危险化学品经营单位主要负责人复训题库及在线模拟考试
Spark调优(提交作业资源参数调优)
Does DMS SQL result set export support parameter transfer?
Analysis of response parsing process of SAP ui5 batch request
2022A特种设备相关管理(电梯)特种作业证考试题库及在线模拟考试
Finally quit, full-time UE
Ding! Techo day Tencent technology open day arrived as scheduled!
2022年7月计划(全力unreal)
如何高效优雅地管理接口文档
9个优秀的占位图服务
想请教股票开户找人办比较方便么?网上开户安全么?
February 15, 2022 learning summary
Idea of capturing mobile terminal variant combination
Industrial digitalization and new generation digitalization system design platform -- Lecture
Node foundation ~ node level