ZZ: Analysis of fd_set in Linux

Source: Internet
Author: User
ZZ: a fd_set data structure is provided in the select () mechanism of fd_set in Linux, which is actually a long array, each array element can be associated with an open file handle (whether it is a socket handle, or another file, named pipe or device handle) to establish a contact...
ZZ: a fd_set data structure is provided in the select () mechanism of fd_set in Linux, which is actually a long array, each array element can be associated with an open file handle (whether it is a socket handle, or other files, named pipes, or device handle). The programmer completes the connection, when select () is called, the kernel modifies the content of fe_set according to the IO status, thereby notifying the socket or file that the select () execution process is readable. The multi-port multiplexing function select at www.2cto.com must first set the number of listening ports before calling the function select. FD_ZERO is to clear the port set, and FD_SET is to set the Port set. The select () function is often used to use a process to listen to multiple server sockets. Sometimes, select () is also used as a latency function. The sleep () delay will release the CPU. if you select (), you can delay the CPU usage. The select () function is mainly based on the fd_set type. Fd_set is a collection of file descriptors (fd). it uses one field to represent an fd (fileDescripction will be described in detail below ), for the fd_set type, use the following four macros: fd_set set; FD_ZERO (& set);/* clears the set so that the set does not contain any fd */FD_SET (fd, & set ); /* Add fd to the set */FD_CLR (fd, & set);/* clear fd from the set */FD_ISSET (fd, & set ); /* test whether fd is in the set */in the past. an fd_set can only contain <32 fd (File description ), because fd_set actually only uses a 32-bit vector to represent fd; now, UNIX systems usually use the header file 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. Based on the bit vector implementation of fd_set, we can re-understand the four macros that operate fd_set: fd_set set; FD_ZERO (& set);/* set all the positions of the set 0, for example, if set occupies 8 places in the memory, set it to 00000000 */FD_SET (0, & set);/* to set 0th position 1, for example, set is 00000000, now it is changed to 100000000, so that the file description of fd = 1 will be added to the set */FD_CLR (4, & set);/* will set the 4th position 0, for example, if set is 10001000, it is now 10000000. in this way, the file description of fd = 4 is removed from set */FD_ISSET (5, & set ); /* test whether the 5th bits of the set are 1. if the original set is 10000100, a non-zero value is returned, indicating that the file descriptor fd = 5 is in the set; otherwise, 0 is returned */note: the maximum value of fd must be
 

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.