In-depth understanding of IO mode under LINUX (3) - select, poll, epoll under Linux

select, poll, epoll are 3 multiplexing (NIO in java) methods under linux

What is the difference between them, or go directly to the code:

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

 The select mode will monitor all readFD, writeFD, exceptFD

One disadvantage of select is that there is a maximum limit to the number of file descriptors that a single process can monitor
poll:
int poll (struct pollfd *fds, unsigned int nfds, int timeout);
 There is no maximum number limit for pollfd in poll mode
but!
There is not much difference between select and poll. They both train all fd/pollfd to get the ready fd. When there are a large number of connected clients, the efficiency will decrease linearly.
epoll:
1)int epfd = epoll_create(intsize);       
Create an ep handle (/proc/process id/fd/) to monitor all registered sockets, which is stored in a red-black tree data structure. The storage of this red-black tree uses mmap to separate the kernel mode and user mode. Sharing, reduces the data exchange between user mode and kernel mode, and select/poll copies the relevant handles from kernel mode to user mode each time it rotates. (mmap, kernel mode user mode has been mentioned before)
2)int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
Adding/modifying/deleting registered sockets is more efficient because it is in the red-black tree. When an event is added, the event will establish a callback connection with the corresponding device (network card) driver. Once the event occurs (file fd Change) The corresponding fd will call back this function and add the event to a rdllist (doubly linked list)
Event type:
EPOLLIN: Indicates that the corresponding file descriptor can be read (including the normal closing of the peer SOCKET); EPOLLOUT: Indicates that the corresponding file descriptor can be written; EPOLLPRI: Indicates that the corresponding file descriptor has urgent data to read (this should indicate that there is a The arrival of foreign data); EPOLLERR: Indicates that the corresponding file descriptor has an error; EPOLLHUP: Indicates that the corresponding file descriptor is hung up;
 
3)int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout)
3.1) Call ep_poll, hang when rdllist is empty, and wake up when rdllist is not empty
3.2) The ep_events_transfer function copies the epitem in the rdlist to the txlist, and clears the rdlist. ep_send_events function (critical), which scans each epitem in txlist and calls the poll method used by its associated fd pair. At this time, the call to poll is only to obtain the newer events on the fd (prevent the previous events from being updated), and then send the obtained events and the corresponding fd to the user space (encapsulated in struct epoll_event, returned from epoll_wait).


 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326316585&siteId=291194637