How to use Select in Linux

Source: Internet
Author: User

Fd_set is a set of file descriptors (Fd,file descriptor) that represents an FD with one.

The system provides 4 macros to describe the descriptor operation:

#include <sys/select.h>
#include <sys/time.h>

Set file description descriptor bits in Fdset corresponding to the file descriptor FD (set to 1)
void Fd_set (int FD, fd_set *fdset);

Clears the bits (set to 0) in the file descriptor descriptor Fdset that correspond to the file descriptor FD
void fd_clr (int FD, fd_set *fdset);

Clears all bits in the file description descriptor Fdset (sets all bits to 0)

void Fd_zero (Fd_set *fdset);

Use Fd_isset to detect if a bit is set in the file descriptor descriptor Fdset that corresponds to the file descriptor FD, after calling select.
void Fd_isset (int FD, fd_set *fdset);

For example, the following section of code:

Fd_set Readset;
Fd_zero (&readset);
Fd_set (5, &readset);
Fd_set (&readset);

The corresponding bits in the file description descriptor Readset corresponding to the file descriptor 5 and 33 are set to 1, and 1 are shown:

After executing the following procedure:
FD_CLR (5, &readset);
The file description descriptor Readset corresponds to the corresponding bit of the file descriptor 5 is set to 0, 2 as shown:

UNIX systems typically define a constant fd_setsize in the header file <sys/select.h>, typically defined as 1024, and the operating system via macro Fd_ SetSize to declare the maximum value of the file descriptor (that is, the number of bits that fd_set contains) that can be manipulated by select in a process. We can modify this value in the header file to change the size of the set of file descriptors used by Select, but be aware that the kernel must be recompiled for the modified value to be valid.

Fd_setsize is a value under Linux, the limit file descriptor cannot be greater than 1024.

Fd_setsize is also the number under Linux, which determines the number of bits that fd_set contains.

The following is an excerpt from man select:

"Executing fd_clr () or fd_set () with a value of FD, that's negative or is equal to or larger than fd_setsize would result In undefined behavior. "

The prototype of the Select function is as follows:

#include <sys/types.h>

#include <sys/time.h>
int select (int maxfdp1,fd_set *readset,fd_set * writeset,fd_set excpetset,const struct timeval *timeout);

There are three possible return values.

1. Normally returns the number of file descriptors ready, that is, the total number of FD that corresponds to a bit that is still 1.
2. After the timeval time is still no equipment ready, the return value is 0;
3. If an error occurs, return 1 and set the corresponding errno.

EBADF: File description Word is invalid or the file is closed
EINTR: This call is interrupted by the signal
EINVAL: Parameter n is negative
ENOMEM: Insufficient core memory

Parameters:

The first parameter, MAXFDP1: is the maximum value of all the added file descriptor sets, plus 1. For example, if our file descriptor is 1, 4, 5, then MAXFDP1 should be set to 6.

The purpose of MAXFDP1 is to improve efficiency so that the function does not have to check all of the fd_set bits.

Maxfdp1,max file descriptor plus 1

2nd, 3, 43 parameters: These three parameters are file descriptor collection types.

2nd: When the state of a file descriptor in the file descriptor collection becomes readable, the system tells the SELECT function to return.

3rd: When the state of a file descriptor in the file descriptor collection becomes writable, the system tells the SELECT function to return.

4th: When there is a special case for a file descriptor in the file descriptor collection, the system tells the SELECT function to return.

When Select returns: three sets of input parameters for the Fd_set type will be changed. The bit of FD that has no state change in Fd_set is set to 0. The bits of the FD that are readable, writable, and with exceptional conditions to be processed remain at 1.

Last parameter: A pointer to the Timeval struct, timeval specifies the number of seconds and microseconds.

struct timeval{

Long tv_sec;//number of seconds

Long tv_usec;//microseconds

};

There are three possibilities of timeval:

1.timeval=null (blocked: function is returned until an FD bit is set to 1)

2.timeval The structure pointed to is set to nonzero time (wait for fixed time: There is an FD bit set to 1 or time exhausted, the function returns)

3.timeval points to a structure with a time set of 0 (non-blocking: functions are returned immediately after checking each FD)

Use process:

Call the macro Fd_zero first to clear the specified Fd_set,

Then call the macro fd_set to add the FD that needs to be tested to Fd_set,

Then call the function Select to test all the FD in the Fd_set, and modify the fd_set corresponding bit according to the state.

Finally, a macro fd_isset is used to check if an FD is still 1 after the function select is called.

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.