Linux IO multiplexing analysis (Select, Poll and Epoll)

Some time ago, I was forced to replace the Select mechanism in a bunch of code that no one maintained with Epoll. I take this opportunity to sort out the principles of Select, Poll and Epoll.

I/O multiplexing:

I/O multiplexing can monitor multiple descriptors through a mechanism. Once a descriptor is ready (generally read ready or write ready), it can notify the program to perform corresponding read and write operations. However, select, poll, and epoll are all synchronous I/Os in essence, because they are all responsible for reading and writing after the read-write event is ready, that is, the process of reading and writing is blocked, while asynchronous I/O is not responsible for reading and writing. The implementation of asynchronous I/O is responsible for copying data from the kernel to the user space.

Select:

Function prototype and use:

 #include <sys/select.h> #include <sys/time.h> int select(int maxfdp1, fd_set *readset,fd_set *writeset,fd_set *exceptset,const struct timeval *timeout) void FD_ZERO(fd_set *fdset);// Empty Collection Void FD_SET (int fd, fd_set * fdset);//Add a given file descriptor to the collection Void FD_CLR (int fd, fd_set * fdset);//Deletes a given file descriptor from the collection Int FD_ISSET (int fd, fd_set * fdset);//Check whether the file descriptor specified in the set can be read or written

Disadvantages:

  1. Every time you call select, you need to copy the fd set from user mode to kernel mode. It also needs to traverse all fds passed in the kernel, which is too expensive
  2. The number of file descriptors supported by select is too small. The default value is 1024, and the system kernel needs to be recompiled to increase the size

Poll:

Function prototype and use:

 # include <poll.h> int poll ( struct pollfd * fds,  unsigned int nfds, int timeout); /*Poll structure*/ struct pollfd { Int fd;/* file descriptor*/ Short events;/* waiting events*/ Short events;/* Actual events*/ };

Disadvantages:

  • Only the upper limit of the select descriptor is solved, but the efficiency problem is not solved.

Epoll:

Epoll was proposed in the 2.6 kernel and is an enhanced version of the previous select and poll. Compared with select and poll, epoll is more flexible and has no descriptor restrictions. Epoll uses one file descriptor to manage multiple descriptors, and stores the events of the file descriptor of the user relationship in an event table of the kernel, so that the copy in user space and kernel space only needs one time.

Function prototype and use:

 #include <sys/epoll.h> int epoll_create(int size); int epoll_ctl(int epfd,  int op, int fd, struct epoll_event *event); int epoll_wait(int epfd,  struct epoll_event * events, int maxevents, int timeout); struct epoll_event { __uint32_t events;  /*  Epoll events */ epoll_data_t data;  /* User data variable */ };

Disadvantages:

  • More complex to use

Simple summary:

contrast Select Poll Epoll
mechanism polling polling notice
Default FD upper limit one thousand and twenty-four No upper limit No upper limit
efficiency low low high

reference material:

https://www.cnblogs.com/anker/p/3265058.html
https://blog.csdn.net/qq_39612543/article/details/121963681


Warning : Undefined variable $output in /www/wwwroot/aotxland.com/wp-content/plugins/wp-alu/functions.php on line twenty-one

Post reply

Your email address will not be disclosed. Required items have been used * tagging