Linux: select() really isn’t broken
Tuesday 24 March 2009 - Filed under Code
Oh, apparently I fail today. Having used select() extensively under Windows, where it is only useful for sockets, I thought this would be a great way to try multiplexing serial interfaces (/dev/tty*) in my embedded Linux box.
The problem: select() never signaled readability for a file descriptor which I knew was readable.
The non-solution: screwing around with all kinds of termios crap, trying to make sure the tty was in the most raw mode possible. If anyone else is interested, this is adequate:
$ stty –F /dev/ttyUSB0 115200 raw
The other non-solution: screwing around trying to see what stuff *did* work with select(). Specifically, descriptors 0 and 1. Odd, but explainable by this:
The real solution: carefully reading the man page. Specifically, this section:
The first nfds descriptors are checked in each set; i.e., the descriptors from 0 through nfds-1 in the descriptor sets are examined. (Example: If you have set two file descriptors "4" and "17", nfds should not be "2", but rather "17 + 1" or "18".)
Yeah.
Select() isn’t broken. Again.
2009-03-24 » admin
25 March 2009 @ 2:45 am
select() /is/ broken, just not for the case you describe. select() cannot handle more than 1000 fds, and it services those fds unfairly. Much better to use epoll.
25 March 2009 @ 7:50 am
I’d also suggest looking at the poll(2) interface- it’s generally more sane (although less portable) than select(2).