[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: select()



Hi Nikhil

Please find the reply to your queries embedded in your mail below.

On Tue, 25 Jul 2000 nikhilwiz@xxxxxxxxx wrote:

> hi. can anyone tell me what's the significance of the first param of
> select() - nfds. when we're passing the fds to be watched anyway thru
> FD_SET(). looking at the kernel source (2.4.0-test2), in do_select(),
> there is a loop from 0 to nfds, checking _every_ fd. Is this
> necessary, even when the fds to be watched are explicitly provided by
> FD_SET()?

yes, the first argument is very important. This is the only way to know
how many members are there in the fd_set array you have provided. Please
note, in C (unlike java) there is no function call that will give you the
number of elements in the array, when the array is passed as an
argument. It may lead to memory violation.

Consider this piece of code:
int arr[10];

main() 
{
	int i;

	for (i=0;i<12;i++)
		print ("%d\n", arr[i]);
}

This will lead to unpredictable values for the last two iterations.
Remember the arguments of the main() function??
argc and argv
here argc is the count of the number of elements in argv[]

> 
> set_current_state(TASK_INTERRUPTIBLE);
> 
> at /usr/src/linux/fs/select.c:193 do_select() 
> 
> this saves a little loss of face, but i still dont get the point
> behind looping thru all the fds. If i wanted to watch 2 fds with
> comparitively high margin between them, select() would loop
> unnecessarily thru the intermediary fds, which seems like a waste of
> time to me. plz. tell me if i'm wrong somewhere...

Don't have the sources at hand, so can't comment on this one right now.

~Nitin

> 
> Nikhil.
> 
> ----------------------------------------------------------------------
> For information on this and other Linux India mailing lists check out
> http://lists.linux-india.org/
> 

                  _____/~-=##=-~\_____                                       
          -=+0+=-< Nitin Kumar Gupta  >-=+0+=-                               
                  ~~~~~\_-=##=-_/~~~~~