1
votes

i got some legacy code using: nonblocking socket, select for timeout, read(2) and write(2). Now it occasionally failed due to select/1024 fd limits. so i need to replace the select.

It seems RCVTIMEO and SNDTIMEO can also check timeout but they work for blocking mode, and it impacts too much to change from non-blocking to blocking.

So is there any other best practice to check timeout for nonblocking socket(no select)? Or i have to get some timer/nanosleep to solve this?

3
To go with the answer from ciphor, check out linux.die.net/man/7/epollSome programmer dude
It is worth mentioning that your OS also has a file descriptor limit which is probably set to 1024. I also recommend something like libev.Eric des Courtis
@JoachimPileborg see my comments on Andy Rossxgwang
@EricdesCourtis limits.comf? i got this done.xgwang
Regarding the comment from Eric, the limit of 1024 descriptors to select is actually often hard-coded (search for FD_SETSIZE). This means that changing the system limit often isn't enough, but you have to #define FD_SETSIZE before including <sys/select.h>.Some programmer dude

3 Answers

4
votes

poll() is essentially a drop-in replacement for using select(), but does not have the 1024 file descriptor limit. You will have to change your code slightly to create an array of struct pollfd structures instead of using fd_sets, but the overall structure of the code should not have to change.

3
votes

epoll is a better solution than select, which is not limited to 1024 descriptors.

In fact, you can use libevent or libev to handle low level asynchronous socket I/O, they are the so-called "best practice" for async I/O.

2
votes

The poll() system call will take a timeout and doesn't have a fixed limit of file descriptors. If you really have 1000 open descriptors though, you would probably be better served by epoll(), which is more complicated to use but has much better scaling characteristics.