epoll

高并发的epoll+线程池,业务在线程池内-ehyyngp-ChinaUnix博客

只愿长相守 提交于 2020-01-24 04:45:30
高并发的epoll+线程池,业务在线程池内-ehyyngp-ChinaUnix博客 高并发的epoll+线程池,业务在线程池内 2011-08-29 21:48:49 分类: C/C++ epoll是linux下高并发服务器的完美方案,因为是基于事件触发的,所以比select快的不只是一个数量级。 单线程epoll,触发量可达到15000,但是加上业务后,因为大多数业务都与数据库打交道,所以就会存在阻塞的情况,这个时候就必须用多线程来提速。 业务在线程池内,这里要加锁才行。测试结果2300个/s 来源: https://www.cnblogs.com/lexus/archive/2013/03/26/2983543.html

Why do we need EPOLLRDHUP when EPOLLHUP seems enough?

我的未来我决定 提交于 2020-01-24 03:31:11
问题 According to the linux man page, EPOLLHUP When reading from a channel such as a pipe or a stream socket, this event merely indicates that the peer closed its end of the channel. EPOLLRDHUP Stream socket peer closed connection, or shut down writing half of connection. I can hardly tell any difference between EPOLLHUP and EPOLLRDHUP . To me, whenever EPOLLRDHUP is used EPOLLHUP can be used instead with the same semantics. Am I right? If not, any explanations? 来源: https://stackoverflow.com

IO模型——IO多路复用机制

孤街醉人 提交于 2020-01-24 02:49:31
(1)I/O多路复用技术通过把多个I/O的阻塞复用到同一个select、poll或epoll的阻塞上,从而使得系统在单线程的情况下可以同时处理多个客户端请求。与传统的多线程/多进程模型比,I/O多路复用的最大优势是系统开销小,系统不需要创建新的额外进程或者线程。 (2)select,poll,epoll本质上都是同步I/O,因为他们都需要在读写事件就绪后自己负责进行读写,也就是说这个读写过程是阻塞的,而异步I/O则无需自己负责进行读写,异步I/O的实现会负责把数据从内核拷贝到用户空间。 (3)I/O多路复用的主要应用场景如下: 服务器 需要同时处理多个处于监听状态或者多个连接状态的套接字; 服务器需要同时处理多种网络协议的套接字; (4)目前支持I/O多路复用的系统调用有 select,poll,epoll,epoll与select的原理比较类似,但epoll作了很多重大改进,现总结如下: ①支持一个进程打开的文件句柄FD个数不受限制(为什么select的句柄数量受限制:select使用位域的方式来传递关心的文件描述符,因为位域就有最大长度,在Linux下是1024,所以有数量限制); ②I/O效率不会随着FD数目的增加而线性下降; ③epoll的API更加简单; 来源: https://www.cnblogs.com/sea-stream/p/10563955.html

day34-2 多路复用之epoll

。_饼干妹妹 提交于 2020-01-19 07:54:28
epoll select存在的问题 select,需要遍历socket列表,频繁的对等待队列进行添加移除操作 数据到达后还需要遍历所有的socket才能获知哪些socket有数据 两个操作消耗的时间随着要监控的socket的数量增加而大大增加,所以最大只能监视1024个socket。 于是就推出了epoll,epoll对问题一方案是把等待队列的维护与阻塞进程这两个操作分开了 对问题2是自己维护了一个等待队列,避免了遍历所有的socket。需要注意:epoll仅能在linux中使用 epoll相关函数 import select # 导入select模块 epoll = select.epoll() # 创建一个epoll对象 epoll.register(文件句柄,事件类型) # 注册要监视的文件句柄和事件 # 事件类型: select.EPOLLIN # 可读事件 select.EPOLLOUT # 可写事件 select.EPOLLERR # 错误事件 select.EPOLLHUP # 客户端断开事件 epoll.unregister(文件句柄) # 销毁文件句柄 epoll.poll(timeout) # 当文件句柄发生变化,则会以列表的形式主动报告给用户进程,timeout为超时时间,默认为-1,即一直等待直到文件句柄发生变化,如果指定为1

epoll 模板

