Select, Epoll, and poll

Source: Internet
Author: User
Tags epoll

    1. Select function

The Select function is primarily used to implement multiplexed input and output models, and select system calls are used to allow us to monitor changes in the state of multiple file handles. The program stops waiting at select until one or more of the monitored file handles have occurred.

Select function:

int select (int nfds,fd_set *readfds,fd_set writefds,fd_set * exceptfds,struct timeval * Timeout)

The parameter Nfds is the maximum file descriptor value +1 that needs to be monitored;
The rdset,wrset,exset corresponds to a set of readable file descriptors that need to be detected, a set of writable file descriptors, and a different
A collection of common file descriptors.
The struct TIMEVAL structure is used to describe a length of time, if there is no event for the descriptor to be monitored during this time
Occurs when the function returns and the return value is 0.
The following macro provides a way to handle these three descriptive phrases:
FD_CLR (INR fd,fd_set* set), used to clear the bits of the associated FD in the description phrase set
Fd_isset (int fd,fd_set *set), used to test whether the bits of the associated FD in the description phrase set are true
Fd_set (int fd,fd_set*set), used to set the bit of the associated FD in the description phrase set
Fd_zero (Fd_set *set), used to clear all bits of the description phrase set
If the parameter timeout is set to:
NULL: Indicates that select () No timeout,select will be blocked until a file descriptor has occurred
Event.
0: Detects only the state of the descriptor collection and then returns immediately without waiting for an external event to occur.
Specific time value: If no event occurs during the specified time period, select will time out to return.
function return value:
Successful execution returns the number of changes to the state of the file descriptor
Returns 0 if the representation has exceeded the timeout time before the state of the descriptive word has changed;
Returns 1 when an error occurs,

Select program:

650) this.width=650; "title=" Select1.png "style=" Float:none "src=" http://s5.51cto.com/wyfs02/M00/85/84/ Wkiom1ena52jfekjaacxnofmsha646.png-wh_500x0-wm_3-wmp_4-s_2348573155.png "alt=" Wkiom1ena52jfekjaacxnofmsha646.png-wh_50 "/>

650) this.width=650; "title=" Select2.png "style=" Float:none "src=" http://s5.51cto.com/wyfs02/M00/85/84/ Wkiom1ena53wdsvoaadw3r7qd9w953.png-wh_500x0-wm_3-wmp_4-s_478317423.png "alt=" Wkiom1ena53wdsvoaadw3r7qd9w953.png-wh_50 "/>

650) this.width=650; "title=" Select3.png "style=" Float:none "src=" http://s5.51cto.com/wyfs02/M00/85/84/ Wkiol1ena53sdrsjaacmhzfugjg095.png-wh_500x0-wm_3-wmp_4-s_2638465775.png "alt=" Wkiol1ena53sdrsjaacmhzfugjg095.png-wh_50 "/>

650) this.width=650; "title=" Select4.png "style=" Float:none "src=" http://s5.51cto.com/wyfs02/M00/85/84/ Wkiol1ena56jt0qlaabvx-zk8a8825.png-wh_500x0-wm_3-wmp_4-s_2383603747.png "alt=" Wkiol1ena56jt0qlaabvx-zk8a8825.png-wh_50 "/>

650) this.width=650; "title=" Select.png "style=" Float:none "src=" http://s5.51cto.com/wyfs02/M01/85/84/ Wkiom1ena57sdmd3aab7svulkto354.png-wh_500x0-wm_3-wmp_4-s_2258181891.png "alt=" Wkiom1ena57sdmd3aab7svulkto354.png-wh_50 "/>

2.poll function

Unlike select, which uses three bitmaps to represent three Fdset, poll is implemented using a POLLFD pointer.

int poll (struct POLLFD * fds,nfds_t nfds,int Timeout)

struct POLLFD

{

int FD;

Short events;

Short revents;

};

The POLLFD structure contains the event to be monitored and the event that occurred, no longer using the Select "parameter-value" delivery method. While
POLLFD does not have a maximum number of limits (but the performance is also degraded when the number is too large). As with the Select function, the poll returns
Back, you need to poll the POLLFD to get the ready descriptor.
From the above, select and poll need to traverse the file descriptor to get a ready socket after returning. Thing
A large number of clients that are connected at the same time may only be in a very low-readiness state, so as the monitoring
The increase in the number of the descriptor, its efficiency will also be linearly reduced.

3.epoll function

int epoll_create (int size);
Creates a handle to a epoll. Since linux2.6.8, the size parameter has been ignored. It is important to note that when creating a good
After the epoll handle, it is going to occupy an FD value, under Linux if the view/proc/process id/fd/, is able to see this
FD, so close () must be called when the Epoll is finished, 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
is to register the type of event to listen on first.
The first parameter is the return value of Epoll_create ().
The second parameter 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 that needs to be monitored.

The fourth parameter tells the kernel what to listen for.

struct Epoll _event

{

_uint32_t events;

epoll_data_t data;

}

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 trigger (edge triggered) mode, which is relative to the horizontal trigger (level
triggered).
Epolloneshot: Listen for only one event, and if you need to continue listening after this event
Socket, you need to add the socket to the Epoll queue again.
3. int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout);
Collects events that have been sent in epoll monitored events. Parameter events is an array of allocated epoll_event structures,
Epoll will assign the event to the events array (events cannot be null pointers, the kernel is only responsible for reusing the data
To this events array, not to help us allocate memory in the user state). Maxevents, the core of the report.
How big the events are, the value of this maxevents cannot be greater than the size when the Epoll_create () was created, and the parameter timeout is timed out
The time (in milliseconds, 0 will return immediately, 1 will be uncertain, and there are statements that are permanently blocked). If the function call succeeds,
Returns the number of file descriptors that have been prepared for the I/O, such as returning 0 to indicate a time-out.

The program looks like this:

650) this.width=650; "title=" Epoll1.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M02/85/85/ Wkiom1enbaszt6zoaadeqxnfzke416.png-wh_500x0-wm_3-wmp_4-s_340155094.png "alt=" Wkiom1enbaszt6zoaadeqxnfzke416.png-wh_50 "/>

650) this.width=650; "title=" Epoll2.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/85/85/ Wkiom1enbaxxq2bzaadmdbc3luo760.png-wh_500x0-wm_3-wmp_4-s_890534943.png "alt=" Wkiom1enbaxxq2bzaadmdbc3luo760.png-wh_50 "/>

650) this.width=650; "title=" Epoll3.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M02/85/84/ Wkiol1enbaxwvtugaadqxne1d9u072.png-wh_500x0-wm_3-wmp_4-s_865362996.png "alt=" Wkiol1enbaxwvtugaadqxne1d9u072.png-wh_50 "/>

650) this.width=650; "title=" Epoll4.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/85/84/ Wkiol1enbawbqcixaab_xmrnvdi757.png-wh_500x0-wm_3-wmp_4-s_3181480864.png "alt=" wKioL1enBaWBqciXAAB_ Xmrnvdi757.png-wh_50 "/>

650) this.width=650; "title=" Empoll.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/85/85/ Wkiom1enbaas3u9oaaa61g8rlno073.png-wh_500x0-wm_3-wmp_4-s_165434202.png "alt=" Wkiom1enbaas3u9oaaa61g8rlno073.png-wh_50 "/>

4. Advantages and disadvantages of three functions and differences

(1) Select function

Cons: (1) Each time a select is called, the FD collection needs to be copied from the user state to the kernel state, which is much larger than FD (2) and each call to select requires a kernel traversal of all the FD passed in. This overhead is also very large in FD (3) The number of file descriptors supported by Select is too small, the default is 1024

(2) Epoll function

Epoll is an enhanced version of the multiplexed IO interface select/poll for Linux, which significantly reduces the CPU utilization of the program in cases where there is only a small amount of activity in a large number of concurrent connections. Because it does not reuse the set of file descriptors to pass the results, forcing the developer to prepare a collection of file descriptors to be listened to before each wait for the event, the other reason is that it does not have to traverse the entire set of descriptors to be listened to when the event is acquired. Just walk through the set of descriptors that are joined to the ready queue by a kernel IO event that wakes up asynchronously. Epoll provides edge triggering (edge triggered) in addition to the level triggered of Select/poll IO events, which makes it possible for user space programs to cache IO status and reduce epoll_wait/ Epoll_pwait calls to improve application efficiency.

