1SELECT_TUT(2)              Linux Programmer's Manual             SELECT_TUT(2)
2
3
4

NAME

6       select,  pselect,  FD_CLR,  FD_ISSET, FD_SET, FD_ZERO - synchronous I/O
7       multiplexing
8

SYNOPSIS

10       #include <sys/time.h>
11       #include <sys/types.h>
12       #include <unistd.h>
13
14       int  select(int  nfds,  fd_set  *readfds,  fd_set   *writefds,   fd_set
15       *exceptfds, struct timeval *utimeout);
16
17       int   pselect(int  nfds,  fd_set  *readfds,  fd_set  *writefds,  fd_set
18       *exceptfds, const struct timespec *ntimeout, sigset_t *sigmask);
19
20       FD_CLR(int fd, fd_set *set);
21       FD_ISSET(int fd, fd_set *set);
22       FD_SET(int fd, fd_set *set);
23       FD_ZERO(fd_set *set);
24

DESCRIPTION

26       select() (or pselect()) is the pivot function of most C  programs  that
27       handle more than one simultaneous file descriptor (or socket handle) in
28       an efficient manner. Its principal arguments are three arrays  of  file
29       descriptors: readfds, writefds, and exceptfds. The way that select() is
30       usually used is to block while waiting for a "change of status" on  one
31       or  more  of  the  file  descriptors. A "change of status" is when more
32       characters become available from the file  descriptor,  or  when  space
33       becomes  available  within the kernel's internal buffers for more to be
34       written to the file descriptor, or when a  file  descriptor  goes  into
35       error  (in  the  case of a socket or pipe this is when the other end of
36       the connection is closed).
37
38       In summary, select() just watches multiple file descriptors, and is the
39       standard Unix call to do so.
40
41       The  arrays  of file descriptors are called file descriptor sets.  Each
42       set is declared as type fd_set, and its contents can  be  altered  with
43       the macros FD_CLR(), FD_ISSET(), FD_SET(),  and FD_ZERO(). FD_ZERO() is
44       usually the first function to be used on a newly declared  set.  There‐
45       after,  the  individual file descriptors that you are interested in can
46       be added one by one with FD_SET().  select() modifies the  contents  of
47       the sets according to the rules described below; after calling select()
48       you can test if your file descriptor is still present in the  set  with
49       the FD_ISSET() macro.  FD_ISSET() returns non-zero if the descriptor is
50       present and zero if it is not. FD_CLR() removes a file descriptor  from
51       the set.
52

ARGUMENTS

54       readfds
55              This set is watched to see if data is available for reading from
56              any of  its  file  descriptors.  After  select()  has  returned,
57              readfds will be cleared of all file descriptors except for those
58              file descriptors that are immediately available for reading with
59              a recv() (for sockets) or read() (for pipes, files, and sockets)
60              call.
61
62       writefds
63              This set is watched to see if there is space to  write  data  to
64              any  of  its  file  descriptors.   After  select() has returned,
65              writefds will be cleared of  all  file  descriptors  except  for
66              those  file descriptors that are immediately available for writ‐
67              ing with a send() (for sockets) or write()  (for  pipes,  files,
68              and sockets) call.
69
70       exceptfds
71              This  set is watched for exceptions or errors on any of the file
72              descriptors. However, that is actually just a rumor. How you use
73              exceptfds  is  to  watch for out-of-band (OOB) data. OOB data is
74              data sent  on  a  socket  using  the  MSG_OOB  flag,  and  hence
75              exceptfds  only  really  applies  to  sockets.  See  recv(2) and
76              send(2) about this. After select() has returned, exceptfds  will
77              be  cleared of all file descriptors except for those descriptors
78              that are available for reading OOB data. You can only ever  read
79              one  byte  of  OOB  data though (which is done with recv()), and
80              writing OOB data (done with send()) can be done at any time  and
81              will not block. Hence there is no need for a fourth set to check
82              if a socket is available for writing OOB data.
83
84       nfds   This is an integer  one  more  than  the  maximum  of  any  file
85              descriptor  in  any  of  the sets. In other words, while you are
86              busy adding file descriptors to your sets,  you  must  calculate
87              the  maximum  integer  value of all of them, then increment this
88              value by one, and then pass this as nfds to select().
89
90       utimeout
91              This is the longest time select() must  wait  before  returning,
92              even if nothing interesting happened. If this value is passed as
93              NULL, then select() blocks indefinitely waiting  for  an  event.
94              utimeout  can  be  set to zero seconds, which causes select() to
95              return immediately. The structure struct timeval is defined as,
96
97              struct timeval {
98                  time_t tv_sec;    /* seconds */
99                  long tv_usec;     /* microseconds */
100              };
101
102       ntimeout
103              This argument has the same meaning as utimeout but struct  time‐
104              spec has nanosecond precision as follows,
105
106              struct timespec {
107                  long tv_sec;    /* seconds */
108                  long tv_nsec;   /* nanoseconds */
109              };
110
111       sigmask
112              This argument holds a set of signals to allow while performing a
113              pselect() call (see sigaddset(3) and sigprocmask(2)).  It can be
114              passed  as  NULL,  in  which  case it does not modify the set of
115              allowed signals on entry and exit to the function. It will  then
116              behave just like select().
117

