select_tut (2) - Linux Manuals
select_tut: synchronous I/O multiplexing
NAME
select, pselect - synchronous I/O multiplexing
SYNOPSIS
See select(2)DESCRIPTION
The select() and pselect() system calls are used to efficiently monitor multiple file descriptors, to see if any of them is, or becomes, "ready"; that is, to see whether I/O becomes possible, or an "exceptional condition" has occurred on any of the file descriptors.This page provides background and tutorial information on the use of these system calls. For details of the arguments and semantics of select() and pselect(), see select(2).
Combining signal and data events
pselect() is useful if you are waiting for a signal as well as for file descriptor(s) to become ready for I/O. Programs that receive signals normally use the signal handler only to raise a global flag. The global flag will indicate that the event must be processed in the main loop of the program. A signal will cause the select() (or pselect()) call to return with errno set to EINTR. This behavior is essential so that signals can be processed in the main loop of the program, otherwise select() would block indefinitely.Now, somewhere in the main loop will be a conditional to check the global flag. So we must ask: what if a signal arrives after the conditional, but before the select() call? The answer is that select() would block indefinitely, even though an event is actually pending. This race condition is solved by the pselect() call. This call can be used to set the signal mask to a set of signals that are to be received only within the pselect() call. For instance, let us say that the event in question was the exit of a child process. Before the start of the main loop, we would block SIGCHLD using sigprocmask(2). Our pselect() call would enable SIGCHLD by using an empty signal mask. Our program would look like:
static volatile sig_atomic_t got_SIGCHLD = 0;
static void
child_sig_handler(int sig)
{
int
main(int argc, char *argv[])
{
The
poll(2)
system call has the same functionality as
select(),
and is somewhat more efficient when monitoring sparse
file descriptor sets.
It is nowadays widely available, but historically was less portable than
select().
The Linux-specific
epoll(7)
API provides an interface that is more efficient than
select(2)
and
poll(2)
when monitoring large numbers of file descriptors.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
static int forward_port;
#undef max
#define max(x,y) ((x) > (y) ? (x) : (y))
static int
listen_socket(int listen_port)
{
Practical
So what is the point of
select()?
Can't I just read and write to my file descriptors whenever I want?
The point of
select()
is that it watches
multiple descriptors at the same time and properly puts the process to
sleep if there is no activity.
UNIX programmers often find
themselves in a position where they have to handle I/O from more than one
file descriptor where the data flow may be intermittent.
If you were to merely create a sequence of
read(2)
and
write(2)
calls, you would
find that one of your calls may block waiting for data from/to a file
descriptor, while another file descriptor is unused though ready for I/O.
select()
efficiently copes with this situation.
Select law
Many people who try to use
select()
come across behavior that is
difficult to understand and produces nonportable or borderline results.
For instance, the above program is carefully written not to
block at any point, even though it does not set its file descriptors to
nonblocking mode.
It is easy to introduce
subtle errors that will remove the advantage of using
select(),
so here is a list of essentials to watch for when using
select().
RETURN VALUE
See
select(2).
NOTES
Generally speaking,
all operating systems that support sockets also support
select().
select()
can be used to solve
many problems in a portable and efficient way that naive programmers try
to solve in a more complicated manner using
threads, forking, IPCs, signals, memory sharing, and so on.
EXAMPLES
Here is an example that better demonstrates the true utility of
select().
The listing below is a TCP forwarding program that forwards
from one TCP port to another.