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 non-zero if a specified file descriptor is present
50 in a set and zero if it is not. FD_CLR() removes a file descriptor
51 from a 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 each of the sets, you must calculate the maxi‐
80 mum integer value of all of them, then increment this value by
81 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 siognal 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 non-portable or borderline results.
204 For instance, the above program is carefully written not to block at
205 any point, even though it does not set its file descriptors to non-
206 blocking mode. It is easy to introduce subtle errors that will remove
207 the advantage of using select(), so here is a list of essentials to
208 watch 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 non-blocking I/O, you will not get
244 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 descriptor to select() again. In the example below, I close the
253 descriptor immediately, and then set it to -1 to prevent it being
254 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 re-initialized 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 only guaranteed to work on Unix systems, however.
274
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
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
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 a;
329 int s;
330 int yes;
331
332 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
333 perror("socket");
334 return -1;
335 }
336 yes = 1;
337 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
338 (char *) &yes, sizeof(yes)) == -1) {
339 perror("setsockopt");
340 close(s);
341 return -1;
342 }
343 memset(&a, 0, sizeof(a));
344 a.sin_port = htons(listen_port);
345 a.sin_family = AF_INET;
346 if (bind(s, (struct sockaddr *) &a, sizeof(a)) == -1) {
347 perror("bind");
348 close(s);
349 return -1;
350 }
351 printf("accepting connections on port %d\n", listen_port);
352 listen(s, 10);
353 return s;
354 }
355
356 static int
357 connect_socket(int connect_port, char *address)
358 {
359 struct sockaddr_in a;
360 int s;
361
362 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
363 perror("socket");
364 close(s);
365 return -1;
366 }
367
368 memset(&a, 0, sizeof(a));
369 a.sin_port = htons(connect_port);
370 a.sin_family = AF_INET;
371
372 if (!inet_aton(address, (struct in_addr *) &a.sin_addr.s_addr)) {
373 perror("bad IP address format");
374 close(s);
375 return -1;
376 }
377
378 if (connect(s, (struct sockaddr *) &a, sizeof(a)) == -1) {
379 perror("connect()");
380 shutdown(s, SHUT_RDWR);
381 close(s);
382 return -1;
383 }
384 return s;
385 }
386
387 #define SHUT_FD1 do { \
388 if (fd1 >= 0) { \
389 shutdown(fd1, SHUT_RDWR); \
390 close(fd1); \
391 fd1 = -1; \
392 } \
393 } while (0)
394
395 #define SHUT_FD2 do { \
396 if (fd2 >= 0) { \
397 shutdown(fd2, SHUT_RDWR); \
398 close(fd2); \
399 fd2 = -1; \
400 } \
401 } while (0)
402
403 #define BUF_SIZE 1024
404
405 int
406 main(int argc, char *argv[])
407 {
408 int h;
409 int fd1 = -1, fd2 = -1;
410 char buf1[BUF_SIZE], buf2[BUF_SIZE];
411 int buf1_avail, buf1_written;
412 int buf2_avail, buf2_written;
413
414 if (argc != 4) {
415 fprintf(stderr, "Usage\n\tfwd <listen-port> "
416 "<forward-to-port> <forward-to-ip-address>\n");
417 exit(EXIT_FAILURE);
418 }
419
420 signal(SIGPIPE, SIG_IGN);
421
422 forward_port = atoi(argv[2]);
423
424 h = listen_socket(atoi(argv[1]));
425 if (h == -1)
426 exit(EXIT_FAILURE);
427
428 for (;;) {
429 int r, nfds = 0;
430 fd_set rd, wr, er;
431
432 FD_ZERO(&rd);
433 FD_ZERO(&wr);
434 FD_ZERO(&er);
435 FD_SET(h, &rd);
436 nfds = max(nfds, h);
437 if (fd1 > 0 && buf1_avail < BUF_SIZE) {
438 FD_SET(fd1, &rd);
439 nfds = max(nfds, fd1);
440 }
441 if (fd2 > 0 && buf2_avail < BUF_SIZE) {
442 FD_SET(fd2, &rd);
443 nfds = max(nfds, fd2);
444 }
445 if (fd1 > 0 && buf2_avail - buf2_written > 0) {
446 FD_SET(fd1, &wr);
447 nfds = max(nfds, fd1);
448 }
449 if (fd2 > 0 && buf1_avail - buf1_written > 0) {
450 FD_SET(fd2, &wr);
451 nfds = max(nfds, fd2);
452 }
453 if (fd1 > 0) {
454 FD_SET(fd1, &er);
455 nfds = max(nfds, fd1);
456 }
457 if (fd2 > 0) {
458 FD_SET(fd2, &er);
459 nfds = max(nfds, fd2);
460 }
461
462 r = select(nfds + 1, &rd, &wr, &er, NULL);
463
464 if (r == -1 && errno == EINTR)
465 continue;
466
467 if (r == -1) {
468 perror("select()");
469 exit(EXIT_FAILURE);
470 }
471
472 if (FD_ISSET(h, &rd)) {
473 unsigned int l;
474 struct sockaddr_in client_address;
475
476 memset(&client_address, 0, l = sizeof(client_address));
477 r = accept(h, (struct sockaddr *) &client_address, &l);
478 if (r == -1) {
479 perror("accept()");
480 } else {
481 SHUT_FD1;
482 SHUT_FD2;
483 buf1_avail = buf1_written = 0;
484 buf2_avail = buf2_written = 0;
485 fd1 = r;
486 fd2 = connect_socket(forward_port, argv[3]);
487 if (fd2 == -1)
488 SHUT_FD1;
489 else
490 printf("connect from %s\n",
491 inet_ntoa(client_address.sin_addr));
492 }
493 }
494
495 /* NB: read oob data before normal reads */
496
497 if (fd1 > 0)
498 if (FD_ISSET(fd1, &er)) {
499 char c;
500
501 r = recv(fd1, &c, 1, MSG_OOB);
502 if (r < 1)
503 SHUT_FD1;
504 else
505 send(fd2, &c, 1, MSG_OOB);
506 }
507 if (fd2 > 0)
508 if (FD_ISSET(fd2, &er)) {
509 char c;
510
511 r = recv(fd2, &c, 1, MSG_OOB);
512 if (r < 1)
513 SHUT_FD1;
514 else
515 send(fd1, &c, 1, MSG_OOB);
516 }
517 if (fd1 > 0)
518 if (FD_ISSET(fd1, &rd)) {
519 r = read(fd1, buf1 + buf1_avail,
520 BUF_SIZE - buf1_avail);
521 if (r < 1)
522 SHUT_FD1;
523 else
524 buf1_avail += r;
525 }
526 if (fd2 > 0)
527 if (FD_ISSET(fd2, &rd)) {
528 r = read(fd2, buf2 + buf2_avail,
529 BUF_SIZE - buf2_avail);
530 if (r < 1)
531 SHUT_FD2;
532 else
533 buf2_avail += r;
534 }
535 if (fd1 > 0)
536 if (FD_ISSET(fd1, &wr)) {
537 r = write(fd1, buf2 + buf2_written,
538 buf2_avail - buf2_written);
539 if (r < 1)
540 SHUT_FD1;
541 else
542 buf2_written += r;
543 }
544 if (fd2 > 0)
545 if (FD_ISSET(fd2, &wr)) {
546 r = write(fd2, buf1 + buf1_written,
547 buf1_avail - buf1_written);
548 if (r < 1)
549 SHUT_FD2;
550 else
551 buf1_written += r;
552 }
553
554 /* check if write data has caught read data */
555
556 if (buf1_written == buf1_avail)
557 buf1_written = buf1_avail = 0;
558 if (buf2_written == buf2_avail)
559 buf2_written = buf2_avail = 0;
560
561 /* one side has closed the connection, keep
562 writing to the other side until empty */
563
564 if (fd1 < 0 && buf1_avail - buf1_written == 0)
565 SHUT_FD2;
566 if (fd2 < 0 && buf2_avail - buf2_written == 0)
567 SHUT_FD1;
568 }
569 exit(EXIT_SUCCESS);
570 }
571
572 The above program properly forwards most kinds of TCP connections
573 including OOB signal data transmitted by telnet servers. It handles
574 the tricky problem of having data flow in both directions simultane‐
575 ously. You might think it more efficient to use a fork(2) call and
576 devote a thread to each stream. This becomes more tricky than you
577 might suspect. Another idea is to set non-blocking I/O using fcntl(2).
578 This also has its problems because you end up using inefficient time‐
579 outs.
580
581 The program does not handle more than one simultaneous connection at a
582 time, although it could easily be extended to do this with a linked
583 list of buffers — one for each connection. At the moment, new connec‐
584 tions cause the current connection to be dropped.
585
587 accept(2), connect(2), ioctl(2), poll(2), read(2), recv(2), select(2),
588 send(2), sigprocmask(2), write(2), sigaddset(3), sigdelset(3), sigemp‐
589 tyset(3), sigfillset(3), sigismember(3), epoll(7)
590
592 This page is part of release 3.22 of the Linux man-pages project. A
593 description of the project, and information about reporting bugs, can
594 be found at http://www.kernel.org/doc/man-pages/.
595
596
597
598Linux 2009-01-26 SELECT_TUT(2)