COMBINING SIGNAL AND DATA EVENTS

119       pselect()  must be used if you are waiting for a signal as well as data
120       from a file descriptor. Programs that receive signals  as  events  nor‐
121       mally  use  the  signal handler only to raise a global flag. The global
122       flag will indicate that the event must be processed in the main loop of
123       the  program.  A  signal will cause the select() (or pselect()) call to
124       return with errno set to EINTR. This behavior is essential so that sig‐
125       nals  can  be  processed  in  the  main  loop of the program, otherwise
126       select() would block indefinitely. Now, somewhere in the main loop will
127       be  a  conditional  to check the global flag. So we must ask: what if a
128       signal arrives after the conditional, but before the select() call? The
129       answer  is that select() would block indefinitely, even though an event
130       is actually pending. This race condition is  solved  by  the  pselect()
131       call.  This  call  can  be  used to mask out signals that are not to be
132       received except within the pselect() call. For  instance,  let  us  say
133       that  the event in question was the exit of a child process. Before the
134       start of the main loop, we would block SIGCHLD using sigprocmask(). Our
135       pselect()  call  would  enable SIGCHLD by using the virgin signal mask.
136       Our program would look like:
137
138       int child_events = 0;
139
140       void child_sig_handler (int x) {
141           child_events++;
142           signal (SIGCHLD, child_sig_handler);
143       }
144
145       int main (int argc, char **argv) {
146           sigset_t sigmask, orig_sigmask;
147
148           sigemptyset (&sigmask);
149           sigaddset (&sigmask, SIGCHLD);
150           sigprocmask (SIG_BLOCK, &sigmask,
151                                       &orig_sigmask);
152
153           signal (SIGCHLD, child_sig_handler);
154
155           for (;;) { /* main loop */
156               for (; child_events > 0; child_events--) {
157                   /* do event work here */
158               }
159               r = pselect (nfds, &rd, &wr, &er, 0, &orig_sigmask);
160
161               /* main body of program */
162           }
163       }
164

PRACTICAL

166       So what is the point of select()? Can't I just read  and  write  to  my
167       descriptors  whenever I want?  The point of select() is that it watches
168       multiple descriptors at the same time and properly puts the process  to
169       sleep  if there is no activity. It does this while enabling you to han‐
170       dle multiple simultaneous pipes and  sockets.  Unix  programmers  often
171       find  themselves  in a position where they have to handle I/O from more
172       than one file descriptor where the data flow may  be  intermittent.  If
173       you  were  to merely create a sequence of read() and write() calls, you
174       would find that one of your calls may block waiting for data from/to  a
175       file  descriptor, while another file descriptor is unused though avail‐
176       able for data. select() efficiently copes with this situation.
177
178       A simple example of the use of select() can be found in  the  select(2)
179       manual page.
180

PORT FORWARDING EXAMPLE

