Using select()
Explore how to implement event-based concurrency using the select() API to monitor multiple network sockets for incoming data. Understand the use of FD_ZERO, FD_SET, FD_ISSET macros to manage descriptor sets and how to avoid blocking calls to maintain server responsiveness. This lesson helps you grasp core concepts that prevent concurrency bugs by leveraging single-threaded event-driven server design.
We'll cover the following...
To make this more concrete, let’s examine how to use select() to see which network descriptors have incoming messages upon them. The code excerpt below shows a simple example.
This code is actually fairly simple to understand. After some initialization, the server enters an infinite loop. Inside the loop, it uses the FD_ZERO() macro to first clear the set of file descriptors and then uses FD_SET() to include all of the file descriptors from minFD to maxFD in the set. This set of descriptors might represent, for example, all of the network sockets to which the server is paying attention. ...