当前位置:网站首页>select/poll/epoll
select/poll/epoll
2022-06-28 18:25:00 【MrPeng1991】
1. IO series ——io A preliminary study of the model
2.IO series ——select/poll/epoll
select/poll/epoll All are IO Multiplexing mechanism , Multiple descriptors can be monitored at the same time , When a descriptor is ready ( Read or write ready ), Then immediately inform the corresponding program to read or write . Essentially select/poll/epoll It's all synchronous I/O, That is, reading and writing are blocked .
1. select
int select (int maxfd, fd_set *readfds,
fd_set *writefds,
fd_set *exceptfds,
struct timeval *timeout);
- maxfd: Represents the largest file descriptor to monitor fd+1
- writefds: Monitor writable fd
- readfds: Monitor readable fd
- exceptfds: Abnormal monitoring fd
- timeout: Time out period
- NULL, Means no timeout is set , Will be blocked until the event on the file descriptor triggers
- 0, It means not to wait , Return immediately , Used to detect file descriptor status
- Positive integer , Represents when no event is triggered at the specified time , Returns a timeout
select Function monitoring 3 Class file descriptor , call select Function will block , Until the descriptor fd Be ready ( Data readable 、 Can write 、 abnormal ) Or a timeout , The function returns . When select When the function returns , By traversing the descriptor set , Find ready descriptor .
select shortcoming
- The number of file descriptors is limited : There is a maximum limit on the number of file descriptors that a single process can monitor , stay Linux Generally speaking 1024, You can increase the upper limit by modifying the macro definition , But there are also weaknesses of low efficiency ;
- Serious performance degradation :IO As the number of monitored descriptors increases , Its performance will decrease linearly ;
2.poll
int poll (struct pollfd *fds, unsigned int nfds, int timeout);
//pollfd Represents a collection of monitored descriptors , as follows
struct pollfd {
int fd; // File descriptor
short events; // Monitored request events
short revents; // What has happened
};
pollfd The structure contains the event And what happened event, also pollfd There is no maximum quantity limit . and select The function is the same , When poll When the function returns , By traversing the descriptor set , Find ready descriptor .
poll shortcoming :
From above select and poll All need to be back , Get the ready... By traversing the file descriptor socket. A large number of clients connected at the same time may have only a few ready at the same time , So as the number of descriptors monitored grows , Its performance will decrease linearly .
3 epoll
epoll It's in the kernel 2.6 Proposed in , yes select and poll Enhanced Edition . be relative to select and poll Come on ,epoll More flexible , There is no limit on the number of descriptors .epoll Use one file descriptor to manage multiple descriptors , Store the events of the user space file descriptor in an event table of the kernel , So in user space and kernel space copy Just once. .epoll The mechanism is Linux The most efficient I/O Reuse mechanism , Waiting for multiple file handles at one place I/O event .
select/poll There is only one way ,epoll The operation process includes 3 A way , Namely epoll_create(), epoll_ctl(),epoll_wait()
3.1 epoll_create
Used to create a epoll The handle of ,size Refers to the number of monitored descriptors , The kernel now supports dynamic expansion , The meaning of this value is only the initial distribution fd Number , When there is not enough space in the back, the capacity will be expanded dynamically . When the creation is finished epoll After handle , Take up one fd value
ls /proc/<pid>/fd/ // It can be executed through the terminal , See this fd
Use up epoll after , Must call close() close , Otherwise, it may lead to fd Exhausted .
3.2 epoll_ctl
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
Used for the file descriptor that needs to listen (fd) perform op operation , For example, will fd Add to epoll Handle .
epfd: yes epoll_create() The return value of ;
op: Express op operation , Use three macros to represent , Respectively represent adding 、 Delete and modify the right fd The monitoring event of ;
- EPOLL_CTL_ADD( add to )
- EPOLL_CTL_DEL( Delete )
- EPOLL_CTL_MOD( modify )
fd: File descriptors to listen for ;
epoll_event: Events that need to be monitored ,struct epoll_event The structure is as follows :
struct epoll_event {
__uint32_t events; /* Epoll event */
epoll_data_t data; /* User available data */
};
events Value for :( Indicates the operation of the corresponding file descriptor )
- EPOLLIN : Can be read ( Including the end SOCKET Normally shut down );
- EPOLLOUT: Can write ;
- EPOLLERR: error ;
- EPOLLHUP: interrupt ;
- EPOLLPRI: High priority readability ( This should indicate that there is out-of-band data coming in );
- EPOLLET: take EPOLL Set to edge trigger mode , This is relative to horizontal triggering .
- EPOLLONESHOT: Listen for only one event , After listening to this event, the supervisor will no longer listen to this event
3.3 epoll_wait
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);
Wait for the event to be reported
- epfd: wait for epfd Upper io event , Back at most maxevents Events ;
- events: The collection used to get events from the kernel ;
- maxevents:events Number , The maxevents Value cannot be greater than create epoll_create() At the time of the size;
- timeout: Timeout time ( millisecond ,0 Will return immediately ).
This function returns the number of events to be processed , Such as return 0 Indicates that time has expired .
4. contrast
stay select/poll in , The process only calls a certain method after , The kernel scans all monitored file descriptors , and epoll Pass in advance epoll_ctl() To register a file descriptor , Once a file descriptor is ready , The kernel will use something like callback The callback mechanism of , Quickly activate the file descriptor , When the process calls epoll_wait() I'll be informed when .( The traversal file descriptor is removed here , But through the mechanism of monitoring callback . That's exactly what it is. epoll The charm of .)
epoll advantage
- There is no limit to the number of descriptors monitored , Supported by the FD The upper limit is the maximum number of files that can be opened , The specific number can be
cat /proc/sys/fs/file-maxsee , In general, this number has a lot to do with system memory , With 3G For your phone, this value is 20-30 ten thousand . - IO Performance does not change with monitoring fd The number of decreased with the increase of .epoll differ select and poll Polling method , But through each fd The callback function defined to implement , Only ready fd To execute the callback function .
If there are not a lot of idle or dead connections ,epoll It's not as efficient as select/poll Much higher . But when a large number of idle connections are encountered ,epoll The efficiency is much higher than select/poll.
边栏推荐
- How to configure the Oracle CDC service name without Sid?
- konva系列教程3:自定义图形
- 2022a special equipment related management (elevator) special operation certificate examination question bank and online simulation examination
- 2022A特种设备相关管理(电梯)特种作业证考试题库及在线模拟考试
- HTNL introduction
- NP tips: random create random matrix sample = np random. random([19, 64 , 64, 3])
- kubernetes可视化界面dashboard
- Industrial digitalization and new generation digitalization system design platform -- Lecture
- halcon知识:矩阵专题【01】
- 堆的概念和代码实现
猜你喜欢