182       Here  is  an  example  that  better  demonstrates  the  true utility of
183       select().  The listing below is a TCP forwarding program that  forwards
184       from one TCP port to another.
185
186       #include <stdlib.h>
187       #include <stdio.h>
188       #include <unistd.h>
189       #include <sys/time.h>
190       #include <sys/types.h>
191       #include <string.h>
192       #include <signal.h>
193       #include <sys/socket.h>
194       #include <netinet/in.h>
195       #include <arpa/inet.h>
196       #include <errno.h>
197
198       static int forward_port;
199
200       #undef max
201       #define max(x,y) ((x) > (y) ? (x) : (y))
202
203       static int listen_socket (int listen_port) {
204           struct sockaddr_in a;
205           int s;
206           int yes;
207           if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
208               perror ("socket");
209               return -1;
210           }
211           yes = 1;
212           if (setsockopt
213               (s, SOL_SOCKET, SO_REUSEADDR,
214                (char *) &yes, sizeof (yes)) < 0) {
215               perror ("setsockopt");
216               close (s);
217               return -1;
218           }
219           memset (&a, 0, sizeof (a));
220           a.sin_port = htons (listen_port);
221           a.sin_family = AF_INET;
222           if (bind
223               (s, (struct sockaddr *) &a, sizeof (a)) < 0) {
224               perror ("bind");
225               close (s);
226               return -1;
227           }
228           printf ("accepting connections on port %d\n",
229                   (int) listen_port);
230           listen (s, 10);
231           return s;
232       }
233
234       static int connect_socket (int connect_port,
235                                  char *address) {
236           struct sockaddr_in a;
237           int s;
238           if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
239               perror ("socket");
240               close (s);
241               return -1;
242           }
243
244           memset (&a, 0, sizeof (a));
245           a.sin_port = htons (connect_port);
246           a.sin_family = AF_INET;
247
248           if (!inet_aton
249               (address,
250                (struct in_addr *) &a.sin_addr.s_addr)) {
251               perror ("bad IP address format");
252               close (s);
253               return -1;
254           }
255
256           if (connect
257               (s, (struct sockaddr *) &a,
258                sizeof (a)) < 0) {
259               perror ("connect()");
260               shutdown (s, SHUT_RDWR);
261               close (s);
262               return -1;
263           }
264           return s;
265       }
266
267       #define SHUT_FD1 {                      \
268               if (fd1 >= 0) {                 \
269                   shutdown (fd1, SHUT_RDWR);  \
270                   close (fd1);                \
271                   fd1 = -1;                   \
272               }                               \
273           }
274
275       #define SHUT_FD2 {                      \
276               if (fd2 >= 0) {                 \
277                   shutdown (fd2, SHUT_RDWR);  \
278                   close (fd2);                \
279                   fd2 = -1;                   \
280               }                               \
281           }
282
283       #define BUF_SIZE 1024
284
285       int main (int argc, char **argv) {
286           int h;
287           int fd1 = -1, fd2 = -1;
288           char buf1[BUF_SIZE], buf2[BUF_SIZE];
289           int buf1_avail, buf1_written;
290           int buf2_avail, buf2_written;
291
292           if (argc != 4) {
293               fprintf (stderr,
294                        "Usage\n\tfwd <listen-port> \
295       <forward-to-port> <forward-to-ip-address>\n");
296               exit (1);
297           }
298
299           signal (SIGPIPE, SIG_IGN);
300
301           forward_port = atoi (argv[2]);
302
303           h = listen_socket (atoi (argv[1]));
304           if (h < 0)
305               exit (1);
306
307           for (;;) {
308               int r, nfds = 0;
309               fd_set rd, wr, er;
310               FD_ZERO (&rd);
311               FD_ZERO (&wr);
312               FD_ZERO (&er);
313               FD_SET (h, &rd);
314               nfds = max (nfds, h);
315               if (fd1 > 0 && buf1_avail < BUF_SIZE) {
316                   FD_SET (fd1, &rd);
317                   nfds = max (nfds, fd1);
318               }
319               if (fd2 > 0 && buf2_avail < BUF_SIZE) {
320                   FD_SET (fd2, &rd);
321                   nfds = max (nfds, fd2);
322               }
323               if (fd1 > 0
324                   && buf2_avail - buf2_written > 0) {
325                   FD_SET (fd1, &wr);
326                   nfds = max (nfds, fd1);
327               }
328               if (fd2 > 0
329                   && buf1_avail - buf1_written > 0) {
330                   FD_SET (fd2, &wr);
331                   nfds = max (nfds, fd2);
332               }
333               if (fd1 > 0) {
334                   FD_SET (fd1, &er);
335                   nfds = max (nfds, fd1);
336               }
337               if (fd2 > 0) {
338                   FD_SET (fd2, &er);
339                   nfds = max (nfds, fd2);
340               }
341
342               r = select (nfds + 1, &rd, &wr, &er, NULL);
343
344               if (r == -1 && errno == EINTR)
345                   continue;
346               if (r < 0) {
347                   perror ("select()");
348                   exit (1);
349               }
350               if (FD_ISSET (h, &rd)) {
351                   unsigned int l;
352                   struct sockaddr_in client_address;
353                   memset (&client_address, 0, l =
354                           sizeof (client_address));
355                   r = accept (h, (struct sockaddr *)
356                               &client_address, &l);
357                   if (r < 0) {
358                       perror ("accept()");
359                   } else {
360                       SHUT_FD1;
361                       SHUT_FD2;
362                       buf1_avail = buf1_written = 0;
363                       buf2_avail = buf2_written = 0;
364                       fd1 = r;
365                       fd2 =
366                           connect_socket (forward_port,
367                                           argv[3]);
368                       if (fd2 < 0) {
369                           SHUT_FD1;
370                       } else
371                           printf ("connect from %s\n",
372                                   inet_ntoa
373                                   (client_address.sin_addr));
374                   }
375               }
376       /* NB: read oob data before normal reads */
377               if (fd1 > 0)
378                   if (FD_ISSET (fd1, &er)) {
379                       char c;
380                       errno = 0;
381                       r = recv (fd1, &c, 1, MSG_OOB);
382                       if (r < 1) {
383                           SHUT_FD1;
384                       } else
385                           send (fd2, &c, 1, MSG_OOB);
386                   }
387               if (fd2 > 0)
388                   if (FD_ISSET (fd2, &er)) {
389                       char c;
390                       errno = 0;
391                       r = recv (fd2, &c, 1, MSG_OOB);
392                       if (r < 1) {
393                           SHUT_FD1;
394                       } else
395                           send (fd1, &c, 1, MSG_OOB);
396                   }
397               if (fd1 > 0)
398                   if (FD_ISSET (fd1, &rd)) {
399                       r =
400                           read (fd1, buf1 + buf1_avail,
401                                 BUF_SIZE - buf1_avail);
402                       if (r < 1) {
403                           SHUT_FD1;
404                       } else
405                           buf1_avail += r;
406                   }
407               if (fd2 > 0)
408                   if (FD_ISSET (fd2, &rd)) {
409                       r =
410                           read (fd2, buf2 + buf2_avail,
411                                 BUF_SIZE - buf2_avail);
412                       if (r < 1) {
413                           SHUT_FD2;
414                       } else
415                           buf2_avail += r;
416                   }
417               if (fd1 > 0)
418                   if (FD_ISSET (fd1, &wr)) {
419                       r =
420                           write (fd1,
421                                  buf2 + buf2_written,
422                                  buf2_avail -
423                                  buf2_written);
424                       if (r < 1) {
425                           SHUT_FD1;
426                       } else
427                           buf2_written += r;
428                   }
429               if (fd2 > 0)
430                   if (FD_ISSET (fd2, &wr)) {
431                       r =
432                           write (fd2,
433                                  buf1 + buf1_written,
434                                  buf1_avail -
435                                  buf1_written);
436                       if (r < 1) {
437                           SHUT_FD2;
438                       } else
439                           buf1_written += r;
440                   }
441       /* check if write data has caught read data */
442               if (buf1_written == buf1_avail)
443                   buf1_written = buf1_avail = 0;
444               if (buf2_written == buf2_avail)
445                   buf2_written = buf2_avail = 0;
446       /* one side has closed the connection, keep
447          writing to the other side until empty */
448               if (fd1 < 0
449                   && buf1_avail - buf1_written == 0) {
450                   SHUT_FD2;
451               }
452               if (fd2 < 0
453                   && buf2_avail - buf2_written == 0) {
454                   SHUT_FD1;
455               }
456           }
457           return 0;
458       }
459
460       The  above  program  properly  forwards  most  kinds of TCP connections
461       including OOB signal data transmitted by telnet servers. It handles the
462       tricky  problem  of having data flow in both directions simultaneously.
463       You might think it more efficient to use a fork()  call  and  devote  a
464       thread to each stream. This becomes more tricky than you might suspect.
465       Another idea is to set non-blocking I/O using  an  ioctl()  call.  This
466       also  has  its  problems  because you end up having to have inefficient
467       timeouts.
468
469       The program does not handle more than one simultaneous connection at  a
470       time,  although  it  could  easily be extended to do this with a linked
471       list of buffers — one for each connection. At the moment,  new  connec‐
472       tions cause the current connection to be dropped.
473

