~/Using the select System Call in C
Jan 18, 2021
The select system call in C monitors multiple file descriptors to see if any are ready for I/O. It is often used for multiplexing sockets or files within a single thread.
Basic usage steps:
- Initialize an fd_set variable.
- Use FD_ZERO, FD_SET, FD_ISSET, FD_CLR macros.
- Set a timeout struct timeval.
- Call select with the highest fd plus one, and pointers to read, write, and except sets.
Minimal example:
|
|
Each set parameter can be NULL if not needed. FD_SET and macros help manage fd_set variables. select returns the number of ready descriptors, 0 for timeout, -1 for error.
Use cases include simple non-blocking socket multiplexing. For large numbers of fds, consider poll or epoll.