Select () function and fd_zero, fd_set, fd_clr, fd_isset (convert)

Source: Internet
Author: User

The select function is used in non-blocking scenarios. When a socket or a set of sockets have signals, the system provides the select function to achieve multiplexing of input/output models,

Prototype: int select (INT maxfd, fd_set * rdset, fd_set * wrset, fd_set * exset, struct timeval * timeout );

Header files: # include <sys/time. h> and # include <unistd. h>

First, we will give a brief introduction to the parameters in the function. The maxfd parameter is the maximum file descriptor value to be monitored + 1;

Rdset, wrset, and exset correspond to the set of readable file descriptors to be detected, the set of writable file descriptors, and the set of abnormal file descriptors respectively.

The struct timeval structure is used to describe the length of a period of time. If no event occurs in the descriptor to be monitored during this time, the function returns and the return value is 0.

In these parameters, there is something similar to a struct, fd_set. What is the name of this? Let's take a look at its meaning. This is a collection of file descriptors (FD). It represents an FD with one digit,

Wait, the file description. You should be familiar with it. Previously, we used this as a place where the file path was saved, that is, a symbol of a file. Now we are not doing any conjecture, let's see how it is introduced below.

For the fd_set type, use the following four macros:

Fd_zero (fd_set * fdset) clears the specified file descriptor set. It must be initialized before setting the file descriptor set. If it is not cleared, the result is unknown because the system usually does not clear the memory space after it is allocated.

Fd_set (fd_set * fdset) is used to add a new file descriptor to the file descriptor set.

Fd_clr (fd_set * fdset) is used to delete a file descriptor in the file descriptor set.

Fd_isset (int fd, fd_set * fdset) is used to test whether the specified file descriptor is in this set.

Past ...... I decided to remove the long introduction to the past situation to ensure everyone's attention, so as to keep the attention of the goal.

Now, Unix systems usually have the header file <sys/select. h> defines the constant fd_setsize, which is the number of descriptive words of the Data Type fd_set. The value is usually 1024, which indicates the FD of <1024.

After studying the fd_set information, let's get back to the understanding of the select function.

Function: test whether the specified FD is readable? Writable? Are there any exception conditions to be processed?

Readset is a set of file description words used to check readability.

Writeset is a set of file descriptions used to check the writability.

Exceptset is used to check whether there are file descriptions with exception conditions. (Note: do not include errors)

Timeout is used to describe a period of time. If no event occurs in the descriptor to be monitored during this period, the function returns and the return value is 0.

Simply put, the Select function is used to test the file FD.

There are three possible test results:

1. Timeout = NULL (blocking: select will be blocked until an event occurs on a file descriptor)

2. The structure pointed to by timeout is set to a non-zero time (wait for a fixed time: if an event occurs or the time is exhausted within the specified time period, the function returns)

3. The structure pointed to by timeout. The time is set to 0 (non-blocking: Only checks the state of the descriptor set, and then returns immediately without waiting for the occurrence of external events)

 

Return Value: return the total number of FD with the corresponding bit still being 1.

Note: Only the FD bits that are readable, writable, and with exception conditions to be processed are still 1. Otherwise, it is 0.

For example, for Recv (), when no data comes to call it, your thread will be blocked. If data never comes, your thread will be blocked for a long time. this is obviously not good.

Therefore, select is used to check whether the set of characters are readable (that is, whether data is read ).

The steps are as follows -- socket s ;..... fd_set set; while (1) {fd_zero (& set); // clear your set of characters fd_set (S, & set ); // Add the knots you are interested in to the set. Here is a Data Reading character s select (0, & set, null );

// Check whether the set of characters are readable,

// In many cases, whether there is data (note, but in many cases)

// Whether the SELECT statement has an error. If (fd_isset (S, & set) is not written)

// Check whether s is in this set. {// select will update this set, remove unreadable sets of knots // retain only qualified sets of knots in this set Recv (S ,...);} // do something here}

 

The key to understanding the select model is to understand fd_set. For convenience, the length of fd_set is 1 byte. Each bit in fd_set can correspond to a file descriptor FD. The fd_set with a length of 1 byte can correspond to a maximum of 8 FD.

(1) execute fd_set set; fd_zero (& set); then set uses bits to indicate that it is 00.

(2) If FD = 5, execute fd_set (FD, & set); then set to 0001,0000 (5th position is 1)

(3) If FD = 2 or FD = 1 is added, Set Becomes 0001,0011.

(4) execute select (6, & set, 0, 0) to block the wait

(5) If both FD = 1 and FD = 2 have readable events, select returns, and set changes. Note: FD = 5 where no event occurs is cleared.

 

Based on the above discussion, we can easily draw the features of the select model:

(1) the number of monitored file descriptors depends on the value of sizeof (fd_set.

(2) It can effectively break through the maximum file descriptors that can be monitored by select.

(3) When FD is added to the select monitoring set, another data structure array must be used to store FD in the select monitoring set,

First, it is used to judge fd_isset as the source data and fd_set after the return of select.

Second, after the SELECT statement is returned, the previously added but no event occurs in FD will be cleared, and the FD will be re-obtained from the array one by one before each start of the SELECT statement (fd_zero first ),

Scans the array and obtains the maximum FD value maxfd, which is used as the first parameter of select.

 

(4) it can be seen that the select model must loop array (add FD, take maxfd) before Select, And Then loop array (fd_isset to determine whether a time has occurred) After Select returns ).

The process of using the select function is generally: First call the macro fd_zero to clear the specified fd_set, and then call the macro fd_set to add the FD to be tested to fd_set,

Then, call the select function to test all FD in fd_set, and use the macro fd_isset to check whether the corresponding bit of a FD is still 1 after the select function is called.

The following is an example of testing the readability of descriptive words in a single file:

Int isready (int fd) {int RC; fd_set FDS; struct Tim TV; fd_zero (& FDs );

Fd_set (FD, & FDs );

TV. TV _sec = TV. TV _usec = 0;

Rc = select (FD + 1, & FDS, null, null, & TV );

If (RC <0) // error return-1;

Return fd_isset (FD, & FDs )? 1: 0 ;}

 

Source: http://blog.sina.com.cn/s/blog_a43aa27401015kt9.html

Select () function and fd_zero, fd_set, fd_clr, fd_isset (convert)

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.