Epoll and select differences

Source: Internet
Author: User

First of all, the framework of this article, first of all, then summarize the differences between the two mechanisms and relations, and finally introduce the use of each interface

First, the question leads to the contact difference

The problem arises, when the need to read more than two I/O, if the use of blocking I/O, it may be blocked for a long time on a descriptor, the other descriptor although there is data but can not be read out, so that real-time can not meet the requirements, the approximate solution has the following:

1. Use multi-process or multi-threading, but this approach can complicate the program, and it also requires a lot of overhead for creating and maintaining processes and threads. (Apache server is the way in which child processes are used, advantages can isolate users)

2. Using a process, but using non-blocking I/O to read the data, when an I/O unreadable when the return immediately, check whether the next is readable, this form of circular polling (polling), this method is more wasteful CPU time, because most of the time is not readable, However, it still takes time to repeatedly execute the read system call.

3. Asynchronous I/O (asynchronous I/O), when a descriptor is ready to use a signal to tell the process, but because the number of signals is limited, multiple descriptors are not applicable.

4. A better way for I/O multiplexing (I/O multiplexing) (seemingly also translated multiplexing), first constructs a list of descriptors (Epoll in the queue), and then calls a function until one of these descriptors is ready to return, returning to tell the process which i/ O ready. The two mechanisms of select and Epoll are multi-path I/O mechanism solutions, select is the POSIX standard, and Epoll is unique to Linux.

The difference (epoll relative to the Select advantage) is mainly three:

The number of handles in the 1.select is limited, and the Linux/posix_types.h header file has such a declaration: The #define __FD_SETSIZE 1024 indicates that select listens for up to 1024 FD at a time. While Epoll does not, its limit is the maximum number of open file handles.

The biggest benefit of the 2.epoll is that it does not decrease efficiency with the number of FD, polling processing in Selec, where the data structure is similar to the data structure of an array, while Epoll is maintaining a queue and looking directly at whether the queue is empty. Epoll only operates on "active" sockets---This is because Epoll is implemented in the kernel implementation based on the callback function above each FD. Then, only the "active" socket will be active to call the callback function (put this handle into the queue), the other idle state handle will not, at this point, Epoll implemented a "pseudo" AIO. However, if the majority of I/O are "active" and the usage of each I/O port is high, the epoll efficiency is not necessarily higher than select (possibly to maintain the queue complexity).

3. Use Mmap to accelerate message delivery between the kernel and user space. Both Select,poll and epoll need the kernel to inform the user of the FD message, how to avoid unnecessary memory copy is very important, at this point, epoll through the kernel in the user space mmap the same piece of memory implementation.

Second, the interface

1) Select

1. int select (int maxfdp1, fd_set *restrict Readfds, Fd_set *restrict Writefds, fd_set *restrict Exceptfds, struct timeval *restrict tvptr);

struct timeval{

Long tv_sec;

Long tv_usec;

}

There are three situations: Tvptr = = NULL Waits forever, tvptr->tv_sec = = 0 && Tvptr->tv_usec = = 0 does not wait at all, not equal to 0 of the time waiting. The three pointers of select can be empty, when select provides a more precise timer than sleep. Note that the first parameter of select MAXFDP1 is not the number of descriptors, but the maximum description multibyte 1, one is to limit the role, prevent errors, and second can give the kernel polling time to provide a previous, improve efficiency. Select returns 1 for an error, 0 for time-out, and a positive value for all the prepared descriptors (the same descriptor if both read and write are ready, the result is +2).

2.int fd_isset (int FD, fd_set *fdset); FD in descriptor set non 0, otherwise return 0

3.int fd_clr (int FD, fd_set *fd_set); int fd_set (int FD, fd_set *fdset); int Fd_zero (Fd_set *fdset);

"Fd_zero () clears a set" in a man in Linux.  Fd_set () and FD_CLR () respectively add and remove a given file descriptor from a SET. Fd_isset () tests to see if a file descriptor are part of the set; This was useful after select () returns. " These functions have nothing to do with the descriptors of 0 and 1, just add the delete detection descriptor in set.

2) Epoll

1.int epoll_create (int size);
Create a handle to the Epoll, which is used to tell the kernel how large the number of listeners is. This parameter differs from the first parameter in select (), giving the value of the fd+1 that is the maximum listener. It should be noted that when the Epoll handle is created, it will occupy an FD value, under Linux if the view/proc/process id/fd/, is able to see the FD, so after the use of Epoll, you must call Close (), or it may cause FD to be exhausted.

2. int epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);
The event registration function of Epoll, which differs from select () tells the kernel what type of event to listen to when listening to events, and instead registers the type of event to listen on first. The first parameter is the return value of Epoll_create (), and the second argument represents an action, represented by three macros:
Epoll_ctl_add: Register the new FD to EPFD;
Epoll_ctl_mod: Modify the monitoring events of the registered FD;
Epoll_ctl_del: Delete a fd from the EPFD;
The third parameter is the FD to listen to, the fourth parameter is to tell the kernel what to listen to, struct epoll_event structure as follows:
struct Epoll_event {
__uint32_t events; /* Epoll Events */
epoll_data_t data; /* USER Data variable */
};

Events can be a collection of several macros:
Epollin: Indicates that the corresponding file descriptor can be read (including a graceful shutdown of the peer socket);
Epollout: Indicates that the corresponding file descriptor can be written;
Epollpri: Indicates that the corresponding file descriptor has an urgent data readable (this should indicate the arrival of out-of-band data);
Epollerr: Indicates an error occurred in the corresponding file descriptor;
Epollhup: Indicates that the corresponding file descriptor is hung up;
Epollet: Set Epoll to edge triggered mode, which is relative to the horizontal trigger (level triggered).
Epolloneshot: Listen to only one event, when the event is monitored, if you still need to continue to listen to the socket, you need to add the socket to the Epoll queue again

About Epoll working mode Et,lt

The LT (level triggered) is the default mode of operation and supports both block and No-block sockets. In this practice, the kernel tells you whether a file descriptor is ready, and then you can perform IO operations on the Ready FD. If you do not do anything, the kernel will continue to notify you, so this mode of programming error is less likely. Traditional Select/poll are the representatives of this model.
ET (edge-triggered) is a high-speed mode of operation and supports only no-block sockets. In this mode, when the descriptor is never ready to be ready, the kernel tells you through Epoll. It then assumes that you know that the file descriptor is ready, and no more ready notifications will be sent for that file descriptor until you do something that causes the file descriptor to no longer be ready, but be aware that if this FD is not being IO-enabled (which causes it to become not ready again), The kernel does not send more notifications (only once)

3. int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout)
Waits for the event to occur, similar to a select () call. Parameter events is used to get the collection of event from the kernel, the maxevents tells the kernel how big the events are, the value of this maxevents cannot be greater than the size when the Epoll_create () was created, the parameter timeout is the timeout (in milliseconds, 0 returns immediately ,-1 permanent blocking). The function returns the number of events that need to be processed, such as returning 0 to indicate a timeout.

Third, reference:

Apue (I/O multi-Channel transfer)

Linux man Epoll Select

Http://blog.chinaunix.net/uid-22663647-id-1771846.html

Http://www.cnblogs.com/OnlyXP/archive/2007/08/10/851222.html

Epoll and select differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.