I/O Multiplexing (select vs. poll vs. epoll/kqueue)

https://news.ycombinator.com/rss Hits: 14
Summary

I/O multiplexing refers to the concept of processing multiple input/output events from a single event loop, with system calls like poll and select (Unix). –WikipediaIntroductionkqueue(on MacOS) and epoll(on Linux) are kernel system calls for scalable I/O event notification mechanisms in an efficient manner. In simple words, you subscribe to certain kernel events and you get notified when any of those events occur. These system calls are desigend for scalable situations such as a webserver where 10,000 concurrent connections are being handled by one server.epoll/kqueue are replacements for their deprecated counterparts poll and select. Let’s take a look at those two and see why they are not suitable for today’s use cases.selectIn order to watch one file descriptor, say 777, you need to write something like this in C:fd_set fds; FD_ZERO(&fds); FD_SET(777, &fds); select(778, &fds, NULL, NULL, NULL);When you call select, you pass nfds = 778 which is the “number of file descriptors”. What select does under the hood is, it simply loops through all the file descriptors from 0 to nfds - 1. It checks if you have set them and if the desired event occured. So, the runtime is O(n) where n is the largest file descriptor you’re watching!This line of the man page, explains it well: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”.)Aside from performance, a bigger problem is, using select you may destroy your call stack and crash your process! Let’s again look at how we set our desired file descriptor:fd_set fds; FD_ZERO(&fds); FD_SET(777, &fds);fds is the set of file descriptors we want to watch, and according to the man page:The descriptor sets are stored as bit fields in arrays of integers.It also later states that:The default size FD_SETSIZE (currently 1024) is somewhat smaller than th...

First seen: 2025-10-12 06:17

Last seen: 2025-10-12 19:19