linux网络编程系列-select和epoll的区别

select和epoll属于I/O多路复用模型,用于持续监听多个socket,获取其IO事件。

select(轮询)

该模型轮询各socket,不管socket是否活跃,随着socket数的增加,性能逐渐下降。

 

#include <sys/select.h>
#include <sys/time.h>
int select (int maxfdpl, fd_set* readset, fd_set* writeset, fd_set* exceptset, const struct timeval* timeout)

调用时轮询一次所有描述字,超时时再轮询一次。如果没有描述字准备好,则返回0;中途错误返回-1;有描述字准备好,则将其对应位置为1,其他描述字置为0,返回准备好的描述字个数。

fd_set:整数数组,每个数中的每一位对应一个描述字,其具体大小有内核的FD_SETSIZE决定,默认值为1024。

void FD_ZERO(fd_set* fdset): clear all bits in fdset
void FD_SET(int fd, fd_set* fdset):turn on the bit for fd in fdset
void FD_CLR(int fd, fd_set* fdset):turn off the bit for fd in fdset
int FD_ISSET(int fd, fd_set* fdset):is the bit for fd on in fdset

 

epoll(触发)

epoll采用了中断注册回调的方式,socket IO就绪时发出中断,然后将socket加入就绪队列。由三个系统调用:epoll_create,epoll_ctl,epoll_wait。

能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率:它会复用文件描述符集合来传递结果,不需要每次等待事件之前都重新准备要被侦听的文件描述符集合;获取事件的时候,它无须遍历整个被侦听的描述符集,只要遍历那些被内核IO事件异步唤醒而加入Ready队列的描述符集合。

select/epoll都需要在内核和用户空间之间复制数据,epoll使用了内存映射(mmap)技术,将内核和用户空间指向同一块内存。

系列函数:

创建函数

创建一个epoll句柄,size-监听套接字的数。当创建好epoll句柄后,会占用一个fd值,所以在使用完epoll后,必须调用close()关闭,否则可能导致fd被耗尽。

 

#include <sys/epoll.h>
int epoll_create(int size)

 

事件注册函数

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)

第一个参数是epoll_create()的返回值;
第二个参数表示动作,用三个宏来表示:
EPOLL_CTL_ADD:注册新的fd到epfd中,
EPOLL_CTL_MOD:修改已经注册的fd的监听事件,
EPOLL_CTL_DEL:从epfd中删除一个fd;
第三个参数是需要监听的fd;
第四个参数是告诉内核监听事件,struct epoll_event结构如下:

typedef union epoll_data { 
void *ptr; 
int fd; 
__uint32_t u32; 
__uint64_t u64; 
} epoll_data_t; struct epoll_event { 
__uint32_t events; /* Epoll events */ 
epoll_data_t data; /* User data variable */ 
};


events可以是以下几个宏的集合:
EPOLLIN :表示对应的文件描述符可以读(包括对端SOCKET正常关闭);
EPOLLOUT:表示对应的文件描述符可以写;
EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);
EPOLLERR:表示对应的文件描述符发生错误;
EPOLLHUP:表示对应的文件描述符被挂断;
EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。ET只有新事件到来时,epoll_wait才能获得通知,即使缓冲区中还有数据,epoll_wait也无法再获得改描述符的事件;LT只要对应的缓冲区中还有数据,epoll_wait就可以获得该描述符对应的事件。
EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把这个socket加入到EPOLL队列里


使用如下:

 

struct epoll_event ev; 
ev.data.fd=listenfd;
ev.events=EPOLLIN|EPOLLET; 
epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev); 


等待函数

 

int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout)


等待事件的产生,参数events用来从内核得到事件集合。maxevents告之内核这个events有多大,这个maxevents的值不能大于创建epoll_create()时的size,参数timeout是超时时间(毫秒,0会立即返回,-1永久阻塞)。该函数返回需要处理的事件数目,如返回0表示已超时。
注意:某fd上发生事件后,其事件类型会被清空,所以如果下一个循环还要关注这个socket fd的话,则需要用epoll_ctl(epfd,EPOLL_CTL_MOD,listenfd,&ev)来重新设置socket fd的事件类型。这时不用EPOLL_CTL_ADD,因为socket fd并未清空,只是事件类型清空。这一步非常重要。


