Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shall we use poll() or select()?

I'm fully aware of the major differences between poll() and select():

  • select() only supports a fixed amount of file descriptors
  • select() is supposedly supported on more systems
  • poll() allows slightly more fine-grained control of event types
  • poll() implementations may differ in certain details

However, they both accomplish the same task in roughly the same way. So:

Shall we use poll() or select()?


EDIT: I might add that I'm not interested in epoll() since portability is of concern to me. Furthermore, libev(ent) is not an option either, since I'm asking this question because I'm writing my own replacement library for libev(ent).

like image 736
Philip Avatar asked Dec 08 '11 22:12

Philip


People also ask

Should I use poll or select?

poll offers somewhat more flavors of events to wait for, and to receive, although for most common networked cases they don't add a lot of value. Different timeout values. poll takes milliseconds, select takes a struct timeval pointer that offers microsecond resolution.

What is the difference between select and poll in polling method?

The select() call has you create three bitmasks to mark which sockets and file descriptors you want to watch for reading, writing, and errors, and then the operating system marks which ones in fact have had some kind of activity; poll() has you create a list of descriptor IDs, and the operating system marks each of ...

Is poll faster than select?

poll( ) is more efficient for large-valued file descriptors. Imagine watching a single file descriptor with the value 900 via select()—the kernel would have to check each bit of each passed-in set, up to the 900th bit.

What is select and poll function?

The select() and poll() methods are used for multiplexing network sockets. Find out how to use these functions, and learn about their limitations. The select() and poll() methods can be a powerful tool when you're multiplexing network sockets.


1 Answers

All remotely modern systems have poll, and it's a greatly superior interface to select/pselect in almost all ways:

  • poll allows more fine-grained detection of status than select.
  • poll does not have limits on the max file descriptor you can use (and more importantly, does not have critical vulnerabilities when you fail to check for file descriptors past the FD_SETSIZE limit).

The only disadvantages I can think of to using poll are that:

  • unlike pselect, poll cannot atomically unmask/mask signals, so you can't use it for waiting for a set of events that includes both file descriptor activity and signals unless you resort to the self-pipe trick.
  • poll only has millisecond resolution for the wait timeout, rather than microsecond (select) or nanosecond (pselect).

Certainly portability of poll is not a consideration anymore. Any system old enough to lack poll is full of so many vulnerabilities it should not be connected to a network.

In summary, unless you have very special needs (tiny timeout intervals, nasty signal interactions, scaling to millions of persistent connections, etc.) I would simply use poll and be done with it. As others have mentioned, libevent is also an option, but it's not clean/safe code (its use of select actually invokes dangerous UB trying to workaround the limitations of select!) and I find code that uses libevent is generally a lot more unnecessarily complicated than code that simply uses poll directly.

like image 140
R.. GitHub STOP HELPING ICE Avatar answered Sep 21 '22 05:09

R.. GitHub STOP HELPING ICE