Are there any platforms where using structure copy on an fd_set (for select() or pselect()) causes p
- by Jonathan Leffler
The select() and pselect() system calls modify their arguments (the 'struct fd_set *' arguments), so the input value tells the system which file descriptors to check and the return values tell the programmer which file descriptors are currently usable.
If you are going to call them repeatedly for the same set of file descriptors, you need to ensure that you have a fresh copy of the descriptors for each call. The obvious way to do that is to use a structure copy:
struct fd_set ref_set_rd;
struct fd_set ref_set_wr;
struct fd_set ref_set_er;
...
...code to set the reference fd_set_xx values...
...
while (!done)
{
struct fd_set act_set_rd = ref_set_rd;
struct fd_set act_set_wr = ref_set_wr;
struct fd_set act_set_er = ref_set_er;
int bits_set = select(max_fd, &act_set_rd, &act_set_wr, &act_set_er, &timeout);
if (bits_set > 0)
{
...process the output values of act_set_xx...
}
}
My question:
Are there any platforms where it is not safe to do a structure copy of the struct fd_set values as shown?
I'm concerned lest there be hidden memory allocation or anything unexpected like that. (There are macros/functions FD_SET(), FD_CLR(), FD_ZERO() and FD_ISSET() to mask the internals from the application.)
I can see that MacOS X (Darwin) is safe; other BSD-based systems are likely to be safe, therefore. You can help by documenting other systems that you know are safe in your answers.
(I do have minor concerns about how well the struct fd_set would work with more than 8192 open file descriptors - the default maximum number of open files is only 256, but the maximum number is 'unlimited'. Also, since the structures are 1 KB, the copying code is not dreadfully efficient, but then running through a list of file descriptors to recreate the input mask on each cycle is not necessarily efficient either. Maybe you can't do select() when you have that many file descriptors open, though that is when you are most likely to need the functionality.)
There's a related SO question - asking about 'poll() vs select()' which addresses a different set of issues from this question.