LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2012, 02:39 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
C Network programming, select()


Dear all,
I have a somehow trivial question about network programming in c.

I was reading yesterday the man pages of select()
Code:
int select(int nfds, fd_set *readfds, fd_set *writefds,
                  fd_set *exceptfds, struct timeval *timeout);
and as you can see the first argument is an integer.
According to the man page
Code:
 nfds is the highest-numbered file descriptor in any of the three sets, plus 1.

I am trying to understand two things
1) Why the highest number there is needed? Before using select one has few file descriptors (actually) that want to track. Would not make sense to pass directly those instead of the maximum number. What one probably wants to do is to ask from kernel "Please look my file descriptors X,Y,Z". How kernel handles that when it know the maximum number of those?

2) Why +1 is needed? Would not be more convenient just to give the max number and for the reason (which I do not also understand) the function then to add +1 to the provided number?

I would like to than k you in advance for your help
B.R
Alex
 
Old 05-23-2012, 03:51 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
The file-descriptors are stored in the three bit-vectors (readfds, writefds, exceptfds), the first parameter only informs the kernel how long it has to check to bit-vector.
For example, if you only want to check the standard input (handle==0), then set nfds to 1.
 
Old 05-23-2012, 10:44 AM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
As far as I could tell from looking it up, it's a legacy feature that short-circuits some work.

I guess having select() implemen a MAX(int *fds) on the inputs is too much work?

Sure, maybe it does save a bit of work.

But I think the signature could be reworked into something a little more sensible.
 
Old 05-23-2012, 10:53 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by jhwilliams View Post
But I think the signature could be reworked into something a little more sensible.
I think someone came to the same conclusion long ago, and developed poll(), and then later epoll().

I'm an old-school developer, so I still use select(); it just add a simple line of code to determine the max file descriptor. Something like:
Code:
int maxfd = -1;
...
maxfd = (fd > maxfd ? fd : maxfd);
...
select(maxfd + 1, &readfds, 0, 0, 0);
 
Old 05-23-2012, 06:44 PM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Quote:
Originally Posted by dwhitney67 View Post
I think someone came to the same conclusion long ago, and developed poll(), and then later epoll()
Lol.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket programming select interface virtualCoder Programming 2 01-16-2010 09:52 PM
How to select the right interface with two network devices. mrgreedy Linux - Networking 2 10-20-2008 02:54 PM
Select Active Network at boot? staniel Linux - Networking 1 03-15-2006 05:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration