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       /* According to POSIX.1-2001, POSIX.1-2008 */
11       #include <sys/select.h>
12
13       /* According to earlier standards */
14       #include <sys/time.h>
15       #include <sys/types.h>
16       #include <unistd.h>
17
18       int select(int nfds, fd_set *readfds, fd_set *writefds,
19                  fd_set *exceptfds, struct timeval *utimeout);
20
21       void FD_CLR(int fd, fd_set *set);
22       int  FD_ISSET(int fd, fd_set *set);
23       void FD_SET(int fd, fd_set *set);
24       void FD_ZERO(fd_set *set);
25
26       #include <sys/select.h>
27
28       int pselect(int nfds, fd_set *readfds, fd_set *writefds,
29                   fd_set *exceptfds, const struct timespec *ntimeout,
30                   const sigset_t *sigmask);
31
32   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
33
34       pselect(): _POSIX_C_SOURCE >= 200112L
35

DESCRIPTION

37       select() (or pselect()) is used to efficiently  monitor  multiple  file
38       descriptors, to see if any of them is, or becomes, "ready"; that is, to
39       see whether I/O becomes possible, or  an  "exceptional  condition"  has
40       occurred on any of the file descriptors.
41
42       Its  principal arguments are three "sets" of file descriptors: readfds,
43       writefds, and exceptfds.  Each set is declared as type fd_set, and  its
44       contents  can  be  manipulated  with  the  macros FD_CLR(), FD_ISSET(),
45       FD_SET(), and FD_ZERO().  A newly declared set should first be  cleared
46       using  FD_ZERO().  select() modifies the contents of the sets according
47       to the rules described below; after calling select() you can test if  a
48       file  descriptor  is  still present in a set with the FD_ISSET() macro.
49       FD_ISSET() returns nonzero if a specified file descriptor is present in
50       a set and zero if it is not.  FD_CLR() removes a file descriptor from a
51       set.
52
53   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              that are immediately available for reading.
59
60       writefds
61              This  set  is  watched to see if there is space to write data to
62              any of its  file  descriptors.   After  select()  has  returned,
63              writefds  will  be  cleared  of  all file descriptors except for
64              those that are immediately available for writing.
65
66       exceptfds
67              This set is watched for "exceptional conditions".  In  practice,
68              only  one such exceptional condition is common: the availability
69              of out-of-band (OOB) data for reading from a  TCP  socket.   See
70              recv(2),  send(2),  and  tcp(7) for more details about OOB data.
71              (One other less common case where select(2) indicates an  excep‐
72              tional condition occurs with pseudoterminals in packet mode; see
73              ioctl_tty(2).)  After select() has returned, exceptfds  will  be
74              cleared  of  all  file descriptors except for those for which an
75              exceptional condition has occurred.
76
77       nfds   This is an integer  one  more  than  the  maximum  of  any  file
78              descriptor  in  any  of  the sets.  In other words, while adding
79              file descriptors to each of the sets,  you  must  calculate  the
80              maximum  integer value of all of them, then increment this value
81              by one, and then pass this as nfds.
82
83       utimeout
84              This is the longest time select()  may  wait  before  returning,
85              even  if  nothing interesting happened.  If this value is passed
86              as NULL, then select() blocks indefinitely waiting  for  a  file
87              descriptor  to  become  ready.  utimeout can be set to zero sec‐
88              onds, which causes select() to return immediately, with informa‐
89              tion  about the readiness of file descriptors at the time of the
90              call.  The structure struct timeval is defined as:
91
92                  struct timeval {
93                      time_t tv_sec;    /* seconds */
94                      long tv_usec;     /* microseconds */
95                  };
96
97       ntimeout
98              This argument for pselect() has the same  meaning  as  utimeout,
99              but struct timespec has nanosecond precision as follows:
100
101                  struct timespec {
102                      long tv_sec;    /* seconds */
103                      long tv_nsec;   /* nanoseconds */
104                  };
105
106       sigmask
107              This  argument  holds  a  set  of signals that the kernel should
108              unblock (i.e., remove  from  the  signal  mask  of  the  calling
109              thread),  while  the caller is blocked inside the pselect() call
110              (see sigaddset(3) and sigprocmask(2)).  It may be NULL, in which
111              case  the call does not modify the signal mask on entry and exit
112              to the function.  In this case, pselect() will then behave  just
113              like select().
114
115   Combining signal and data events
116       pselect() is useful if you are waiting for a signal as well as for file
117       descriptor(s) to become ready for I/O.  Programs that  receive  signals
118       normally  use  the  signal  handler  only  to raise a global flag.  The
119       global flag will indicate that the event must be processed in the  main
120       loop  of  the program.  A signal will cause the select() (or pselect())
121       call to return with errno set to EINTR.  This behavior is essential  so
122       that  signals  can be processed in the main loop of the program, other‐
123       wise select() would block indefinitely.  Now,  somewhere  in  the  main
124       loop  will  be a conditional to check the global flag.  So we must ask:
125       what if a signal arrives after the conditional, but before the select()
126       call?   The  answer  is  that  select()  would block indefinitely, even
127       though an event is actually pending.  This race condition is solved  by
128       the  pselect() call.  This call can be used to set the signal mask to a
129       set of signals that are to be received only within the pselect()  call.
130       For  instance,  let us say that the event in question was the exit of a
131       child process.  Before the start of  the  main  loop,  we  would  block
132       SIGCHLD  using sigprocmask(2).  Our pselect() call would enable SIGCHLD
133       by using an empty signal mask.  Our program would look like:
134
135       static volatile sig_atomic_t got_SIGCHLD = 0;
136
137       static void
138       child_sig_handler(int sig)
139       {
140           got_SIGCHLD = 1;
141       }
142
143       int
144       main(int argc, char *argv[])
145       {
146           sigset_t sigmask, empty_mask;
147           struct sigaction sa;
148           fd_set readfds, writefds, exceptfds;
149           int r;
150
151           sigemptyset(&sigmask);
152           sigaddset(&sigmask, SIGCHLD);
153           if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {
154               perror("sigprocmask");
155               exit(EXIT_FAILURE);
156           }
157
158           sa.sa_flags = 0;
159           sa.sa_handler = child_sig_handler;
160           sigemptyset(&sa.sa_mask);
161           if (sigaction(SIGCHLD, &sa, NULL) == -1) {
162               perror("sigaction");
163               exit(EXIT_FAILURE);
164           }
165
166           sigemptyset(&empty_mask);
167
168           for (;;) {          /* main loop */
169               /* Initialize readfds, writefds, and exceptfds
170                  before the pselect() call. (Code omitted.) */
171
172               r = pselect(nfds, &readfds, &writefds, &exceptfds,
173                           NULL, &empty_mask);
174               if (r == -1 && errno != EINTR) {
175                   /* Handle error */
176               }
177
178               if (got_SIGCHLD) {
179                   got_SIGCHLD = 0;
180
181                   /* Handle signalled event here; e.g., wait() for all
182                      terminated children. (Code omitted.) */
183               }
184
185               /* main body of program */
186           }
187       }
188
189   Practical
190       So what is the point of select()?  Can't I just read and  write  to  my
191       file  descriptors  whenever  I  want?  The point of select() is that it
192       watches multiple descriptors at the same time  and  properly  puts  the
193       process  to sleep if there is no activity.  UNIX programmers often find
194       themselves in a position where they have to handle I/O from  more  than
195       one  file  descriptor  where the data flow may be intermittent.  If you
196       were to merely create a sequence of read(2)  and  write(2)  calls,  you
197       would  find that one of your calls may block waiting for data from/to a
198       file descriptor, while another file descriptor is unused  though  ready
199       for I/O.  select() efficiently copes with this situation.
200
201   Select law
202       Many people who try to use select() come across behavior that is diffi‐
203       cult to understand and produces nonportable or borderline results.  For
204       instance,  the  above  program is carefully written not to block at any
205       point, even though it does not set its file descriptors to  nonblocking
206       mode.   It  is  easy  to  introduce  subtle errors that will remove the
207       advantage of using select(), so here is a list of essentials  to  watch
208       for when using select().
209
210       1.  You should always try to use select() without a timeout.  Your pro‐
211           gram should have nothing to do if there is no data available.  Code
212           that  depends  on timeouts is not usually portable and is difficult
213           to debug.
214
215       2.  The value nfds  must  be  properly  calculated  for  efficiency  as
216           explained above.
217
218       3.  No file descriptor must be added to any set if you do not intend to
219           check its result after the select()  call,  and  respond  appropri‐
220           ately.  See next rule.
221
222       4.  After  select() returns, all file descriptors in all sets should be
223           checked to see if they are ready.
224
225       5.  The functions read(2), recv(2), write(2), and send(2) do not neces‐
226           sarily  read/write the full amount of data that you have requested.
227           If they do read/write the full amount, it's because you have a  low
228           traffic load and a fast stream.  This is not always going to be the
229           case.  You should cope with the case of your functions managing  to
230           send or receive only a single byte.
231
232       6.  Never  read/write  only  in  single  bytes at a time unless you are
233           really sure that you have a small amount of data to process.  It is
234           extremely  inefficient  not  to  read/write as much data as you can
235           buffer each time.  The buffers in the example below are 1024  bytes
236           although they could easily be made larger.
237
238       7.  Calls to read(2), recv(2), write(2), send(2), and select() can fail
239           with the error EINTR, and calls to read(2), recv(2)  write(2),  and
240           send(2)  can  fail  with  errno set to EAGAIN (EWOULDBLOCK).  These
241           results must be properly managed (not  done  properly  above).   If
242           your  program  is  not  going  to  receive  any signals, then it is
243           unlikely you will get EINTR.  If your program  does  not  set  non‐
244           blocking I/O, you will not get EAGAIN.
245
246       8.  Never  call  read(2),  recv(2),  write(2), or send(2) with a buffer
247           length of zero.
248
249       9.  If the functions read(2), recv(2), write(2), and send(2) fail  with
250           errors other than those listed in 7., or one of the input functions
251           returns 0, indicating end of file, then you should  not  pass  that
252           file  descriptor  to select() again.  In the example below, I close
253           the file descriptor immediately, and then set it to -1  to  prevent
254           it being included in a set.
255
256       10. The  timeout  value  must  be  initialized  with  each  new call to
257           select(), since some operating systems modify the structure.   pse‐
258           lect() however does not modify its timeout structure.
259
260       11. Since  select()  modifies  its file descriptor sets, if the call is
261           being used in a loop, then the sets must  be  reinitialized  before
262           each call.
263
264   Usleep emulation
265       On systems that do not have a usleep(3) function, you can call select()
266       with a finite timeout and no file descriptors as follows:
267
268           struct timeval tv;
269           tv.tv_sec = 0;
270           tv.tv_usec = 200000;  /* 0.2 seconds */
271           select(0, NULL, NULL, NULL, &tv);
272
273       This is guaranteed to work only on UNIX systems, however.
274

