Introduction of the Select Poll Epoll model in Linux I/O multiplexing and comparison of its advantages and disadvantages

Source: Internet
Author: User
Tags bitwise epoll readable

About I/O multiplexing:

I/O multiplexing (also known as "event-driven") is the first thing to understand. The operating system provides you with a feature. When one of your sockets is readable or writable. It can give you a notice. When used with a non-clogging socket, it is only when the system notifies me which descriptive descriptor is readable that I run the read operation. The ability to ensure that every read reads valid data without making a pure return-1 and Eagain. Write operations are similar. This functionality of the operating system is implemented through system calls such as Select/poll/epoll. These functions are able to monitor the read-write readiness of multiple descriptive descriptors at the same time, so. * * I/O operations of multiple descriptive descriptors can be sequentially completed in a single thread, which is called I/O multiplexing, where "multiplexing" refers to reusing the same thread.

I. I/o multiplexing Select

1. Introduction:
The purpose of the select system call is to: within a specified period of time. Listen for readable, writable, and unusual events on the file description descriptor that the user is interested in. Poll and select should be categorized as such system calls, which can block the same time to detect a group of IO devices that support non-clogging until a device triggers an event or exceeds a specified wait time-that is, their duty is not to do Io, but to help the caller find the device that is currently ready.
The following is a schematic of select:

2. The select system calls the API for example:

#include <sys/time.h>#include <sys/types.h>#include <unistd.h>intselect(int*readfds*writefds*exceptfds*timeout);

The FD_SET structure is a set of file description descriptors, which is actually an integer array, each of each element of the array is tagged with a file descriptive descriptor. Fd_set the number of file descriptive descriptors that can be accommodated is specified by Fd_setsize. In normal cases, fd_setsize equals 1024, which limits the total number of file descriptor descriptors that select can handle at the same time.

3, the following describes the meaning of each of the parameters:
1) The Nfds parameter specifies the total number of file descriptive descriptors that are being monitored.

It is usually set to the maximum value plus 1 for all the file description descriptors of Select listener.
2) Readfds, Writefds, Exceptfds respectively point to readable, writable and abnormal events corresponding to the file description of the set of descriptors. These three parameters are incoming outgoing parameters, refers to the user before the call to select, the users will be interested in a readable, writable, or unusual file descriptor by Fd_set (the following) function is added into the Readfds, Writefds, Exceptfds file Description of the narrative set of descriptors, Select will listen to these file description descriptors in the descriptor set, assuming there is a ready file descriptive descriptor,Select resets the Readfds, Writefds, and Exceptfds file descriptor sets to inform the application which file descriptor is ready. This attribute causes the Select function to return. Before you call select again, you must reset the file description descriptor that we care about , that is, the set of three file-descriptive descriptors that we passed before.
3) The timeout parameter is used to specify the time-out for the Select function (which is also discussed below for the Select return value).

struct timeval{    long tv_sec;        //秒数    long tv_usec;       //微秒数};

4. The following functions (macro implementations) are used to manipulate the set of file description descriptors:

void  fd_set (   int  fd, fd_set *set ); //Set File Description descriptor FD  in Set void    FD_CLR (int  fd, fd_set *set ); //clears the FD bit in set   int  Fd_isset (int  fd, fd_set *set ); //infer if a file description descriptor is set in the set, FD  void           Fd_zero (fd_set *set ); //empties all the bits in the set (before using the file description descriptor set. should be emptied first)  //(note the difference between FD_CLR and Fd_zero, one is to clear one, one is to clear the whole site)   

5, the Return of select:
1) Assuming that the specified timeout is null,select will always wait until a file descriptor is ready, select returns.
2) Assume that the specified time of timeout is 0,select not wait at all, return immediately;
3) Assuming that a fixed time is specified, in this period of time, assuming that the specified file descriptor is ready, the SELECT function returns, assuming that more than the specified time, select also returns.
4) return value condition:
A) within the time-out period, assuming that the file description descriptor is ready, select returns the total number of file descriptor descriptors that are ready (including readable, writable, and abnormal), assuming no file descriptor is ready, select returns 0;
b) When the Select call fails, return 1 and set errno. Assume a signal is received. Select returns 1 and sets errno to Eintr.

6, the document description of the readiness of the narrative character conditions:
In Network programming,
1) The socket is readable in the following cases:
A) The socket core receives a buffer of bytes greater than or equal to its low water mark So_rcvlowat;
b) The socket communicates the other side to close the connection, at which point the socket is readable, but once the socket is read. will return 0 immediately (this method can be used to infer whether the client side is disconnected);
c) Listen for a new connection request on the socket.
D) There are unhandled errors on the socket.


2) sockets can be written in the following cases:
A) The number of bytes available in the socket core send buffer is greater than or equal to its low water mark So_sndlowat;
b) The read end of the socket is closed. The socket is writable at this time. Once the socket has been manipulated. The process will receive a sigpipe signal.
c) After the socket is connected with connect successfully;
D) There are unhandled errors on the socket.

II. Poll of I/O multiplexing

1. The principle of poll system call is similar to the prototype and select basically. It is also a certain number of file descriptive descriptors that are polled within a specified time. To test whether there is a ready person.

2, poll system call API such as the following:

#include <poll.h>int poll(structint timeout);

3, the following describes the meaning of each of the parameters:
1) The first parameter is a pointer to the first element of an array of structures, each of which is a POLLFD structure that specifies the conditions for testing a given descriptive descriptor.

