Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C select() function deprecated?

Tags:

c

io

posix-select

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?

like image 340
Miroslav Trninic Avatar asked Dec 16 '22 06:12

Miroslav Trninic


2 Answers

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.

like image 149
Karoly Horvath Avatar answered Jan 05 '23 00:01

Karoly Horvath


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:

  • libevent
  • libev
  • libuv

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.

like image 21
hdante Avatar answered Jan 04 '23 22:01

hdante