RETURN VALUE

276       On success, select() returns the total number of file descriptors still
277       present in the file descriptor sets.
278
279       If  select()  timed  out, then the return value will be zero.  The file
280       descriptors set should be all empty (but may not be on some systems).
281
282       A return value of -1 indicates an error, with errno being set appropri‐
283       ately.   In the case of an error, the contents of the returned sets and
284       the struct timeout contents are undefined and should not be used.  pse‐
285       lect() however never modifies ntimeout.
286

NOTES

288       Generally  speaking,  all  operating  systems that support sockets also
289       support select().  select() can be used to solve  many  problems  in  a
290       portable  and  efficient  way  that naive programmers try to solve in a
291       more complicated manner using threads, forking, IPCs,  signals,  memory
292       sharing, and so on.
293
294       The  poll(2) system call has the same functionality as select(), and is
295       somewhat more efficient when monitoring sparse  file  descriptor  sets.
296       It  is  nowadays  widely  available, but historically was less portable
297       than select().
298
299       The Linux-specific epoll(7) API provides  an  interface  that  is  more
300       efficient  than  select(2) and poll(2) when monitoring large numbers of
301       file descriptors.
302

EXAMPLE

304       Here is an  example  that  better  demonstrates  the  true  utility  of
305       select().   The listing below is a TCP forwarding program that forwards
306       from one TCP port to another.
307
308       #include <stdlib.h>
309       #include <stdio.h>
310       #include <unistd.h>
311       #include <sys/time.h>
312       #include <sys/types.h>
313       #include <string.h>
314       #include <signal.h>
315       #include <sys/socket.h>
316       #include <netinet/in.h>
317       #include <arpa/inet.h>
318       #include <errno.h>
319
320       static int forward_port;
321
322       #undef max
323       #define max(x,y) ((x) > (y) ? (x) : (y))
324
325       static int
326       listen_socket(int listen_port)
327       {
328           struct sockaddr_in addr;
329           int lfd;
330           int yes;
331
332           lfd = socket(AF_INET, SOCK_STREAM, 0);
333           if (lfd == -1) {
334               perror("socket");
335               return -1;
336           }
337
338           yes = 1;
339           if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,
340                   &yes, sizeof(yes)) == -1) {
341               perror("setsockopt");
342               close(lfd);
343               return -1;
344           }
345
346           memset(&addr, 0, sizeof(addr));
347           addr.sin_port = htons(listen_port);
348           addr.sin_family = AF_INET;
349           if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
350               perror("bind");
351               close(lfd);
352               return -1;
353           }
354
355           printf("accepting connections on port %d\n", listen_port);
356           listen(lfd, 10);
357           return lfd;
358       }
359
360       static int
361       connect_socket(int connect_port, char *address)
362       {
363           struct sockaddr_in addr;
364           int cfd;
365
366           cfd = socket(AF_INET, SOCK_STREAM, 0);
367           if (cfd == -1) {
368               perror("socket");
369               return -1;
370           }
371
372           memset(&addr, 0, sizeof(addr));
373           addr.sin_port = htons(connect_port);
374           addr.sin_family = AF_INET;
375
376           if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {
377               fprintf(stderr, "inet_aton(): bad IP address format\n");
378               close(cfd);
379               return -1;
380           }
381
382           if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
383               perror("connect()");
384               shutdown(cfd, SHUT_RDWR);
385               close(cfd);
386               return -1;
387           }
388           return cfd;
389       }
390
391       #define SHUT_FD1 do {                                \
392                            if (fd1 >= 0) {                 \
393                                shutdown(fd1, SHUT_RDWR);   \
394                                close(fd1);                 \
395                                fd1 = -1;                   \
396                            }                               \
397                        } while (0)
398
399       #define SHUT_FD2 do {                                \
400                            if (fd2 >= 0) {                 \
401                                shutdown(fd2, SHUT_RDWR);   \
402                                close(fd2);                 \
403                                fd2 = -1;                   \
404                            }                               \
405                        } while (0)
406
407       #define BUF_SIZE 1024
408
409       int
410       main(int argc, char *argv[])
411       {
412           int h;
413           int fd1 = -1, fd2 = -1;
414           char buf1[BUF_SIZE], buf2[BUF_SIZE];
415           int buf1_avail = 0, buf1_written = 0;
416           int buf2_avail = 0, buf2_written = 0;
417
418           if (argc != 4) {
419               fprintf(stderr, "Usage\n\tfwd <listen-port> "
420                        "<forward-to-port> <forward-to-ip-address>\n");
421               exit(EXIT_FAILURE);
422           }
423
424           signal(SIGPIPE, SIG_IGN);
425
426           forward_port = atoi(argv[2]);
427
428           h = listen_socket(atoi(argv[1]));
429           if (h == -1)
430               exit(EXIT_FAILURE);
431
432           for (;;) {
433               int ready, nfds = 0;
434               ssize_t nbytes;
435               fd_set readfds, writefds, exceptfds;
436
437               FD_ZERO(&readfds);
438               FD_ZERO(&writefds);
439               FD_ZERO(&exceptfds);
440               FD_SET(h, &readfds);
441               nfds = max(nfds, h);
442
443               if (fd1 > 0 && buf1_avail < BUF_SIZE)
444                   FD_SET(fd1, &readfds);
445                   /* Note: nfds is updated below, when fd1 is added to
446                      exceptfds. */
447               if (fd2 > 0 && buf2_avail < BUF_SIZE)
448                   FD_SET(fd2, &readfds);
449
450               if (fd1 > 0 && buf2_avail - buf2_written > 0)
451                   FD_SET(fd1, &writefds);
452               if (fd2 > 0 && buf1_avail - buf1_written > 0)
453                   FD_SET(fd2, &writefds);
454
455               if (fd1 > 0) {
456                   FD_SET(fd1, &exceptfds);
457                   nfds = max(nfds, fd1);
458               }
459               if (fd2 > 0) {
460                   FD_SET(fd2, &exceptfds);
461                   nfds = max(nfds, fd2);
462               }
463
464               ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);
465
466               if (ready == -1 && errno == EINTR)
467                   continue;
468
469               if (ready == -1) {
470                   perror("select()");
471                   exit(EXIT_FAILURE);
472               }
473
474               if (FD_ISSET(h, &readfds)) {
475                   socklen_t addrlen;
476                   struct sockaddr_in client_addr;
477                   int fd;
478
479                   addrlen = sizeof(client_addr);
480                   memset(&client_addr, 0, addrlen);
481                   fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);
482                   if (fd == -1) {
483                       perror("accept()");
484                   } else {
485                       SHUT_FD1;
486                       SHUT_FD2;
487                       buf1_avail = buf1_written = 0;
488                       buf2_avail = buf2_written = 0;
489                       fd1 = fd;
490                       fd2 = connect_socket(forward_port, argv[3]);
491                       if (fd2 == -1)
492                           SHUT_FD1;
493                       else
494                           printf("connect from %s\n",
495                                   inet_ntoa(client_addr.sin_addr));
496
497                       /* Skip any events on the old, closed file descriptors. */
498                       continue;
499                   }
500               }
501
502               /* NB: read OOB data before normal reads */
503
504               if (fd1 > 0 && FD_ISSET(fd1, &exceptfds)) {
505                   char c;
506
507                   nbytes = recv(fd1, &c, 1, MSG_OOB);
508                   if (nbytes < 1)
509                       SHUT_FD1;
510                   else
511                       send(fd2, &c, 1, MSG_OOB);
512               }
513               if (fd2 > 0 && FD_ISSET(fd2, &exceptfds)) {
514                   char c;
515
516                   nbytes = recv(fd2, &c, 1, MSG_OOB);
517                   if (nbytes < 1)
518                       SHUT_FD2;
519                   else
520                       send(fd1, &c, 1, MSG_OOB);
521               }
522               if (fd1 > 0 && FD_ISSET(fd1, &readfds)) {
523                   nbytes = read(fd1, buf1 + buf1_avail,
524                             BUF_SIZE - buf1_avail);
525                   if (nbytes < 1)
526                       SHUT_FD1;
527                   else
528                       buf1_avail += nbytes;
529               }
530               if (fd2 > 0 && FD_ISSET(fd2, &readfds)) {
531                   nbytes = read(fd2, buf2 + buf2_avail,
532                             BUF_SIZE - buf2_avail);
533                   if (nbytes < 1)
534                       SHUT_FD2;
535                   else
536                       buf2_avail += nbytes;
537               }
538               if (fd1 > 0 && FD_ISSET(fd1, &writefds) && buf2_avail > 0) {
539                   nbytes = write(fd1, buf2 + buf2_written,
540                              buf2_avail - buf2_written);
541                   if (nbytes < 1)
542                       SHUT_FD1;
543                   else
544                       buf2_written += nbytes;
545               }
546               if (fd2 > 0 && FD_ISSET(fd2, &writefds) && buf1_avail > 0) {
547                   nbytes = write(fd2, buf1 + buf1_written,
548                              buf1_avail - buf1_written);
549                   if (nbytes < 1)
550                       SHUT_FD2;
551                   else
552                       buf1_written += nbytes;
553               }
554
555               /* Check if write data has caught read data */
556
557               if (buf1_written == buf1_avail)
558                   buf1_written = buf1_avail = 0;
559               if (buf2_written == buf2_avail)
560                   buf2_written = buf2_avail = 0;
561
562               /* One side has closed the connection, keep
563                  writing to the other side until empty */
564
565               if (fd1 < 0 && buf1_avail - buf1_written == 0)
566                   SHUT_FD2;
567               if (fd2 < 0 && buf2_avail - buf2_written == 0)
568                   SHUT_FD1;
569           }
570           exit(EXIT_SUCCESS);
571       }
572
573       The above program properly  forwards  most  kinds  of  TCP  connections
574       including  OOB  signal  data transmitted by telnet servers.  It handles
575       the tricky problem of having data flow in  both  directions  simultane‐
576       ously.   You  might  think  it more efficient to use a fork(2) call and
577       devote a thread to each stream.  This  becomes  more  tricky  than  you
578       might  suspect.  Another idea is to set nonblocking I/O using fcntl(2).
579       This also has its problems because you end up using  inefficient  time‐
580       outs.
581
582       The  program does not handle more than one simultaneous connection at a
583       time, although it could easily be extended to do  this  with  a  linked
584       list  of  buffers—one  for each connection.  At the moment, new connec‐
585       tions cause the current connection to be dropped.
586

SEE ALSO

588       accept(2), connect(2), ioctl(2), poll(2), read(2), recv(2),  select(2),
589       send(2),  sigprocmask(2), write(2), sigaddset(3), sigdelset(3), sigemp‐
590       tyset(3), sigfillset(3), sigismember(3), epoll(7)
591

COLOPHON

593       This page is part of release 5.04 of the Linux  man-pages  project.   A
594       description  of  the project, information about reporting bugs, and the
595       latest    version    of    this    page,    can     be     found     at
596       https://www.kernel.org/doc/man-pages/.
597
598
599
600Linux                             2019-03-06                     SELECT_TUT(2)
Impressum