Google launches advanced API security to protect APIs from security threats

Node foundation ~ node level

Applet graduation project reservation based on wechat housekeeping service applet graduation project opening report function reference

Redis 原理 - Hash

NFT流动性协议的安全困局—NFT借贷协议XCarnival被黑事件分析

io模型初探

Go, begin, end, for, after, instead of

数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”

How to design a business high performance and high availability computing architecture - job

为什么 insert 配置 'SELECT LAST_INSERT_ID()' 返回个0呢?
随机推荐
【译】clickhouse 22.4和22.5核心特性一览
Unity about oculus quest2 basic development based on XR interaction toolkit 003- capture function - making a VR bowling game
工业数字化与新一代数字化系统设计平台----讲座
Flutter tips: mediaquery and build optimization secrets you don't know
Go, begin, end, for, after, instead of
Mycat+分库分表
GCC getting started manual
HackTheBox-baby CachedView
2022 practice questions and mock examination of Shandong Province safety officer C certificate examination
通达信能开户炒股吗?安全吗?
【云驻共创】昇腾异构计算架构CANN,助力释放硬件澎湃算力
How to create a CSR (certificate signing request) file?
如何设计业务高性能高可用计算架构 - 作业
Applet graduation design based on wechat beauty salon technician appointment applet graduation design opening report function reference
Spruce network deepflow helps 5g core network and telecom cloud build observability
如何使用 SAP CDS view 中的 currency conversion 功能
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
Common DOS commands
Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference
全力冲unreal了