当前位置:网站首页>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.
边栏推荐
- halcon知识:矩阵专题【01】
- GCC getting started manual
- Database mysql statement final review CTGU
- Can data sources only be connected to Alibaba cloud cloud databases? Can't you connect the databases installed in Alibaba cloud servers?
- kubernetes可视化界面dashboard
- 2022年化工自动化控制仪表考试模拟100题模拟考试平台操作
- 终于辞职了,全职ue了
- Finally quit, full-time UE
- Mycat+分库分表
- dataworks SQL脚本支持语句块的if else 判断吗
猜你喜欢

2022年化工自动化控制仪表考试模拟100题模拟考试平台操作

图形系统——1. 布局加载

杂记:数据库go,begin,end,for,after,instead of

nuc980心跳灯

2022 review questions and answers for safety production management personnel of hazardous chemical production units

Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference

Applet graduation design based on wechat gym private education appointment applet graduation design opening report function reference

架构自治服务:构建数据驱动的架构洞察

如何从RHEL 8升级到RHEL 9

面部识别试验涉及隐私安全问题?国外一公司被紧急叫停
随机推荐
oracle cdc 但是使用的服务名没有sid 该怎么配置呢?
CANN媒体数据处理V2,JPEGD接口介绍
NFT流动性协议的安全困局—NFT借贷协议XCarnival被黑事件分析
Unity about oculus quest2 developing 002-ui interaction based on XR interaction Toolkit
Pure big resentment! Those who were discouraged from taking the postgraduate entrance examination
Ask flynk SQL cdc Can you synchronize multiple tables and then sink them into one table? Synchronized tables can be accessed through a joi
请问大智慧上开户安全吗
nuc980心跳灯
golang中的select详解(转)
GCC getting started manual
How to create a CSR (certificate signing request) file?
如何使用 SAP CDS view 中的 currency conversion 功能
Small program graduation design based on wechat real estate intermediary house viewing appointment small program graduation design opening report function reference
Redis 原理 - Hash
Unity about oculus quest2 basic development based on XR interaction toolkit 003- capture function - making a VR bowling game
How to configure the Oracle CDC service name without Sid?
Can data sources only be connected to Alibaba cloud cloud databases? Can't you connect the databases installed in Alibaba cloud servers?
Industrial digitalization and new generation digitalization system design platform -- Lecture
Dnslog injection
Small program graduation project based on wechat campus lost and found graduation project opening report function reference