Select () System Call and fd_set Application

Source: Internet
Author: User
Select () System Call and application of fd_set in file descriptor set

Zhang Qing, network center of Hunan Hengyang Environmental Engineering Company

In network programs, it is common for a process to process multiple file descriptors at the same time. The Select () system call can enable the process to detect multiple I/O devices waiting for at the same time. When no device is ready, select () is blocked. When any device is ready, select ().
Select () is called in the form:
# Include <sys/select. h>
# Include <sys/time. h>
Int select (INT maxfd, fd_set * readfds, fd_set * writefds, fe_set * limit TFDs, const struct timeval * timeout );
The first parameter of select is the number of bits to be detected in the file descriptor set. This value must be at least 1 larger than the maximum file descriptor to be detected. ParameterReadfdsSpecifies the file descriptor set for read monitoring. ParameterWritefdsSpecifies the file descriptor set for write monitoring, and the parameterExceptfdsSpecifies the file descriptor set monitored by exception conditions.
The timeout parameter acts as a Timer: at the specified time, no matter whether the device is ready or not, a call is returned. The timeval structure is defined as follows:
Struct timeval {
Long TV _sec; // The number of seconds.
Long TV _usec; // indicates a few nuances
}
Timeout gets different values, and the call has different properties:
1. If the value of timeout is 0, the call will return immediately;
2. If timeout is null, the Select () call is blocked until the file descriptor is ready;
3. Timeout is a positive integer, which is a general timer.
When a select call returns, except the ready descriptors, select clears all unready descriptors in readfds, writefds, and specified TFDs. The return value of select is as follows:
1. Under normal circumstances, the number of ready file descriptors is returned;
2. No device is ready after the timeout duration. The returned value is 0;
3. If select is interrupted by a signal, it returns-1 and sets errno to eintr.
4. if an error occurs,-1 is returned and the corresponding errno is set.
The system provides four macros to operate the descriptor set:
# Include <sys/select. h>
# Include <sys/time. h>
Void fd_set (int fd, fd_set * fdset );
Void fd_clr (int fd, fd_set * fdset );
Void fd_isset (int fd, fd_set * fdset );
Void fd_zero (fd_set * fdset );
Macro fd_set sets the BIT (set to 1) corresponding to the file descriptor FD in the file descriptor set fdset ), macro fd_clr clears the FD bit (set to 0) corresponding to the file descriptor in the fdset of the file descriptor set ), macro fd_zero clears all bits in the fdset of the file descriptor set (both set all bits to 0 ). Use these three macros to set the descriptor shielding bit before calling the SELECT statement. After the SELECT statement is called, use fd_isset to check whether the bit corresponding to the file descriptor FD in the fdset of the file descriptor set is set.
In the past, the descriptor set was implemented as an integer shielding code, but this implementation will not work for more than 32 file descriptors. A descriptor set is usually represented by a bit field in an integer array. each bit of an array element corresponds to a file descriptor. For example, if an integer occupies 32 digits, the first element of the integer array represents the file descriptor 0 to 31, and the second element of the array represents the file descriptor 32 to 63, and so on. The macro fd_set sets the bit of the FD file descriptor to 1 in the integer array, and the macro fd_clr sets the bit of the FD file descriptor to 0 in the integer array, the macro fd_zero sets all bits in the integer array to 0. Assume that the following program is executed:
# Include <sys/select. h>
# Include <sys/time. h>
Fd_set readset;
Fd_zero (& readset );
Fd_set (5, & readset );
Fd_set (33, & readset );
The corresponding bits corresponding to the file descriptor 6 and 33 in the file descriptor set readset are set to, as shown in figure:
After executing the following program:
Fd_clr (5, & readset );
The corresponding bits of the file descriptor set readset corresponding to file descriptor 6 are set to 0, 2, as shown in:
Generally, the operating system uses the macro fd_setsize to declare the maximum number of file descriptors that can be operated by select in a process. For example:
Header file in 4.4bsd We can see:
# Ifndef fd_setsize
# Define fd_setsize 1024
# Endif
In the header file <bits/types. h> of Red Hat Linux, we can see:
# DEFINE _ fd_setsize 1024
In the header file <sys/select. h>, we can see that:
# Include <bits/types. h>
# Define fd_setsize _ fd_setsize
It defines fd_setsize as 1024, and an integer occupies 4 bytes, Which is 32 bits. An integer array containing 32 elements is used to represent the file descriptor set. We can modify this value in the header file to change the size of the file descriptor set used by the SELECT statement, but the modified value can be valid only after the kernel is re-compiled. The current version of UNIX operating system does not limit the maximum fd_setsize, which is usually limited by memory and system management.
After understanding the implementation mechanism of the file descriptor set, we can flexibly use it. (The following program runs in Red Hat Linux 6.0. The fd_isempty function is used to determine whether the file descriptor set is empty. The function fd_fetch is used to retrieve all file descriptors in the file descriptor set)
# Include <stdio. h>
# Include <string. h>
# Include <sys/time. h>
# Include <sys/select. h>
Struct my_fd_set {
Fd_set FS; // defines the file descriptor set FS
Unsigned int nconnect; // number of file descriptors in the file descriptor set FS
Unsigned int nmaxfd; // The largest file descriptor in the file descriptor set FS
};
/* The fd_isempty function is used to determine whether the file descriptor set is null. If it is null, 1 is returned. If it is not null, 0 is returned */
Int fd_isempty (struct my_fd_set * PFS)
{
Int I;
/* The file descriptor set fd_set is implemented through an integer array, therefore, the number of elements in an integer array myset is defined as the number of bytes occupied by the memory space of the file descriptor set fd_set divided by the number of bytes occupied by the integer memory space.
*/
Unsigned int myset [sizeof (fd_set)/sizeof (INT)];
/* Copy the file descriptor set PFS-> FS to the array myset */
Memcpy (myset, & PFS-> FS, sizeof (fd_set ));
For (I = 0; I <sizeof (fd_set)/sizeof (INT); I ++)
/* If an element of myset is not 0, it indicates that the file descriptor set is not empty, then the function returns 0 */
If (myset [I])
Return 0;
Return 1;/* if all elements of myset are 0, it indicates that the file descriptor set is empty, then the function returns 1 */
}
/* The fd_fetch function performs bit operations on the file descriptor set, converts a bit of 1 to the corresponding file descriptor, and then performs I/O operations on it */
Void fd_fetch (struct my_fd_set * PFS)
{
Struct my_fd_set * tempset; // defines a temporary structure pointer.
Unsigned int myset [sizeof (fd_set)/sizeof (unsigned INT)];
Unsigned int I, nbit, nfind, ntemp;
Tempset = PFS;
Memcpy (myset, & tempset-> FS, sizeof (fd_set ));
/* Divide maxfd by the number of digits occupied by the integer to obtain the subscript of the corresponding bits of maxfd in the file descriptor set corresponding to the corresponding element of the integer array myset, the purpose is to reduce the number of searches */
Nfind = tempset-> nmaxfd/(sizeof (INT) * 8 );
For (I = 0; I <= nfind; I ++ ){
/* If an element of the array myset is 0, the 32-bit value of the file descriptor set corresponding to this element is 0, the next element is determined. */
If (myset [I] = 0) continue;
/* If an element of the array myset is not 0, it indicates that the 32-bit file descriptor set corresponding to this element has 1. Assign the value of myset [I] to the Temporary Variable ntemp, perform bitwise operations on ntemp and convert the bitwise of 1 to the corresponding file descriptor */
Ntemp = myset [I];
/* Nbit records the binary digits of an integer, and performs the & 1 operation on ntemp from low to high until the highest digit of the integer, or until the number of file descriptors in the file descriptor set is equal to 0 */
For (nbit = 0; tempset-> nconnect & (nbit <sizeof (INT) * 8); nbit ++ ){
If (ntemp & 1 ){
/* If one is 1, the corresponding file descriptor is nbit + 32 * I, and then we can perform I/O operations on it. Here I just made a simple display. */
Printf ("I = % d, nbit = % d, the file description is % d/N", I, nbit, nbit + 32 * I );
/* After obtaining a file descriptor, subtract 1 from the number of file descriptors in the file descriptor Set */
Tempset-> nconnect --;}
Ntemp> = 1; // ntemp shifts one digit to the right.
}
}
}
 
/* The following main program tests the above two functions */
Main ()
{
/* Assume that fd1, fd2, and FD3 are three file descriptors, which can be socket descriptors in actual use */
Int fd1 = 7, fd2 = 256, FD3 = 1023, isempty;
Struct my_fd_set connect_set;
Connect_set.nconnect = 0;
Connect_set.nmaxfd = 0;
Fd_zero (& connect_set.fs );
/* Test the fd_isempty function before the fd_set operation */
Isempty = fd_isempty (& connect_set );
Printf ("isempty = % d/N", isempty );
Fd_set (fd1, & connect_set.fs );
Fd_set (fd2, & connect_set.fs );
Fd_set (FD3, & connect_set.fs );
Connect_set.nconnect = 3;
Connect_set.nmaxfd = FD3;
/* After the fd_set operation, add the file descriptor to the file descriptor set and test the function fd_isempty */
Isempty = fd_isempty (& connect_set );
Printf ("isempty = % d/N", isempty );
/* Test the function FD _ fetch */
Fd_fetch (& connect_set );
}
 
/* Program output result :*/
Isempty is 1
Isempty is 0
I = 0, nbit = 7, the file description is 7
I = 8, nbit = 0, the file description is 256
I = 31, nbit = 31, the file description is 1023

 

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.