4

I am reading a book about network progamming in C. It is from 2004. In the example code, author is using select C function to accept multiple connections from the client. Is that function deprecated today?

I see that there are different ways to accept multiplexed I/O like poll and epoll. What are the advantages?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Miroslav Trninic
  • 3,327
  • 4
  • 28
  • 51
  • What makes you think that `select()` is deprecated? It's still defined in the latest POSIX specification. – Flow Sep 15 '13 at 13:34
  • I was wondering the same. select under linux is limited to monitoring the first 1024 file descriptors, even if there is only one descriptor to monitor that is equal or above 1024 it will really go wrong.Due to thread usage file descriptors are shared among all thread, then what was dedidated to process is now cumulated for all thread in same process. Using select with programs requiring to handle more than 1024 connections require then to use forking processes and not use threads. – philippe lhardy Apr 05 '14 at 09:51

2 Answers2

8

It's not deprecated, and lots of programs rely on it.

It's just not the best tool as it has some limitations:

  • The number of file descriptors is limited (OS specific, usually possible to increase it with kernel recompiling).
  • Doesn't scale well (with lots of fds): the whole FD set must be maintained, and re-initialized as select manipulates it.

Feel free to use it if these aren't relevant for you. Otherwise use poll/libevent if you're looking for a cross-platform solution, or in some rare-cases epoll/kqueue for platform specific optimized solutions.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 3
    The fact that many programs rely on something does not mean it will never be deprecated. It means it is unlikely to be removed. Deprecation just means recommending something be avoided. There is no inconsistency between recommending something be avoided and it being relied on in many programs. – Eric Postpischil Sep 15 '13 at 14:04
  • @EricPostpischil: can you name *anything* from C that was ever deprecated in the last 10 years? – Karoly Horvath Sep 15 '13 at 14:07
  • That is a non sequitur. I do not argue about whether or not `select` will be deprecated. I take issue with the implication that “Lots of programs relying on it” means something will never be deprecated. – Eric Postpischil Sep 15 '13 at 14:10
  • Yes, you I right, but I guess the OP was worried that being depreceted means that's at one point it's not going to be *supported* anymore. I was trying refer to that kind of deprecation process. – Karoly Horvath Sep 15 '13 at 14:14
  • @KarolyHorvath see gethostbyname man under linux : The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() functions are obsolete. Applications should use getaddrinfo(3), getname‐info(3), and gai_strerror(3) instead. BUT i don't know exactly WHEN it was deprecated ( was it before 2004 or not ... ). – philippe lhardy Apr 05 '14 at 09:54
  • @philippelhardy: yeah. As I said before, I see zero chance that even these will be removed in the next 10 years. I regularly see these functions in fresh code here on SO, because people simply copy old solutions. – Karoly Horvath Apr 05 '14 at 14:20
4

It's not deprecated in its behavior, but its design may have performance issues. For example, linux epoll() documentation states:

API can be used either as an edge-triggered or a level-triggered inter‐ face and scales well to large numbers of watched file descriptors.

Since the efficient alternatives are specific to each operating system, an option better than directly using select() is to use a cross platform multiplexing library (which uses the best implementation available), examples being:

If you're developing for a specific operating system, use the recommended implementation for high performance applications.

However, since some people don't like current libraries for I/O multiplexing (due to "being ugly"), select is still a viable alternative.

Bernardo Ramos
  • 4,048
  • 30
  • 28
hdante
  • 7,685
  • 3
  • 31
  • 36