Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
They report different things in the return value. 'select' tells you the total number of 'ready' file descriptors - it doesn't tell you if any are for read, write, etc. - you need to check your FD sets for that. 'poll' will say something is ready to be read, write will not block, etc. 'poll' is a bit like calling 'select' with a write_FDS and read_FDS where both the read and write FDS are the same.
The basic difference is that select()'s fd_set is a bit mask and
therefore has some fixed size. It would be possible for the kernel to
not limit this size when the kernel is compiled, allowing the
application to define FD_SETSIZE to whatever it wants (as the comments
in the system header imply today) but it takes more work. 4.4BSD's
kernel and the Solaris library function both have this limit. But I
see that BSD/OS 2.1 has now been coded to avoid this limit, so it's
doable, just a small matter of programming. :-) Someone should file a
Solaris bug report on this, and see if it ever gets fixed.
With poll(), however, the user must allocate an array of pollfd
structures, and pass the number of entries in this array, so there's
no fundamental limit. As Casper notes, fewer systems have poll() than
select, so the latter is more portable. Also, with original
implementations (SVR3) you could not set the descriptor to -1 to tell
the kernel to ignore an entry in the pollfd structure, which made it
hard to remove entries from the array; SVR4 gets around this.
Personally, I always use select() and rarely poll(), because I port my
code to BSD environments too. Someone could write an implementation
of poll() that uses select(), for these environments, but I've never
seen one. Both select() and poll() are being standardized by POSIX
Does that answer your question well enough?
I searched the same thing a while back, when trying to figure out which one to use in my multisocket program, and have to admit that knowing almost nothing about those at first it proved a little difficult to make the choice back then
Like it says there (and apparently a bunch of other places as well), both can be used for the same tasks, so it's up to the programmer to choose one. I think I read somewhere that poll() can notify you about more events than select() can, so it could have "broader usability"; though that's a limited benefit, as it probably depends on the implementation (what you need). Some programs even use both; if there's poll() available, the program may make use of it, and if there isn't, do the job with select() instead. And there are probably other means as well that can achieve the same results that poll() and select() can, those two just happen to be the "standardized" ways I think.