struct pollfd{    int fd;             //指定要监听的文件描写叙述符    short events;       //指定监听fd上的什么事件    short revents;      //fd上事件就绪后,用于保存实际发生的时间}。

The event to be monitored is specified by the events member, and the function returns the state of the descriptive descriptor in the corresponding Revents member (each file descriptor has two events, one is the incoming events, and the other is the outgoing type of revents. This avoids the use of incoming outgoing parameters. Note the difference from select, which tells the application what events actually occurred on the FD. Events and revents can be bitwise OR of multiple events.
2) The second parameter is the number of file description descriptors to listen to, that is, the number of elements of the array FDS;
3) The third parameter has the same meaning as select.

4. Poll Event Type:

When you use Pollrdhup, you define the _gnu_source at the beginning of the code

5, the return of poll:
Same as SELECT.

Iii. Epoll of I/O multiplexing

1. Introduction:
Epoll and select and poll differ greatly in their use and implementation.

First, Epoll uses a set of functions to complete, rather than a single function. Secondly. Epoll the events in the file description descriptor that the user cares about are placed in an event table in the kernel. There is no need to pass the file Descriptor collection event set repeatedly for each invocation, as in select and poll.

2. Create a file Description descriptor that specifies the event table in the kernel:

#include<sys/epoll.h>int epoll_create(int size);    //调用成功返回一个文件描写叙述符。失败返回-1并设置errno。

A size parameter does not work. Just give the kernel a hint. Tell it how big the event table needs to be.

The file description descriptor returned by the function specifies the kernel event table to be visited, and is the handle to all other Epoll system calls.

3. Operation Kernel Event table:

#include<sys/epoll.h>int epoll_ctl(intintintstruct epoll_event *event);    //调用成功返回0,调用失败返回-1并设置errno。

EPFD is the file handle returned by Epoll_create. Identifies the event table. OP Specifies the type of operation.

The following 3 types of operations are available:

a)EPOLL_CTL_ADD, 往事件表中注冊fd上的事件;b)EPOLL_CTL_MOD, 改动fd上注冊的事件;c)EPOLL_CTL_DEL, 删除fd上注冊的事件。

The event parameters specify events, such as the following for the Epoll_event definition:

struct epoll_event{    __int32_t events;       //epoll事件    epoll_data_t data;      //用户数据};typedefunion epoll_data{    void *ptr;    int  fd;    uint32_t u32;    uint64_t u64;}epoll_data;

When using Epoll_ctl, an FD is added, changed into the kernel event table, or the event of FD is removed from the kernel event table.

Assuming that you are adding events to the event table, you can add event events to the FD on data. Instead of using FD in data, the FD is placed in the memory referred to by PTR in user data (since Epoll_data is a consortium.) Can only use one of the data), then set events.

3. epoll_wait function
The most critical function of a epoll system call is epoll_wait, which waits for a group file to describe the event on the descriptor for a period of time.

#include<sys/epoll.h>int epoll_wait(intstructintint timeout);    //函数调用成功返回就绪文件描写叙述符个数,失败返回-1并设置errno。

Timeout parameters and select are the same as poll. Specify a time-out period; maxevents Specifies the maximum number of events to listen on. Events is an outgoing parameter. The Epoll_wait function assumes that a fully-ready event is copied from the Kernel event table (the file referred to EPFD) to the array specified in events, assuming that the detected event is ready.

This array is used to output the ready events that the epoll_wait detects, not the same as select and poll. This is also the biggest difference between the Epoll and the former, the following in the difference between the three are also said.

Comparison of four or three groups of I/O multiplexing functions

Same point:
1) All three need to be on the FD to register users concerned about the event.
2) All three want a timeout parameter specifies the time-out period.
different points:
1) Select:
A) Select specifies three file descriptive set of descriptors, each of which are readable, writable, and anomalous events, so it is not possible to distinguish more closely between the events that may occur.
b) Select assumes that the detection of a ready event will be changed on the original file description descriptor to tell the application what time it took to describe the descriptor on the file, so when you call select again, you must first reset the file descriptor ;
c) Select uses the file description of the entire booklet set polling method, will return the whole user register of events collection, so the application index ready file time complexity is O (n).
d) The maximum number of file descriptors that a select agrees to listen to is usually limited. is usually 1024. It is assumed that performance greater than 1024,select will drop sharply;
e) can only work in the LT mode.

2) Poll:
A) poll describes the file descriptor and event binding, and events can be specified independently. and can be a bitwise OR of multiple events. This further refines the event's register, and poll alone uses an element to hold the result when it is ready to return, so that the next time poll is called. There is no need to reset the previous registration event;
b) The poll uses a document describing the presentation of the narrative set polling for all the brochures. Returns the collection of events for the entire user's brochure. So the time complexity of the application index-ready file is O (n).
C) Poll use Nfds to specify the maximum number of file descriptors and events to listen to, this number can reach the system agreed to open the largest file description of the descriptor. That is, 65535.
D) can only work in the LT mode.

3) Epoll:
A) epoll the file Description descriptor and events of the user's booklet into the event table in the kernel. Provides a stand-alone system call Epoll_ctl to manage user events, and epoll the way callbacks are used. Once the file description descriptor is ready for the register, the callback function is triggered, which copies the ready file descriptor and events to the memory managed by the user space events. This allows the time complexity of the application index-ready file to reach O (1).
b) Epoll_wait uses maxevents to develop a maximum number of file descriptors and events to listen to, which can reach the maximum file description descriptor that the system agrees to open, which is 65535.
c) Not only works in the LT mode, but also supports the ET efficient mode (that is, the Epolloneshot event, the reader can check the event type on their own, for epoll thread safety has very good help).

Select/poll/epoll Summary:

Introduction of the Select Poll Epoll model in Linux I/O multiplexing and comparison of its advantages and disadvantages

Related Article

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.