Comparison of Linux/Unix select functions and select/poll and epoll

Source: Internet
Author: User

Select () is an important function in Linux/Unix network programming. You can call the select function to determine the status of one or more Sockets (descriptors, determine whether data on the socket needs to be read or written

# Include <sys/select. h> # include <sys/time. h> int select (int max_fd_p_1, fd_set * readset, fd_set * writeset, fd_set * reset tset, struct timeval * timeout); Return: Number of ready descriptors, 0 -- timeout, -1 -- Error

 

The select function has five parameters. We will introduce each parameter.

Parameter max_fd_p_1
The kernel requires a number to specify the maximum descriptive word, because the kernel uses this parameter to traverse a group of descriptive words. Because the description is counted from 0, max_fd_p_1 adds 1 to the maximum description.
Readset
Select can test whether a set of descriptive words are readable. You need to tell the kernel all the descriptive words that interest you. readset is used to specify a collection of descriptive words that interest you.
Writeset
Like a readset that specifies a set of readable descriptive words, writeset is used to specify a set of writeable descriptive words that you care about.
Parameter effectset
Like readset and writeset, javastset is used to specify a collection of abnormal descriptive words of interest, that is, when a descriptive word in the collection encounters an exception, it will receive a notification from the kernel.
Timeout
Used to specify the timeout length. This is a struct.
Struct timeval {long TV _sec; // number of seconds long TV _usec; // Number of microseconds}

Usage Details UNP Third Edition

Difference between epoll and select/poll:

1. support a process to open a large number of socket Descriptors (FD)
Select
The most intolerable thing is that the FD opened by a process has certain limitations, which are set by FD_SETSIZE. The default value is 2048. For IM servers that need to support tens of thousands of connections, there are obviously too few. At this time, you can choose to modify this macro and then re-compile the kernel, but the materials also pointed out that this will bring about a reduction in network efficiency, and 2. You can choose a multi-process solution (traditional
Apache solution). However, although the cost of creating a process on linux is relatively small, it can still be ignored. In addition, data synchronization between processes is far less efficient than inter-thread synchronization, so it is not a perfect solution. However
Epoll does not have this limit. The FD limit supported by epoll is the maximum number of files that can be opened. This number is generally greater than 2048. For example, the number of machines with 1 GB of memory is about 0.1 million.
/Proc/sys/fs/file-max. Generally, this number has a great relationship with the system memory.

2. IO efficiency does not decrease linearly as the number of FD increases

Another critical weakness of traditional select/poll is that when you have a large set of sockets, but due to network latency, only some of the sockets at any time are "active, however, each select/poll call will linearly scan all sets, resulting in a linear decline in efficiency. However, epoll does not have this problem. It only operates on "active" sockets-this is because epoll is implemented based on the callback function on each fd in kernel implementation. Then, only the "active" socket will actively call
Callback Function, other idle status socket will not, in this regard, epoll implements a "pseudo" AIO, because at this time the driver is in the OS kernel. In some
In benchmark, if all the sockets are basically active-for example, in a high-speed LAN environment, epoll is not more efficient than select/poll. On the contrary, if epoll_ctl is used too much, the efficiency is also slightly lower. However, once idle is used
Connections simulates the WAN environment, and epoll is far more efficient than select/poll.

3. Use mmap to accelerate message transmission between the kernel and user space.

This actually involves the specific implementation of epoll. Both select, poll, and epoll require the kernel to notify users of FD messages. It is important to avoid unnecessary memory copies, epoll is implemented through the same memory of the user space mmap kernel. If you want me to focus on epoll from the 2.5 kernel, you will not forget to manually
Mmap.

4. kernel fine-tuning

This is not an advantage of epoll, but an advantage of the entire linux platform. Maybe you can doubt the linux platform, but you cannot avoid the linux platform giving you the ability to fine-tune the kernel. For example, if the Kernel TCP/IP protocol stack uses a memory pool to manage the sk_buff structure, you can dynamically adjust the size of this memory pool (skb_head_pool) during runtime ---
Echo
XXXX>/proc/sys/net/core/hot_list_length is complete. For example, the listen function's 2nd parameters (TCP completes the length of the packet queue after three handshakes) can also be dynamically adjusted based on the memory size of your platform. Even in a special system with a large number of data packets but the size of each data packet itself is small, try the latest napi nic driver architecture.

Related Article

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.