1SELECT_TUT(2) Linux Programmer's Manual SELECT_TUT(2)
2
3
4
6 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O
7 multiplexing
8
10 /* According to POSIX.1-2001 */
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 || _XOPEN_SOURCE >= 600
35
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 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 pseudo-terminals in packet mode;
73 see tty_ioctl(4).) After select() has returned, exceptfds will
74 be 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 only to be received 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 descriptors whenever I want? The point of select() is that it watches
192 multiple descriptors at the same time and properly puts the process to
193 sleep if there is no activity. Unix programmers often find themselves
194 in a position where they have to handle I/O from more than one file
195 descriptor where the data flow may be intermittent. If you were to
196 merely create a sequence of read(2) and write(2) calls, you would find
197 that one of your calls may block waiting for data from/to a file
198 descriptor, while another file descriptor is unused though ready for
199 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 only manag‐
230 ing to send or receive 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. The functions read(2), recv(2), write(2), and send(2) as well as
239 the select() call can return -1 with errno set to EINTR, or with
240 errno set to EAGAIN (EWOULDBLOCK). These results must be properly
241 managed (not done properly above). If your program is not going to
242 receive any signals, then it is unlikely you will get EINTR. If
243 your program does not set nonblocking I/O, you will not get EAGAIN.
244
245 8. Never call read(2), recv(2), write(2), or send(2) with a buffer
246 length of zero.
247
248 9. If the functions read(2), recv(2), write(2), and send(2) fail with
249 errors other than those listed in 7., or one of the input functions
250 returns 0, indicating end of file, then you should not pass that
251 descriptor to select() again. In the example below, I close the
252 descriptor immediately, and then set it to -1 to prevent it being
253 included in a set.
254
255 10. The timeout value must be initialized with each new call to
256 select(), since some operating systems modify the structure. pse‐
257 lect() however does not modify its timeout structure.
258
259 11. Since select() modifies its file descriptor sets, if the call is
260 being used in a loop, then the sets must be reinitialized before
261 each call.
262
263 Usleep Emulation
264 On systems that do not have a usleep(3) function, you can call select()
265 with a finite timeout and no file descriptors as follows:
266
267 struct timeval tv;
268 tv.tv_sec = 0;
269 tv.tv_usec = 200000; /* 0.2 seconds */
270 select(0, NULL, NULL, NULL, &tv);
271
272 This is only guaranteed to work on Unix systems, however.
273
275 On success, select() returns the total number of file descriptors still
276 present in the file descriptor sets.
277
278 If select() timed out, then the return value will be zero. The file
279 descriptors set should be all empty (but may not be on some systems).
280
281 A return value of -1 indicates an error, with errno being set appropri‐
282 ately. In the case of an error, the contents of the returned sets and
283 the struct timeout contents are undefined and should not be used. pse‐
284 lect() however never modifies ntimeout.
285
287 Generally speaking, all operating systems that support sockets also
288 support select(). select() can be used to solve many problems in a
289 portable and efficient way that naive programmers try to solve in a
290 more complicated manner using threads, forking, IPCs, signals, memory
291 sharing, and so on.
292
293 The poll(2) system call has the same functionality as select(), and is
294 somewhat more efficient when monitoring sparse file descriptor sets.
295 It is nowadays widely available, but historically was less portable
296 than select().
297
298 The Linux-specific epoll(7) API provides an interface that is more
299 efficient than select(2) and poll(2) when monitoring large numbers of
300 file descriptors.
301
303 Here is an example that better demonstrates the true utility of
304 select(). The listing below is a TCP forwarding program that forwards
305 from one TCP port to another.
306
307 #include <stdlib.h>
308 #include <stdio.h>
309 #include <unistd.h>
310 #include <sys/time.h>
311 #include <sys/types.h>
312 #include <string.h>
313 #include <signal.h>
314 #include <sys/socket.h>
315 #include <netinet/in.h>
316 #include <arpa/inet.h>
317 #include <errno.h>
318
319 static int forward_port;
320
321 #undef max
322 #define max(x,y) ((x) > (y) ? (x) : (y))
323
324 static int
325 listen_socket(int listen_port)
326 {
327 struct sockaddr_in a;
328 int s;
329 int yes;
330
331 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
332 perror("socket");
333 return -1;
334 }
335 yes = 1;
336 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
337 (char *) &yes, sizeof(yes)) == -1) {
338 perror("setsockopt");
339 close(s);
340 return -1;
341 }
342 memset(&a, 0, sizeof(a));
343 a.sin_port = htons(listen_port);
344 a.sin_family = AF_INET;
345 if (bind(s, (struct sockaddr *) &a, sizeof(a)) == -1) {
346 perror("bind");
347 close(s);
348 return -1;
349 }
350 printf("accepting connections on port %d\n", listen_port);
351 listen(s, 10);
352 return s;
353 }
354
355 static int
356 connect_socket(int connect_port, char *address)
357 {
358 struct sockaddr_in a;
359 int s;
360
361 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
362 perror("socket");
363 close(s);
364 return -1;
365 }
366
367 memset(&a, 0, sizeof(a));
368 a.sin_port = htons(connect_port);
369 a.sin_family = AF_INET;
370
371 if (!inet_aton(address, (struct in_addr *) &a.sin_addr.s_addr)) {
372 perror("bad IP address format");
373 close(s);
374 return -1;
375 }
376
377 if (connect(s, (struct sockaddr *) &a, sizeof(a)) == -1) {
378 perror("connect()");
379 shutdown(s, SHUT_RDWR);
380 close(s);
381 return -1;
382 }
383 return s;
384 }
385
386 #define SHUT_FD1 do { \
387 if (fd1 >= 0) { \
388 shutdown(fd1, SHUT_RDWR); \
389 close(fd1); \
390 fd1 = -1; \
391 } \
392 } while (0)
393
394 #define SHUT_FD2 do { \
395 if (fd2 >= 0) { \
396 shutdown(fd2, SHUT_RDWR); \
397 close(fd2); \
398 fd2 = -1; \
399 } \
400 } while (0)
401
402 #define BUF_SIZE 1024
403
404 int
405 main(int argc, char *argv[])
406 {
407 int h;
408 int fd1 = -1, fd2 = -1;
409 char buf1[BUF_SIZE], buf2[BUF_SIZE];
410 int buf1_avail, buf1_written;
411 int buf2_avail, buf2_written;
412
413 if (argc != 4) {
414 fprintf(stderr, "Usage\n\tfwd <listen-port> "
415 "<forward-to-port> <forward-to-ip-address>\n");
416 exit(EXIT_FAILURE);
417 }
418
419 signal(SIGPIPE, SIG_IGN);
420
421 forward_port = atoi(argv[2]);
422
423 h = listen_socket(atoi(argv[1]));
424 if (h == -1)
425 exit(EXIT_FAILURE);
426
427 for (;;) {
428 int r, nfds = 0;
429 fd_set rd, wr, er;
430
431 FD_ZERO(&rd);
432 FD_ZERO(&wr);
433 FD_ZERO(&er);
434 FD_SET(h, &rd);
435 nfds = max(nfds, h);
436 if (fd1 > 0 && buf1_avail < BUF_SIZE) {
437 FD_SET(fd1, &rd);
438 nfds = max(nfds, fd1);
439 }
440 if (fd2 > 0 && buf2_avail < BUF_SIZE) {
441 FD_SET(fd2, &rd);
442 nfds = max(nfds, fd2);
443 }
444 if (fd1 > 0 && buf2_avail - buf2_written > 0) {
445 FD_SET(fd1, &wr);
446 nfds = max(nfds, fd1);
447 }
448 if (fd2 > 0 && buf1_avail - buf1_written > 0) {
449 FD_SET(fd2, &wr);
450 nfds = max(nfds, fd2);
451 }
452 if (fd1 > 0) {
453 FD_SET(fd1, &er);
454 nfds = max(nfds, fd1);
455 }
456 if (fd2 > 0) {
457 FD_SET(fd2, &er);
458 nfds = max(nfds, fd2);
459 }
460
461 r = select(nfds + 1, &rd, &wr, &er, NULL);
462
463 if (r == -1 && errno == EINTR)
464 continue;
465
466 if (r == -1) {
467 perror("select()");
468 exit(EXIT_FAILURE);
469 }
470
471 if (FD_ISSET(h, &rd)) {
472 unsigned int l;
473 struct sockaddr_in client_address;
474
475 memset(&client_address, 0, l = sizeof(client_address));
476 r = accept(h, (struct sockaddr *) &client_address, &l);
477 if (r == -1) {
478 perror("accept()");
479 } else {
480 SHUT_FD1;
481 SHUT_FD2;
482 buf1_avail = buf1_written = 0;
483 buf2_avail = buf2_written = 0;
484 fd1 = r;
485 fd2 = connect_socket(forward_port, argv[3]);
486 if (fd2 == -1)
487 SHUT_FD1;
488 else
489 printf("connect from %s\n",
490 inet_ntoa(client_address.sin_addr));
491 }
492 }
493
494 /* NB: read oob data before normal reads */
495
496 if (fd1 > 0)
497 if (FD_ISSET(fd1, &er)) {
498 char c;
499
500 r = recv(fd1, &c, 1, MSG_OOB);
501 if (r < 1)
502 SHUT_FD1;
503 else
504 send(fd2, &c, 1, MSG_OOB);
505 }
506 if (fd2 > 0)
507 if (FD_ISSET(fd2, &er)) {
508 char c;
509
510 r = recv(fd2, &c, 1, MSG_OOB);
511 if (r < 1)
512 SHUT_FD2;
513 else
514 send(fd1, &c, 1, MSG_OOB);
515 }
516 if (fd1 > 0)
517 if (FD_ISSET(fd1, &rd)) {
518 r = read(fd1, buf1 + buf1_avail,
519 BUF_SIZE - buf1_avail);
520 if (r < 1)
521 SHUT_FD1;
522 else
523 buf1_avail += r;
524 }
525 if (fd2 > 0)
526 if (FD_ISSET(fd2, &rd)) {
527 r = read(fd2, buf2 + buf2_avail,
528 BUF_SIZE - buf2_avail);
529 if (r < 1)
530 SHUT_FD2;
531 else
532 buf2_avail += r;
533 }
534 if (fd1 > 0)
535 if (FD_ISSET(fd1, &wr)) {
536 r = write(fd1, buf2 + buf2_written,
537 buf2_avail - buf2_written);
538 if (r < 1)
539 SHUT_FD1;
540 else
541 buf2_written += r;
542 }
543 if (fd2 > 0)
544 if (FD_ISSET(fd2, &wr)) {
545 r = write(fd2, buf1 + buf1_written,
546 buf1_avail - buf1_written);
547 if (r < 1)
548 SHUT_FD2;
549 else
550 buf1_written += r;
551 }
552
553 /* check if write data has caught read data */
554
555 if (buf1_written == buf1_avail)
556 buf1_written = buf1_avail = 0;
557 if (buf2_written == buf2_avail)
558 buf2_written = buf2_avail = 0;
559
560 /* one side has closed the connection, keep
561 writing to the other side until empty */
562
563 if (fd1 < 0 && buf1_avail - buf1_written == 0)
564 SHUT_FD2;
565 if (fd2 < 0 && buf2_avail - buf2_written == 0)
566 SHUT_FD1;
567 }
568 exit(EXIT_SUCCESS);
569 }
570
571 The above program properly forwards most kinds of TCP connections
572 including OOB signal data transmitted by telnet servers. It handles
573 the tricky problem of having data flow in both directions simultane‐
574 ously. You might think it more efficient to use a fork(2) call and
575 devote a thread to each stream. This becomes more tricky than you
576 might suspect. Another idea is to set nonblocking I/O using fcntl(2).
577 This also has its problems because you end up using inefficient time‐
578 outs.
579
580 The program does not handle more than one simultaneous connection at a
581 time, although it could easily be extended to do this with a linked
582 list of buffers — one for each connection. At the moment, new connec‐
583 tions cause the current connection to be dropped.
584
586 accept(2), connect(2), ioctl(2), poll(2), read(2), recv(2), select(2),
587 send(2), sigprocmask(2), write(2), sigaddset(3), sigdelset(3), sigemp‐
588 tyset(3), sigfillset(3), sigismember(3), epoll(7)
589
591 This page is part of release 3.25 of the Linux man-pages project. A
592 description of the project, and information about reporting bugs, can
593 be found at http://www.kernel.org/doc/man-pages/.
594
595
596
597Linux 2010-06-10 SELECT_TUT(2)