SELECT LAW

475       Many people who try to use select() come across behavior that is diffi‐
476       cult to understand and produces non-portable or borderline results. For
477       instance,  the  above  program is carefully written not to block at any
478       point, even though it does not set its file descriptors to non-blocking
479       mode  at all (see ioctl(2)). It is easy to introduce subtle errors that
480       will remove the advantage of using select(), hence  I  will  present  a
481       list of essentials to watch for when using the select() call.
482
483       1.     You  should  always  try to use select() without a timeout. Your
484              program should have nothing to do if there is no data available.
485              Code  that  depends  on  timeouts is not usually portable and is
486              difficult to debug.
487
488       2.     The value nfds must be properly  calculated  for  efficiency  as
489              explained above.
490
491       3.     No file descriptor must be added to any set if you do not intend
492              to check its result after the select() call, and respond  appro‐
493              priately. See next rule.
494
495       4.     After  select() returns, all file descriptors in all sets should
496              be checked to see if they are ready.
497
498       5.     The functions read(), recv(), write(), and send() do not  neces‐
499              sarily  read/write  the  full  amount  of  data  that  you  have
500              requested. If they do read/write the full  amount,  its  because
501              you  have  a  low  traffic  load  and a fast stream. This is not
502              always going to be the case. You should cope with  the  case  of
503              your functions only managing to send or receive a single byte.
504
505       6.     Never  read/write only in single bytes at a time unless your are
506              really sure that you have a small amount of data to process.  It
507              is  extremely  inefficient not to read/write as much data as you
508              can buffer each time.  The buffers in the example above are 1024
509              bytes although they could easily be made larger.
510
511       7.     The functions read(), recv(), write(), and send() as well as the
512              select() call can return -1 with errno set  to  EINTR,  or  with
513              errno  set to EAGAIN (EWOULDBLOCK).  These results must be prop‐
514              erly managed (not done properly above). If your program  is  not
515              going  to  receive  any signals then it is unlikely you will get
516              EINTR. If your program does not set non-blocking I/O,  you  will
517              not  get  EAGAIN.  Nonetheless  you should still cope with these
518              errors for completeness.
519
520       8.     Never call read(), recv(), write(),  or  send()  with  a  buffer
521              length of zero.
522
523       9.     If  the  functions read(), recv(), write(), and send() fail with
524              errors other than those listed in 7., or one of the input  func‐
525              tions  returns  0,  indicating  end of file, then you should not
526              pass that descriptor to select() again.  In the above example, I
527              close  the descriptor immediately, and then set it to -1 to pre‐
528              vent it being included in a set.
529
530       10.    The timeout value must be initialized  with  each  new  call  to
531              select(),  since  some  operating  systems modify the structure.
532              pselect() however does not modify its timeout structure.
533
534       11.    I have heard that the Windows socket layer does  not  cope  with
535              OOB  data  properly.  It  also does not cope with select() calls
536              when no file descriptors are set at all. Having no file descrip‐
537              tors  set  is  a useful way to sleep the process with sub-second
538              precision by using the timeout.  (See further on.)
539