Since Epoll is an improvement on select and poll, the above three drawbacks should be avoided. How did that epoll all work out? Before we take a look at the different invocation interfaces of Epoll and select and poll, both Select and poll provide only a function--select or poll function. While Epoll provides three functions, Epoll_create,epoll_ctl and Epoll_wait,epoll_create are created with a epoll handle; Epoll_ctl is the type of event registered to listen; Epoll_ Wait is waiting for the event to occur.

For the first drawback, the Epoll solution is in the Epoll_ctl function. Each time a new event is registered in the Epoll handle (specifying Epoll_ctl_add in Epoll_ctl), all FD is copied into the kernel instead of being duplicated at epoll_wait. Epoll guarantees that each FD will be copied only once throughout the process.

For the second disadvantage, the solution for Epoll is not to add current to the FD-corresponding device-waiting queue each time, like Select or poll, but to hang the current only once at Epoll_ctl (which is necessary) and to specify a callback function for each FD. This callback function is invoked when the device is ready to wake the waiting queue, and the callback function will add the ready FD to a ready list. Epoll_wait's job is actually to see if there is a ready-to-use FD (using Schedule_timeout () to sleep for a while, judging the effect of a meeting, and the 7th step in the Select implementation is similar).

For the third disadvantage, Epoll does not have this limit, it supports the maximum number of FD can open file, this number is generally far greater than 2048, for example, in 1GB memory of the machine is about 100,000, the exact number can cat/proc/sys/fs/ File-max, in general, this number and system memory relationship is very large.

Summarize:

(1) The Select,poll implementation requires itself to constantly poll all FD collections until the device is ready, during which time the sleep and wake-up cycles may be repeated. While Epoll actually needs to call Epoll_wait to constantly poll the ready linked list, there may be multiple sleep and wake alternates, but when it is device ready, call the callback function, put the ready FD into the Ready list, and wake the process into sleep in epoll_wait. While both sleep and alternate, select and poll traverse the entire FD collection while "Awake", while Epoll is "awake" as long as it is OK to determine if the ready list is empty, which saves a lot of CPU time. This is the performance boost that the callback mechanism brings.

(2) Select,poll each call to the FD set from the user state to the kernel state copy once, and to the device to wait for the queue to hang once, and epoll as long as a copy, and the current to wait for the queue is hung only once (in Epoll_ At the start of wait, note that the wait queue here is not the device waiting queue, just a epoll internally defined wait queue. This can also save a lot of overhead.

Same point:
1) All three need to register the user's concern on the FD;
2) All three need a timeout parameter to specify the time-out period;
different points:
1) Select:
A) Select specifies three sets of file descriptors, namely, readable, writable, and anomalous events, so that all possible events cannot be distinguished in more detail;
b) Select if a ready event is detected, it is changed on the original file descriptor to tell the application what time it occurred on the file descriptor, so when you call select again, you must first reset the file descriptor ;
c) Select takes the form of polling for all registered file descriptor sets, returning the entire user-registered collection of events, so the time complexity of the application index-ready file is O (n);
d) The maximum number of file descriptors that a select allows to listen to is usually limited, typically 1024, if the performance of the 1024,select is significantly lower;
e) can only work in the LT mode.

2) Poll:
A) poll the file descriptor and event binding, events can be specified independently, and can be a bitwise OR of multiple events, so that the registration of the event is more granular, and poll alone with an element used to save the results of a ready return, so that the next time you call poll, you do not have to reset the previously registered events;
b) poll uses a set of polling for all registered file descriptors, which returns the entire user-registered collection of events, so the time complexity of the application index-ready file is O (n).
C) poll uses the Nfds parameter to specify the maximum number of file descriptors and events to listen to, which can reach the maximum file descriptor that the system allows to open, which is 65535.
D) can only work in the LT mode.

3) Epoll:
A) Epoll the user registration of the file descriptor and events into the event table in the kernel, provides a separate system call Epoll_ctl to manage the user's events, and Epoll in the form of callbacks, once the registered file descriptor is ready, the trigger callback function, The callback function copies the ready file descriptor and events to the memory managed by the user space events, so that the time complexity of the application index-ready file reaches O (1).
b) Epoll_wait uses maxevents to develop the maximum number of file descriptors and events that can be opened by the system, that is, 65535 of the maximum file descriptors allowed.
c) Not only can work in the LT mode, but also support the ET efficient mode (ie, epolloneshot event, the reader can check the event type for themselves, for epoll thread safety is very helpful).


Select, Epoll, and poll

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.