Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

IO Multiplexing-epoll

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Epoll is an improved poll for handling a large number of handles, and the biggest advantage over select,poll is that it does not become less efficient as the number of sturdy fd increases. Because select in the kernel is handled by polling, the greater the number of polling fd, the more time-consuming it naturally takes, and the number of slelct snooping is limited (although it can be changed through header files, it does not address the root of the problem)

I. related system calls to epoll

There are only three simple interfaces for epoll: epoll_creat,epoll_ctl,epoll_wait

(1) int epoll_creat (int size)

Create an epoll handle. When an epoll handle is created, it will occupy an FD value, so call the close () function to close it after use, otherwise fd may be exhausted.

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

Epoll's event registration function, which is different from the select function that tells the kernel what type of event to listen for when listening to events, but registers the type of event to listen for here first

The first parameter is the return value of epoll_creat

The second parameter represents the action, represented by three macros

EPOLL_CTL_ADD: register a new fd into epfd

EPOLL_CTL_MOD; modifies the listening events of the registered fd

EPOLL_CTL_DEL: removes a fd from epfd

The third parameter is the fd to be listened to

The fourth parameter tells the kernel what events to listen for

Event can be a collection of the following macros

EPOLLIN: indicates that the corresponding file descriptor can be read (including normal shutdown of socket)

EPOLLOUT: indicates that the corresponding file descriptor can be written

EPOLLPRI: indicates that the corresponding file descriptor has urgent data to read

EPOLLERR: an error occurred in the corresponding file descriptor

EPOLLET: set EPOLL to edge division mode, which is relative to horizontal trigger

EPOLLONESET: listen for events only once. If you need to listen later, you need to type this socket into the EPOLL queue again.

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

Collect the event parameter event that has occurred in the events monitored by epoll. The event parameter event is an array of allocated epoll_event structures. Epoll assigns the events that occur to the events array (events cannot be a null pointer, and the kernel is only responsible for copying data to the array, but will not open up memory for users). Maxevents tells the kernel how big this maxevents cannot be greater than the size when epoll_ctreat () is created, and states that timeout is a timeout event (milliseconds, 0 returns immediately,-1 will be uncertain, that is, permanent blocking) if the function call succeeds. Returns the number of file descriptors that have been prepared. A return of 0 indicates a timeout.

two。 How epoll works

Epoll only tells those ready file descriptors, and when you call epoll_wait () to get the ready file descriptor, instead of returning the actual file descriptor, you just need to go to an array specified by epoll to get the appropriate number of file descriptors at a time.

Another essential improvement is that epoll uses event-based readiness notification. In select/poll, the kernel scans all monitored file descriptors only after the process calls a certain method, while epoll first registers a file descriptor with epoll_ctl () and is notified when the process calls epoll_wait ().

three。 How epoll works-horizontal trigger (LT)-Edge trigger (ET)

LT (level triggered) is the default way for epoll to work, and supports both block and no-block socket. In this practice, the kernel tells you whether a file descriptor is ready, and then you can IO the ready fd. If you don't do anything, the kernel will continue to notify you, so programming errors in this mode are less likely. The traditional select/poll is the representative of this model.

ET (edge-triggered) is a high-speed working mode, which only supports no-block socket, and it is more efficient than LT. The difference between ET and LT is that when a new event arrives, of course, the event can be obtained from the epoll_wait call in ET mode, but if the socket buffer corresponding to the event is not processed this time, when no new event arrives in the socket, it is impossible to retrieve the event from the epoll_wait call again in ET mode. In LT mode, by contrast, as long as there is data in the socket buffer corresponding to an event, the event can always be obtained from epoll_wait.

Therefore, the development of epoll-based applications in LT mode is easier and less error-prone. When an event occurs in ET mode, if the buffer data is not thoroughly processed, the user request in the buffer will not be responded to.

IV. The advantages of epoll

(1) support a process to open a large number of destination socket descriptors (FD)

The number of fd that select can open is limited, generally 1024. For servers that support tens of thousands of links, epoll does not have this limit. The upper limit of FD it supports is the maximum number of files that can be opened. This tree seed is generally greater than 2048.

(2) IO efficiency does not decrease linearly with the increase of the number of FD.

The Achilles' heel of traditional select/poll is that when it has a large set, it polls the scan set every time, resulting in a decline in efficiency, but epoll only operates on active socket-- because in the kernel epoll is implemented according to the callback function above each fd, and only active functions call callback functions.

5. How to handle millions of handles by epoll under Linux

(1) first call epoll_creat to create an epoll object. The parameter size is the maximum number of handles that the kernel can handle correctly.

(2) epoll_ctl operates the epoll established above, such as adding the newly created socket to the epoll for monitoring, or removing epoll from a socke handle that epoll is monitoring.

(3) epoll_wait returns the user-mode process within a given timeout time when all the monitored handles send an audit change (millisecond, 0 returns immediately,-1 will be uncertain, that is, permanent blocking). If the function call is successful, it returns the number of file descriptors that have been prepared. If 0 is returned, it will time out.

From the above call mode, we can see that epoll is superior to select/poll: because the latter will return all the socket you want to monitor to the select/poll system call each time it is called, which means that you need to copy the socket list in user mode to kernel state. If tens of thousands of handles result in copy hundreds of KB of memory to kernel state each time, it is very inefficient, but epoll_wait does not have to pass socket handles to the kernel. Because the kernel has already got the list of handles to monitor in epoll_ctl

Here is the code section

1. Create a listening socket, set the port to take 127.0.0.1, specify the cluster as IPv4, specify the port number and convert it to network byte sequence, specify the ip address and convert it to network byte sequence.

two。 Call epoll_creat to get an epoll model, and judge that if the creation is successful, call the epoll_ctl function to register the type of event to listen for. The listening event type set in this code is EPOLLIN: indicates that the corresponding file descriptor can be read.

Epoll's event registration function, which is different from the select function that tells the kernel what type of event to listen for when listening to events, but registers the type of event to listen for here first

The first parameter is the return value of epoll_creat

The second parameter represents the action, represented by three macros

EPOLL_CTL_ADD: register a new fd into epfd

EPOLL_CTL_MOD; modifies the listening events of the registered fd

EPOLL_CTL_DEL: removes a fd from epfd

The third parameter is the fd to be listened to

The fourth parameter tells the kernel what events to listen for

Event can be a collection of the following macros

EPOLLIN: indicates that the corresponding file descriptor can be read (including normal shutdown of socket)

EPOLLOUT: indicates that the corresponding file descriptor can be written

EPOLLPRI: indicates that the corresponding file descriptor has urgent data to read

EPOLLERR: an error occurred in the corresponding file descriptor

EPOLLET: set EPOLL to edge division mode, which is relative to horizontal trigger

EPOLLONESET: listen for events only once. If you need to listen later, you need to type this socket into the EPOLL queue again.

3. Call the epoll_wait function to collect the events that have occurred in the events monitored by epoll. The parameter event is an array of allocated epoll_event structures. Epoll assigns events to the events array (events cannot be a null pointer, and the kernel is only responsible for copying data to the array, but will not open up memory for users). Maxevents tells the kernel how big the value of this maxevents cannot be greater than the size when epoll_ctreat () is created.

Explain that timeout is a timeout event (millisecond, 0 returns immediately,-1 will be uncertain, that is, permanent blocking). If the function call is successful, the number of file descriptors that have been prepared is returned. If 0 is returned, the timeout is indicated.

Determine whether it is a listening socket, receive it if it is and ready, and read it in the way of edge transfer, and if other sockets, receive it the same way.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report