USLEEP EMULATION

541       On systems that do not have a usleep() function, you can call  select()
542       with a finite timeout and no file descriptors as follows:
543
544           struct timeval tv;
545           tv.tv_sec = 0;
546           tv.tv_usec = 200000;  /* 0.2 seconds */
547           select (0, NULL, NULL, NULL, &tv);
548
549       This is only guaranteed to work on Unix systems, however.
550

RETURN VALUE

552       On success, select() returns the total number of file descriptors still
553       present in the file descriptor sets.
554
555       If select() timed out, then the return value will be  zero.   The  file
556       descriptors set should be all empty (but may not be on some systems).
557
558       A return value of -1 indicates an error, with errno being set appropri‐
559       ately. In the case of an error,  the  returned  sets  and  the  timeout
560       struct  contents  are undefined and should not be used.  pselect() how‐
561       ever never modifies ntimeout.
562

NOTES

564       Generally speaking, all operating systems that  support  sockets,  also
565       support  select().  Many types of programs become extremely complicated
566       without the use of select().  select() can be used to solve many  prob‐
567       lems  in  a  portable  and  efficient way that naive programmers try to
568       solve in a more complicated manner using threads, forking,  IPCs,  sig‐
569       nals, memory sharing, and so on.
570
571       The  poll(2) system call has the same functionality as select(), and is
572       somewhat more efficient when monitoring sparse  file  descriptor  sets.
573       It  is  nowadays  widely  available, but historically was less portable
574       than select().
575
576       The Linux-specific epoll(7) API provides  an  interface  that  is  more
577       efficient  than  select(2) and poll(2) when monitoring large numbers of
578       file descriptors.
579

SEE ALSO

581       accept(2), connect(2), ioctl(2), poll(2), read(2), recv(2),  select(2),
582       send(2),  sigprocmask(2), write(2), sigaddset(3), sigdelset(3), sigemp‐
583       tyset(3), sigfillset(3), sigismember(3), epoll(7)
584
585
586
587Linux                             2006-05-13                     SELECT_TUT(2)
Impressum