实例

 

 struct epoll_event ev, *events;for(;;) {nfds = epoll_wait(kdpfd, events, maxevents, -1);for(n = 0; n < nfds; ++n) {if(events[n].data.fd == listener) {client = accept(listener, (struct sockaddr *) &local,&addrlen);if(client < 0){perror("accept");continue;}setnonblocking(client);ev.events = EPOLLIN | EPOLLET;ev.data.fd = client;if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, client, &ev) < 0) {fprintf(stderr, "epoll set insertion error: fd=%d\n",client);return -1;}} else {do_use_fd(events[n].data.fd);}}
}

对比:
select - 如果同时建立很多连接,但只有少数事件发生,这种轮询会造成效率很低;频繁从内核拷贝、复制描述字;监听描述字受限于内核的FD_SETSIZE;
epoll - 这种回调触发式操作会保证效率;不需要频繁的拷贝;监听描述字没有限止,只与系统资源有关;epoll返回时已经明确的知道哪个sokcet fd发生了事件,不用再一个个比对。

 

转载于:https://www.cnblogs.com/whuqin/archive/2013/05/09/4982011.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274255.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

产品原型制作_早期制作原型如何帮助您设计出色的数字产品

产品原型制作Utilizing prototypes this way, is a missed shot. A missed shot to create an outcome that solves a real problem for customers. An outcome that is worthwhile. Prototypes are a great tool to discover what people need, what they dream of, what thei…

ThinkPHP add、save无法添加、修改不起作用

ThinkPHP add、save无法添加、修改不起作用 案例&#xff1a;数据库新添加一字段&#xff0c;修改值不成功。解决方案&#xff1a;将Runtime/Data/_fields/下面的字段缓存删除&#xff0c;或者直接删除整个Runtime文件也是可以的分析&#xff1a;由于Thinkphp&#xff0c;采用字…

photoshop最新版本_iPad Pro应该拥有更好的Photoshop版本

photoshop最新版本I remember when Adobe came to an Apple Keynote in 2018 to show how powerful was Photoshop on the new iPad Pros.我记得Adobe在2018年参加Apple Keynote时展示了Photoshop在新iPad Pro上的强大功能。 In fact, like everyone else, I was blown away, …

android 辅助功能_关于辅助功能的9个神话

android 辅助功能Most designers don’t know about accessibility or have misconceptions about it, such as thinking it will hinder their creativity or that it doesn’t apply to their clients. Find out 9 myths about accessibility and why you and your clients s…

【语言处理与Python】1.5自动理解自然语言

【词义消歧】在词义消歧中&#xff0c;我们要算出特定上下文中词被赋予的是哪个意思。自动消除歧义需要使用上下文&#xff0c;利用相邻词汇有相近含义这样一个简单的事实。【指代消解】解决“谁对谁做了什么”&#xff0c;即监测主语和动词的宾语。确定带刺或名词短语指的是什…

绊倒在舌头上

It’s not something you’d confess in tenth grade, but I’ve always been fascinated by typography. By crisp, pared down symbols in clear white space, the infinite variety of lines, ligatures and curves that make up the letters of language. Even the sample …

[开源]jquery.ellipsis根据宽度(不是字数)进行内容截断,支持多行内容

jquery.ellipsis 自动计算内容宽度&#xff08;不是字数&#xff09;截断&#xff0c;并加上省略号&#xff0c;内容不受中英文或符号限制。 如果根据字数来计算的话&#xff0c;因为不同字符的宽度并不相同&#xff0c;比如l和W&#xff0c;特别是中英文&#xff0c;最终内容宽…

前端开发时间格式的转换方法_开发人员投资时间而不浪费时间的10种方法

