question about select() and range of for loop to read sockets
Just a quick question about using select()
.
I'm using select()
to read from multiple sockets. When I looked up examples on how to use select()
, a lot of tutorials showed going through for loops and checking for FD_ISSET. The problem I have with those tutorials is that the for loop starts from i = 0
. and checks if the bit has been set for the file descriptor i
using FD_ISSET. Couldn't the for loop start from say your minfd (just like how you would keep track of maxfd)?? Or am I missing something??
Following link is an example of such for开发者_Go百科 loop (look at the fourth example that he gives) http://www.developerweb.net/forum/showthread.php?t=2933
If that was the only example out there that used such for loop, I might understand it was a mistake or bad coding but I've seen several examples of such for loop of uselessly going through literally thousands of sockets and I'm convinced I'm missing something. Any comments or inputs are appreciated.
There's a couple of possible reasons:
- If you know that you are always including
stdin
in your list of monitored file descriptors, then you know thatminfd
will always be 0 - so there's no point in specifically keeping track of it; - New file descriptors are always allocated on a lowest-number-available basis - so unless you application behaves very unusually,
minfd
would always be near-zero anyway.
Rather than directly looping over file descriptors though, it's actually more usual to loop over some data structure that contains your file descriptors (along with some metadata) - for example, if you kept your outstanding connections in a linked list:
for (conn = list_head; conn; conn = conn->next)
{
if (FD_ISSET(conn->fd, &readfds))
{
/* Do something ... */
}
}
Well I guess you could keep track of the actual file descriptors and check only those that you know you've added to the set.
Filesystem reuses the smallest available file descriptor and there is always a limit on number of file descriptors that a process can access so I would guess that the maxfd number will not get too large anyway.
Another guess would be that because it was written in C that was the simplest way of implementing the iteration over all file descriptors in the set.
In C++ you can easily create list or vector of file descriptors but in C you would have to add lot more explanation on what you are doing to keep track of file descriptors so to keep it simple they just use the maxfd as upper limit.
My 5c.
精彩评论