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
Prototype:
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
Prototype:
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.
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)
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
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.
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.
diskless boot everyone should have heard of it, and now many Internet cafes and enterprises have use
The Linux command line is useful, efficient, and fun, but sometimes dangerous, especially if you are
Computer Store News: first Linux systems for flat panel launch For those who have tired of the N
Mono is a cross-platform, open source .NET development framework. Support for Linux systems, which
The correct way to restore EXT3 Superblock(bad superblock)
How to uninstall software under Linux
The role of CTRL+Z when running commands under Linux operating system
Svn use under linux (svn command)
Linux system uses NFS to achieve file sharing
Linux files, file descriptors and dup() and dup2()
Analysis and comparison of four real-time operating systems
Live555 can't get ip address solution under LINUX
Linux system command sudo use detailed explanation
Kdump and crash configuration methods and failure analysis methods
Linux use pass to manage passwords
Windows7 system 0X0000007A blue screen failure
How to open 802.1x network authentication in Win8 system?
Master tricks: XP cool search skills two
Pointing to the maze Commonly used software practical skills five
Teach you Win XP Remote Desktop can also play multi-user
Traffic function of Win8 wireless network
The new ChromeOS operating system has been opened