Select/poll/epoll comparison analysis


select/poll/epoll is an IO multiplexing mechanism that can monitor multiple descriptors at the same time. When a descriptor is ready (read or write ready), it will immediately notify the corresponding The program performs a read or write operation. Essentially select/poll/epoll is synchronous I/O, ie read and write is blocked.
One, select

int select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

Monitor 3 classes from select function File descriptors: writefds, readfds, exceptfds. Calling the select function blocks until the descriptor is ready (data is readable, writable, or an exception occurs) or timeout, and the function returns. When the select function returns, you can find the ready descriptor by traversing the descriptor set.

select disadvantages

  • The maximum number of file descriptors that a single process can monitor is 1024 on Linux. You can increase the upper limit by modifying the macro definition, but it also exists. Inefficiency is weak;
  • IO efficiency As the number of monitored descriptors increases, its efficiency also decreases linearly;

    2, poll

    Int poll (struct pollfd *fds, unsigned int nfds, int timeout);

    where pollfd represents the set of monitored descriptors, as follows
    struct pollfd { int fd; //file descriptors short events; //monitored The request event is short revents; //the event has occurred}; the

    pollfd structure contains the event to be monitored and the event that occurred, and there is no maximum limit on the pollfd (but too large a number will also cause a drop in performance). As with the select function, when the poll function returns, you can find the ready descriptor by traversing the descriptor set.

    From the above, both select and poll need to retrieve the ready socket by traversing the file descriptor after returning. In fact, a large number of clients connected at the same time may have only a few ready states at a time, so as the number of monitored descriptors increases, their efficiency decreases linearly.
    Three, epoll

    epoll is proposed in the 2.6 kernel, is an enhanced version of select and poll. Compared to select and poll, epoll is more flexible and has no descriptor limit. Epoll uses a file descriptor to manage multiple descriptors, storing events in the user space's file descriptors into an event table in the kernel, so that copying in user space and kernel space is only required once. The epoll mechanism is Linux's most efficient I/O multiplexing mechanism, waiting for I/O events for multiple file handles in one place.

    Select/poll has only one method, and epoll has three methods, epoll_create(), epoll_ctl(), and epoll_wait().
    3.1 epoll_create()
    int epoll_create(int size);

    is used to create a handle for epoll, size refers to the number of descriptors to listen to. Now the kernel supports dynamic expansion, the meaning of this value is only The number of fd allocated for the first time will be dynamically expanded when there is not enough space. After creating the epoll handle, it takes up an fd value.
    ls /proc/<pid>/fd///It can be executed by the terminal. After seeing the fd

    after using epoll, you must call close(). Turn off, otherwise it may cause fd to be exhausted.
    3.2 epoll_ctl()
    int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

    is used to perform op operations on file descriptors (fd) that need to be listened to, such as fd Add to the epoll handle.

  • epfd: is the return value of epoll_create();
  • op: indicates the op operation, represented by three macros, which represent adding, deleting, and modifying the listening event to fd. ;
  • EPOLL_CTL_ADD (add)
  • EPOLL_CTL_DEL (deleted)
  • EPOLL_CTL_MOD (modified)

  • fd: file description needs to listen
  • epoll_event: The event to be listened to, struct epoll_event is structured as follows:
    struct epoll_event { __uint32_t events; /* Epoll event */epoll_data_t data; /* user available data */};

    events may indicate corresponding file descriptor value :( operation)

  • EPOLLIN: read (including peer SOCKET normally closed);
  • EPOLLOUT: Writable ;
  • EPOLLERR: Error;
  • EPOLLHUP: Interrupt;
  • EPOLLPRI: High-priority readability (here should indicate the presence of out-of-band data);
    < Li>EPOLLET: Set EPOLL to edge trigger mode, this is phase In terms of the level trigger.
  • EPOLLONESHOT: listen only one incident, when finished listening this incident will no longer listen to this event

    3.3 epoll_wait ()
    int epoll_wait (int epfd , struct epoll_event * events, int maxevents, int timeout);
  • epfd: Wait for io events on epfd, returning maxevents events;
  • events: collections used to get events from the kernel ;
  • maxevents:events, the maxevents value cannot be greater than the size when creating epoll_create();
  • timeout: timeout (milliseconds, 0 will return immediately).

    This function returns the number of events that need to be processed. If 0 is returned, it has timed out.
    4. Contrast

    In select/poll, the process only scans all monitored file descriptors after calling a certain method, and epoll registers a file with epoll_ctl() in advance. Descriptor, once a file descriptor is ready, the kernel will use a callback-like callback mechanism to quickly activate the file descriptor and be notified when the process calls epoll_wait(). (The traversal of the file descriptor is removed here, but by listening to the mechanism of the callback. This is the charm of epoll.)

    epoll advantage

    1. Monitoring The number of descriptors is not limited. The supported FD limit is the maximum number of open files. The specific number can be viewed by cat /proc/sys/fs/file-max. Generally speaking, this number has a great relationship with system memory. This value for mobile phones is 200,000-300,000.

    2. IO efficiency does not increase with the number of monitoring fd decline. Epoll is different from the way poll and poll poll, but is implemented by the callback function defined by each fd. Only the ready fd will execute the callback function.

      If you do not a lot of idle-connection or dead-connection, epoll and efficiency ratio does not select /poll much higher, but when faced with a large number of idle-connection, you will find Epoll is much more efficient than select/poll.

  • Copyright © Windows knowledge All Rights Reserved