[UNIX] Select, poll, epoll Learning

Source: Internet
Author: User
Tags epoll

All three are UNIX-based multiplexing kernel interfaces. Select is a cross-platform interface, poll is the systemv standard, and epoll is a proprietary Linux interface, which is transformed Based on Poll.

Select

Function prototype:

Int select (int n,
Fd_set * readfds,
Fd_set * writefds,
Fd_set * required TFDs,
Struct timeval * timeout );
The SELECT statement monitors arrays of multiple file descriptors through system calls. After the SELECT statement is returned, the ready file descriptor is modified, this allows the process to obtain the ready file descriptor and continue the I/O operation.

The current advantage of select is that almost all systems support select. The disadvantage is that you can only listen to a maximum of 1024 file descriptors. To listen to more than 1024 file descriptors, You need to manually modify the fd_setsize, however, this activity degrades the network I/O performance, or creates multiple processes and listens to 1024 file descriptors for each process. This increases the complexity of the program.

The first parameter n of select needs to be processed by the programmer. n is the maximum file descriptor value in all sets + 1; the file descriptor listed in the second readfds parameter of select is monitored for available data to be read; the file descriptor listed in the third writefds parameter of select is monitored for write completion without blocking; the file descriptor listed by 'select' in 'forwartfds is monitored for exceptions or uncontrolled data availability (these statuses are only used for sockets ); when the file descriptor list is set to null, this type of file descriptor is not monitored. The fourth timeout parameter of select is used to set the time-out length. If the SELECT statement still does not have available file descriptors after the time-out duration expires, it is returned; the return value of select indicates the number of currently available file descriptors. The range is 0-1024. When the return value is time-out, the return value is 0. Otherwise, the return value is a value in the range of 1.

Struct timeval {

Long TV _sec;/* seconds */
Long TV _usec;/* 10e-6 second */

};

If the timeout parameter of the SELECT statement is not null, even if no file descriptor is ready for I/O, the SELECT statement is returned after the second of ty_sec and the second of ty_usec. The processing of timeout in different systems is different, it is best to set it once before each call to select.

Adding or deleting a file descriptor to or from the file descriptor set monitored by select usually uses a macro defined by the system.

Fd_zero: clears the set.

Fd_set: Add a file descriptor to the collection.

Fd_clr: delete a file descriptor from the collection

Fd_isset: indicates whether the specified file descriptor is in the collection.

The maximum file descriptor value that can be put into fd_set is 1024, which is determined by fd_setsize.

After the Select call is successful, the non-null file descriptor set will be modified, and only the prepared file descriptor will be left in the set. The fd_isset Round Robin will be used to determine the file descriptor for preparing I/O.

The disadvantage of select is that the data structure of the maintained storage file descriptor needs to be replicated back and forth between the kernel space and the user space. As the number of file descriptors increases, the overhead of replication also increases linearly; due to network latency, many sockets are inactive, but the SELECT statement also performs a scan. These two disadvantages cause the select efficiency to decrease with the number of file descriptors and the timeliness of processing a large number of FD inactive sets.

Select only supports level triggered (Level triggered): that is, if I/O operations are not performed on a FD after it is returned to be available, the next time the SELECT statement is returned, this FDI/o is still reported.

Poll

Poll and select are essentially similar, but there is no limit on the maximum number of file descriptors. Poll and select have the same disadvantages: The maintained file descriptor data structure is copied back and forth in the kernel space and user space, efficiency decreases with the number of monitored file descriptors.

Int poll (struct pollfd * FDS, unsigned int NFDs, int timeout );
Struct pollfd {
Int FD;/* file descriptor */
Short events;/* Requested events to watch */
Short revents;/* returned events witnessed */
};

Each pollfd in poll indicates a monitored file descriptor, NFDs indicates the maximum number of monitored file descriptors, and timeout indicates the timeout duration, in milliseconds.

The fd of the pollfd struct is the file descriptor, the events is the event mask to be monitored, and the revents is the event mask of the file descriptor operation result to be monitored. The kernel sets this domain when returning the result. Every events may be returned in revents.

 Epoll

Epoll is a method directly supported by the kernel after linux2.6 and has all the advantages of select and poll. It is considered to be the best I/O readiness notification method in linux2.6.

Epoll supports level triggered and edge triggered. When the file descriptor is ready, only one process is notified, even if the process does not perform I/O operations on it, will not be notified in the future), edge triggering method is suitable for high-speed I/O, but programming implementation is more complicated.

Epoll also only informs the ready file descriptors. When epoll_wait is successfully called, the returned value indicates the ready file descriptor, in this case, epoll extracts the corresponding number of file descriptors from a specified array. epoll uses the file ing technology (MMAP), and the kernel and user mode access the same memory segment, this avoids the mutual replication of file descriptors between the kernel and the user State. epoll also uses event-based readiness notification. epoll registers a file descriptor through epoll_ctl in advance, once a file descriptor is ready, the system uses a mechanism similar to callback to quickly activate this file descriptor. When the process calls epoll_wait, it will be notified.

Epoll has the advantage that the number of file descriptors that can be monitored is equal to the maximum number of file descriptors that can be opened by the system; epoll efficiency does not linearly decrease with the increase in the number of file descriptors monitored; epoll reduces the overhead of kernel and user-mode Data Replication through file ing.

Function prototype

Int epoll_create (INT size );

Create an epoll handle and specify the number of file descriptors to listen

Int epoll_ctl (INT epfd, int op, int FD, struct epoll_event * event );

Perform operations on the epoll handle

Epfd specifies the epoll handle;

OP is the specified operation ep_ctl_add (register a new handle to epoll); ep_ctl_mod (modify the listening event of the registered file descriptor); ep_ctl_del (delete a registered FD)

FD is the file descriptor to be registered

Event is the monitored event

The epoll_event structure is as follows:

Struct epoll_event {

_ Uint32_t events;/* epoll events */

Epoll_data_t data;/* User Data variable */

}

Events can use the following macro Sets

Epollin: The current file descriptor is readable.

Epollout: The current file descriptor can be written.

Epollpri: The current file descriptor has urgent data that can be read (it should indicate that out-of-band data has arrived)

Epollerr: The current file descriptor is incorrect.

Epollhup: The file descriptor is hung up.

Epollet: Set epoll to edge trigger mode

Epolloneshot: only listens to events once. If you need to listen to this descriptor after a listener is completed, you need to add the descriptor to the epoll queue again.

Int epoll_wait (INT epfd, struct epoll_event * events, int maxevents, int timeout );

Wait for the occurrence of the event, similar to the return of select.

 Postscript

This study note is based on the Web search, non-original, and does not use select, poll, epoll. It can be said that learning from scratch will inevitably be a little thin, and will be supplemented later with practical experience.

Supplement

Can I use both IPC and select, poll, and epoll? Normally, this will cause a lot of trouble, because the implementation of IPC is not a file descriptor. If you need to use IPC and multiplexing (select, poll, epoll) at the same time ), the Fork sub-process can be used, and then the parent process and sub-process communicate with each other using IPC or pipeline. One of the processes uses multiplexing technology to monitor file descriptors.

Multiplexing focuses on processing system file descriptors such as socket, pipeline, Pseudo Terminal (Pty), and terminal device (TTY), which does not work for common file descriptors.

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.