In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Epoll is a unique Ithumb O reuse function of Linux, which is very different from select and poll in implementation and use.
Epoll uses a set of functions to accomplish tasks, rather than individual functions.
Epoll places events on file descriptors that users care about in an event table in the kernel so that there is no need to repeatedly pass in file descriptor sets or event sets for each call, as select and poll do.
But epoll needs to use an additional file descriptor to uniquely identify this event table in the kernel.
Epoll API
Epoll has three system calls: epoll_create, epoll_ctl and epoll_wait.
1.epoll_create
Epoll_create creates an additional file descriptor to uniquely identify the event table in the kernel.
1) the size parameter doesn't work right now, but just gives the kernel a hint of how big the event table needs to be.
2) the file descriptor returned by this function will be used as the first parameter of all other epoll system calls to specify the kernel event table to access.
2.epoll_ctl
The kernel event table that epoll_ctl uses to manipulate epoll.
1) the fd parameter is the file descriptor to operate on.
2) the op parameter specifies the operation type, which can be divided into three types:
EPOLL_CTL_ADD registers events on fd to the event table EPOLL_CTL_MOD modifies registered events on fd EPOLL_CTL_DEL deletes events registered on fd
3) the event parameter specifies the event, which is the epoll_event structure pointer type.
The events member describes the event type. The file types supported by epoll are basically the same as poll. The macro that represents the epoll event type is preceded by an "E" before the corresponding macro of poll. But epoll has two additional event types, EPOLLET and EPOLLONESHOT, which are critical to the efficient operation of epoll.
The data member is used to store user data, and its type epoll_data_t is defined as follows:
Epoll_data is a consortium, and the most frequently used of its four members is fd, which specifies the target file descriptor to which the event belongs.
Ptr members can be used to specify user data related to fd. But because epoll_data_t is a consortium, we cannot use both its ptr members and fd members, so if we want to associate file descriptors with user data for fast data access, we have to use other means, such as abandoning the use of epoll_data_t 's fd members and including fd in the user data that ptr points to.
Epoll_ctl returns 0 on success,-1 on failure and sets errno.
3.epoll_wait
It waits for a set of events on the file descriptor during a timeout period.
Parameters from back to front
1) the meaning of timeout parameter is the same as that of timeout parameter of poll API.
2) the maxevents parameter specifies the maximum number of events to listen to.
3) if the epoll_wait function detects events, it copies all ready events from the kernel event table (specified by the epfd parameter) into the array pointed to by its second parameter, events. This array is only used to output the ready events detected by epoll, unlike the array parameters of select and poll, which are used both to pass in user registered events and to output ready events detected by the kernel. This greatly improves the efficiency of indexing ready file descriptors in the application.
Epoll_wait returns the number of file descriptors ready on success,-1 on failure and sets errno.
LT and ET mode
There are two modes for epoll to operate on file descriptors: LT (Level Trigger) mode and ET (Edge Trigger) mode.
LT working mode is the default working mode, in which epoll is equivalent to a more efficient poll. When epoll_wait detects that there is time on it and notifies the application of this event, the application can not deal with the event immediately. In this way, the next time the application calls eoll_wait, epoll_wait also advertises the event to the application again until the event is handled.
When an EPOLLET event on a file descriptor is registered with the epoll kernel event table, epoll manipulates the file descriptor in ET mode. ET mode is the efficient working mode of epoll. When epoll_wait detects an event on it and notifies the application of the event, the application must handle the event immediately, because subsequent epoll_wait calls will no longer notify the application of the event. It can be seen that ET mode greatly reduces the number of times the same epoll event is triggered repeatedly, so it is more efficient than LT mode.
"Linux High performance Server programming"
LT supports both block and non-block socket. In this mode, the kernel tells us whether a file descriptor is ready or not, and then we can perform an I _ fd O operation on the ready file. If we don't do anything, the kernel will continue to notify us. Therefore, the possibility of programming errors in this mode is less likely, and the traditional select/poll is the representative of this pattern.
ET is a high-speed working mode, which only supports non-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, developing epoll-based applications in LT mode should be simpler 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.
Nginx defaults to using ET mode to use epoll.
Epoll ET mode Why fd must be set to non-blocking
ET (Edge trigger) data ready is notified only once, that is, if you want to use ET mode, when the data is ready, you need to read until there is an error or completion. However, if the current fd is blocked (the default), then when the data in the buffer is read, if the peer does not close the write side, then the read function will always block, affecting other fd and subsequent logic.
Series. So set fd to non-blocking, when there is no data, read can not read anything, but certainly will not be blocked, then, it means that the buffer data has been read, you need to continue to process the subsequent logic (reading other fd or wait).
Advantages of epoll:
1. Support for a process to open a large number of destination socket descriptors (fd).
The disadvantage of select is that the fd opened by a process is limited, specified by FD_SETSIZE, and the default value is 2048. It is obviously too small for IM servers that need to support tens of thousands of connections. To solve this problem, we first choose to modify the macro and then recompile the kernel, but the data also points out that this will lead to a decline in network efficiency. Second, you can choose a multi-process solution (traditional Apache solution), but although the cost of creating a process on Linux is relatively small, it can not be ignored. In addition, inter-process data synchronization is far from being less efficient than inter-thread synchronization, so it is not a perfect solution. However, epoll does not have this limit, and the upper limit of fd it supports is the maximum number of files that can be opened, which is generally much greater than 2048, which has a lot to do with system memory. For example, it is about 10w on a machine with 1GB memory, and the exact number can be seen in cat / proc/sys/fs/file-max.
The 2.I/O efficiency does not decrease with the increase of the number of fd.
Another disadvantage of traditional select/poll is that when we have a large set of socket, but because of the network delay, only part of the socket is active at any time, but each call of select/poll will linearly scan the whole set, resulting in a linear decline in efficiency. But epoll doesn't have this problem, it only operates on "active" socket-because in the kernel implementation epoll is implemented according to the callback function above each fd.
3. Use mmap to accelerate messaging between the kernel and user space.
Whether select, poll, or epoll, fd messages are notified to the user space through the kernel, and epoll is implemented through the same block of memory as the user space mmap.
Use epoll's tcp server to complete a simple HTTP message echo
Test with a browser:
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.