一笑奈何 提交于 2020-01-18 08:10:45
select 的问题: 1.当进程被唤醒不清楚到底哪个socket有数据,只能遍历一遍 2.每一次select的执行,都需要将这进程,再加入到等待队列中 ​ 为了防止重复添加等待队列,当某一次操作完成时,也必须从等待队列中删除进程 所以select最大限制被设置为了1024 ,如此看来select连多线程都比不上 于是推出了poll 和 epoll poll只是简单对select进行了优化,但是还不够完美 ,epoll才是最后的解决方案 注意:epoll仅能在linux中使用 案例: import socket import select s = socket.socket() s.bind(("127.0.0.1",1689)) s.listen() # 创建一个epoll对象 epoll = select.epoll() # 注册读就绪事件 (有数据可以读取了) # s.fileno()用于获取文件描述符 epoll.register(s.fileno(),select.EPOLLIN) # 存储文件描述符与socket的对应关系 fd_sockets = {s.fileno():s} while True: # 该函数是阻塞会直到你关注的事件发生 # 返回值为文件描述符与发生的事件类型 是一个列表 列表中是元组 第一个是描述符 第二个是事件 for fd,event in

受”误解“的Java AIO

天大地大妈咪最大 提交于 2020-01-18 00:01:16
为什么说 AIO 受”误解“,虽然这个”误解“被打上了双引号,但还是不得不承认它的发展状况并不好。AIO 是 Java 7 开始提供的新特性,而这个”新特性“到如今都成了陈年老酒还鲜有人去品味它。要知道 Java 7 可是在 2011年7月份发布的,市面上基于 AIO 实现的通信框架竟然寥寥无几,关于这项技术的介绍文章也普遍比较粗略。通过阅读那些介绍 AIO 的文章,似乎从学术层面大家就不怎么待见这项技术。 作为 AIO 的学习者、受益者,我觉得有必要先对网上的一些 ”偏见“ 表达一下自己的观点。如果能有幸在认知上搭成共识,之后的学习交流会更加顺畅一点。通常偏见源于比较,AIO 与 BIO、NIO 的对比明细如表所示。 误解一 通过上表的比较可以看出 AIO 的性价比应该是优于 NIO 的,而实际情况却是大多数人更偏爱与 NIO,准确的说应该是偏爱 NIO 通信框架:Netty。这本无可厚非,Netty 确实是一款非常优秀的项目,可是很多人错误的解读了 Netty 在 Github 上关于不支持 AIO 的理由,这更加遏制了 AIO 的发展。 Not faster than NIO (epoll) on unix systems (which is true) 这句话表达的本意应该是:NIO 和 AIO 在 unix 系统上使用的都是 epoll 模式,本质都是一样的。但 Not

How to pass a callback function pointer to epoll_event structure in C++

被刻印的时光 ゝ 提交于 2020-01-16 18:12:06
问题 I have been searching an answer for this question and I came across the functions timerfd_create and epoll in Linux. In a tutorial it was said that epoll_ctl() has an epoll_data_t union member of the epoll_event structure which can be used to execute a callback function on a timerfd event firing. But I am not sure on how to do it. Can anyone please help. 回答1: You can't put a callback function pointer into epoll_event because it can't fit in any of these slots: typedef union epoll_data { void

Proper handling of EWOULDBLOCK with polling on a non-blocking socket

南楼画角 提交于 2020-01-14 09:42:08
问题 I've been working on a polling TCP daemon for some time now. Recently, I've read that non-blocking sockets can sometimes throw an EWOULDBLOCK error during a send() or recv(). My understanding is that if recv() throws an EWOULDBLOCK, this (usually) means that there's nothing to receive. But what I'm unclear on is under what circumstances send() would throw an EWOULDBLOCK, and what would be proper procedure for handling such an event? If send() throws an EWOULDBLOCK, should the daemon simply

How to use the epoll/kqueue enabled version of GHC/Haskell for network connections?

坚强是说给别人听的谎言 提交于 2020-01-14 04:24:08
问题 There is a lot of old information on the net regarding an epoll/kqueue enabled GHC. For example, the code on the Simple Servers wiki page doesn't compile anymore. Could someone provide a basic example of how to use this feature with a modern GHC version to build, e.g. a TCP server that just responds with "Hello" on connect? 回答1: GHC's IO manager uses epoll/kqueue under the hood without any special programmer effort. Just write the naive threaded program -- that puts each concurrent blocking

How to code an epoll based sockets client in C

只愿长相守 提交于 2020-01-14 04:15:11
问题 All the examples I can find online are servers. I want to build a basic web crawler using epoll. So I need a basic client example to get me started. When I say basic I really mean a complete example that demonstrates multiple connections with sending and receiving of data to live web hosts. A simple HEAD request and its response for example. 回答1: Here is a sample c code for client socket with epoll. #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <sys