前端开发时间格式的转换方法In today’s, in the past and probably in the future world — the time is more valuable than money, and the right time waits for no one. Hence, we have to make the most out of it to succeed in life.在当今&#xff0c;过去甚至未来世界…

链表基本操作

单链表结构&#xff1a; typedef struct node { int data; struct node *next; }node&#xff1b; typedef struct node *LinkList; /*创建单链表&#xff0c;将新的节点插入到链表的尾部*/ createList(LinkList L, int n) { LinkList p,r;  //p节点用来接收插入的元素&#…

python 投资组合_重新设计投资组合的好处

python 投资组合Yep, I’m here to nag you a bit about that portfolio that you haven’t updated in a while.是的&#xff0c;我在这里想和您谈谈您有一段时间没有更新的作品集。 Yes, it’s time to get to work on it again.是的 &#xff0c;是时候重新开始研究了。 Y…

李安的电影梦by李安

1978年,当我准备报考美国伊利诺大学的戏剧电影系时&#xff0c;父亲十分反感&#xff0c;他给我举了一个数字&#xff1a;在美国百老汇&#xff0c;每年只有200个角&#xff0c;但却有50000人要一起争夺这少得可怜的角色。当时我一意孤行&#xff0c;决意登上了去美国的班机&am…

抓取html中用到的css_如何使用HTML和CSS制作像《星球大战》一样的抓取文字

抓取html中用到的cssThe opening to Star Wars is iconic. The effect of text scrolling both up and away from the screen was both a crazy cool special effect for a movie back in 1977 and a cool typographical style that was brand new at the time.《星球大战》的开…

不安装游戏apk直接启动法

原文地址&#xff1a;http://blog.zhourunsheng.com/2011/09/%E6%8E%A2%E7%A7%98%E8%85%BE%E8%AE%AFandroid%E6%89%8B%E6%9C%BA%E6%B8%B8%E6%88%8F%E5%B9%B3%E5%8F%B0%E4%B9%8B%E4%B8%8D%E5%AE%89%E8%A3%85%E6%B8%B8%E6%88%8Fapk%E7%9B%B4%E6%8E%A5%E5%90%AF%E5%8A%A8%E6%B3%95…

web字体设置成平方字体_Web字体正确完成

web字体设置成平方字体When using web fonts we must carefully avoid its hidden pitfalls. To do this designers, frontend developers, DevOps, and authors each have a role to play in creating a great end-user experience.使用网络字体时&#xff0c;必须小心避免其隐…

Android客户端打包方案分享

基本介绍 Android应用的自动化打包是应用持续集成以及多渠道发布的基础。当前Android客户端自动化打包的主要有两种方式&#xff0c;Ant和Maven。两种方式本质上都是调用Android SDK里面提供的工具&#xff0c;不过各自有各自的特点。 1. Ant脚本 好处&#xff1a;开发成本较低…

sql注入修复方法是_旧的方法是修复我们可以看到的内容。

sql注入修复方法是When envisioning the futurestate of a company or a service, we’re usually faced with the challenge of designing for a customer that doesn’t exist yet. What do we mean by this? Well, they exist in the obvious sense, they’re just not ‘t…

library听证会_听证会

library听证会My Initial Experience with a Screen Reader我对屏幕阅读器的初步体验 As a new web developer, I have been learning to make my sites visually appealing but still be accessible, and all-in-all, it’s been going decent. I’ve been including cool ic…

jsoup测试例子

1、测试代码 import java.io.File; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class Test { public static void main(String[] args) { Test t new Test(); t.parseUrl(); } publ…

ux和ui_他们说,以UX / UI设计师的身份加入一家初创公司。 他们说,这会很有趣。

ux和uiSure, working in a startup environment sounds fun. The stories of flexibility and freedom that it entails spark curiosity into people’s minds, making it enticing to explore a career in the startup scene. In reality, working in a startup just present…

程序员在囧途

程序员在囧途&#xff1a;http://www.shenyisyn.org/2013/05/21/cxyzhc.htm转载于:https://www.cnblogs.com/Qiaoyq/archive/2013/05/22/3092904.html