3

I have written a chat server using C on Linux. I have tested the same and it works fine with respect to performance. The only thing which lags is that I am using select system call for handling of sockets descriptors. Since select has the limit of 1024 so at max my chat server can handle only 1024 users concurrently.

I know that the other option which I can use is poll, but not so sure about it and its performance as compared to select.

Please suggest me the most effective way by which I can resolve this situation.

user1457812
  • 31
  • 1
  • 3
  • 2
    possible duplicate of [Handling more than 1024 file descriptors, in C on Linux](http://stackoverflow.com/questions/848717/handling-more-than-1024-file-descriptors-in-c-on-linux) – Christian.K Jun 15 '12 at 05:10
  • 2
    definately use epoll over select – jxh Jun 15 '12 at 05:16
  • The link provide above by Christian.K definetly provides a solution to this issue. – alk Jun 15 '12 at 07:59
  • Where in my program I can re-define __FD_SETSIZE because fdset takes it from system file – user1457812 Jun 29 '12 at 14:27

3 Answers3

5

poll() can be used as an almost drop-in replacement for select(), and will allow you to exceed 1024 file descriptors (you can make make the array passed to poll() as large as you want).

It will have similar performance characteristics to select(), since both require the kernel and userspace application to scan the entire array - but if select() is working OK for you, then poll() should too. (There is actually a slight performance improvement in poll() - the .events field, specifying the events you are interested in for each file descriptor, is not changed by poll(), so you don't have to rebuild the array before every call like you do with the file descriptor sets passed to select()).

If you later find yourself having performance problems caused by scanning the poll file descriptor array, you can consider switching to the epoll interface, which is more complicated but also scales better with very large numbers of file descriptors.

caf
  • 233,326
  • 40
  • 323
  • 462
3

Your question is known as the C10K problem (how to deal with more than 10 thousands simultaneous connections). You'll find lot of resources on the web, e.g. this one.

And you should consider select as an obsolete system call. Even with only dozens of file descriptors, you should at least prefer poll

Notice that Qt and Gtk provide you with an event loop machinery, often using poll (and QtCore or Glib can be used outside of graphical interfaces). There is also libev and libevent. I suggest using one of them.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Linux has no 1024 limit on select(). But:

  1. select() performance is very poor
  2. FreeBSD does :)

Your can use poll(). But its performance suffers when number of active connections increases.

Using epoll() is preferable on Linux however I would suggest to use libevent

libevent is fast, clean and portable way to implement heavy loaded servers and for linux it has epoll under the hood.

eldarko
  • 39
  • 2
  • Thats a poor wording. Indeed the 1024 limit can be increased when redefining the size of the FDSET macro during compile time. But this will not work well with any library. So it's best to not go this way, it might be worse giving hard to detect errors . – Lothar May 25 '22 at 12:58