The principle, advantages and disadvantages of the Select model

Source: Internet
Author: User

About I/O multiplexing:

I/O multiplexing (also known as "event-driven"), the first thing to understand is that the operating system provides you with a feature that can give you a notification when one of your sockets is readable or writable. This way, when used with a non-blocking socket, only if the system notifies me which descriptor is readable, I go to the read operation, can guarantee that every read can read the valid data without doing the pure return-1 and eagain of useless. Write operations are similar. This functionality of the operating system is implemented through system calls such as Select/poll/epoll, which can monitor the read-write readiness of multiple descriptors at the same time, so that I/O operations of multiple descriptors can be done alternately sequentially within a thread, which is called I/O multiplexing, The "reuse" here refers to reusing the same thread.

I/O multiplexing select

1. Introduction:
The purpose of the select system call is to listen for readable, writable, and anomalous events on the file descriptors that are of interest to the user for a specified period of time. Poll and select should be categorized as system calls that can block simultaneous detection of a set of non-blocking IO devices, until a device triggers an event or exceeds a specified wait time-that is, their duty is not to do Io, but to help the caller find the device that is currently ready.
Here is the schematic of select:

2. The select System invokes the API as follows:

#include <sys/time.h><sys/types.h><unistd.h>intSelect( int struct timeval *timeout);

The Fd_set struct is a set of file descriptors, which is actually an integer array, each of which is marked with a file descriptor for each element in the array. The number of file descriptors that can be accommodated by Fd_set is specified by fd_setsize, Fd_setsize equals 1024 in general, which limits the total amount of file descriptors that select can process at the same time.

3, the following describes the meaning of each parameter:
1) The Nfds parameter specifies the total number of file descriptors that are being monitored. It is usually set to the maximum value of all file descriptors in select Listener plus 1;
2) Readfds, Writefds, and Exceptfds respectively refer to the set of file descriptors corresponding to the events such as readable, writable, and abnormal. These three parameters are passed in the outgoing parameter, refers to before the call to select, the user will be interested in the readable, writable, or abnormal file descriptor through Fd_set (described below) The function is added into the Readfds, Writefds, Exceptfds file descriptor set, Select will listen for the file descriptors in these file descriptor sets, and if there is a ready file descriptor,Select resets the Readfds, Writefds, Exceptfds file description Fu Grulai notifies the application which file descriptors are ready. This feature will cause the Select function to return, before calling select again, you must reset the file descriptor we care about , that is, three file descriptions descriptor is not the one we passed in before.
3) The timeout parameter is used to specify the time-out for the Select function (which is also discussed below for the Select return value).

struct timeval{    long tv_sec;        // number of seconds    long tv_usec;       // number of microseconds };

4. The following functions (macro implementations) are used to manipulate the file description descriptor:

voidFd_set (intFD, Fd_set *Set);//set file descriptor fd in setvoidFD_CLR (intFD, Fd_set *Set);//clears the FD bit in setintFd_isset (intFD, Fd_set *Set);//determine if file descriptor FD is set in setvoidFd_zero (Fd_set *Set);//empties all bits in set (should be emptied before using the file descriptor set)//(Note the difference between FD_CLR and Fd_zero, one is to clear one, one is to clear all bits)

5, the Return of select:
1) If you specify timeout as Null,select will always wait until a file descriptor is ready, select returns;
2) If the specified time of timeout is 0,select not wait at all, return immediately;
3) If a fixed time is specified, the Select function returns if the specified file descriptor is ready for that period of time, and select returns if the specified time is exceeded.
4) return value condition:
A) within the timeout period, if the file descriptor is ready, select returns the total number of file descriptors ready (including readable, writable, and abnormal), and if no file descriptor is ready, select returns 0;
b) When the Select call fails, returns 1 and sets errno, if a signal is received, select returns 1 and sets errno to Eintr.

6. The file descriptor's readiness condition:
in network programming,
1) The socket is readable in the following cases :
a) The socket core receives a buffer of bytes greater than or equal to its low water mark So_rcvlowat;
b) The socket communicates the other side to close the connection, at this time the socket is readable, but once the socket is read, it will immediately return 0 (this method can be used to determine whether the client side disconnected);
c) Listen for a new connection request on the socket;
d) There are unhandled errors on the socket.
2) sockets can be written in the following cases:
a) The number of bytes available in the socket core send buffer is greater than or equal to its low water mark So_sndlowat;
b) The read end of the socket is closed, at which point the socket can be written, and once the socket is operated, the process receives the sigpipe signal;
c) After the socket is connected with connect successfully;
d) There are unhandled errors on the socket.

Select Advantages:

The Select model is the most common IO model in Windows Sockets. It uses the Select function for IO management. By calling the Select function, the application can determine whether the socket has data and whether it can write to the socket.

such as: Before calling the Recv function, call the Select function, if the system does not have readable data then the Select function will block here. When the system has readable or writable data, the Select function returns, and you can call the RECV function to receive the data.

as you can see Using the Select model, you need to call the function two times. The first time the Select function is called the second socket API. The advantage of using this mode is that you can wait for multiple sockets.

Select Cons:
(1) Each time you use Select, you need to copy the FD collection from the user state to the kernel state, the cost of FD is very large
(2) At the same time each tune? With select all need to traverse in the kernel to pass all the FD, this overhead is also very large when FD?
(3) Select? The number of file descriptors is too small, the default is 1024

The principle, advantages and disadvantages of the Select model

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.