Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to test if a fd_set is "nonzero"?

Tags:

Is there an existing function to test the condition "are any bits set in a fd_set"?

If possible, I'd like to test whether any bits are set in a fd_set, as opposed to testing whether a specific fd is set, with FD_ISSET(). I'm trying to code something along the lines of (pseudocode):

...
select( max_fd + 1, &readfds, &writefds, NULL, NULL );
if ( FD_ISSET( specific_read_fd, &readfds ) )
{
  handleSpecificReadFdSet();
}
else if ( FD_IS_ANY_SET( &readfds ) ) // Desired functionality
{
  handleOtherReadFdSet();
}
else if ( FD_ISSET( specific_write_fd, &writefds ) )
{
  handleSpecificWriteFdSet();
}
else // if ( FD_IS_ANY_SET( &writefds ) )
{
  handleOtherWriteFdSet()
}
...

I.e. in response to select() becoming unblocked, I want separate handling for four conditions:

1) Whether a specific fd was set in the read fds

2) Whether any fd other than the specific read fd was set in the read fds

3) Whether a specific fd was set in the write fds

4) Whether any fd other than the specific write fds was set in the write fds

Is there an existing function that provides such a "is any fd in this fds set?" functionality? Or is the only way to do this to use FD_ISSET in a loop, e.g.:

...
bool ret_val = false;
for ( int i = 0; i < max_fd; ++i )
{
  if ( i == specific_read_fd ) continue;
  if ( FD_ISSET( i, &readfds ) ) { ret_val = true; break; }
}
return ret_val;
...

I'm open to alternatives to solving this problem other than specifically a "FD_IS_ANY_SET()" function - I'm not super experienced with select().

like image 542
StoneThrow Avatar asked Oct 16 '17 21:10

StoneThrow


People also ask

What does Fd_zero do?

This function initializes the file descriptor set to contain no file descriptors.

What is fd_set in socket?

The fd_set structure is used by various Windows Sockets functions and service providers, such as the select function, to place sockets into a "set" for various purposes, such as testing a given socket for readability using the readfds parameter of the select function.

What does select () do in C?

The select() function tests file descriptors in the range of 0 to nfds-1. If the readfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read.

What are the three file descriptors used in the select () call?

On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets (that is, the total number of bits that are set in readfds, writefds, exceptfds).


1 Answers

Here's how you might test whether or not an fd_set is empty:

bool FD_IS_ANY_SET(fd_set const *fdset)
{
    static fd_set empty;     // initialized to 0 -> empty
    return memcmp(fdset, &empty, sizeof(fd_set)) != 0;
}

The function FD_IS_ANY_SET returns true if *fdset contains at least one file descriptor, otherwise false.

like image 111
Frank B. Brokken Avatar answered Oct 14 '22 03:10

Frank B. Brokken