1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
|
.\" This manpage is copyright (C) 2001 Paul Sheer.
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" very minor changes, aeb
.\"
.\" Modified 5 June 2002, Michael Kerrisk <mtk-manpages@gmx.net>
.\" 2006-05-13, mtk, removed much material that is redundant with select.2
.\" various other changes
.\"
.TH SELECT_TUT 2 2006-05-13 "Linux" "Linux Programmer's Manual"
.SH NAME
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
synchronous I/O multiplexing
.SH SYNOPSIS
.nf
/* According to POSIX.1-2001 */
.br
.B #include <sys/select.h>
.sp
/* According to earlier standards */
.br
.B #include <sys/time.h>
.br
.B #include <sys/types.h>
.br
.B #include <unistd.h>
.sp
\fBint select(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
fd_set *\fIexceptfds\fB, struct timeval *\fItimeout\fB);
.sp
.BI "void FD_CLR(int " fd ", fd_set *" set );
.br
.BI "int FD_ISSET(int " fd ", fd_set *" set );
.br
.BI "void FD_SET(int " fd ", fd_set *" set );
.br
.BI "void FD_ZERO(fd_set *" set );
.sp
.B #define _XOPEN_SOURCE 600
.B #include <sys/select.h>
.sp
\fBint pselect(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
fd_set *\fIexceptfds\fB, const struct timespec *\fItimeout\fB,
const sigset_t *\fIsigmask\fB);
.fi
.SH DESCRIPTION
\fBselect\fP() (or \fBpselect\fP()) is the pivot function of
most C programs that
handle more than one simultaneous file descriptor (or socket handle)
in an efficient
manner.
Its principal arguments are three arrays of file descriptors:
\fIreadfds\fP, \fIwritefds\fP, and \fIexceptfds\fP.
The way that
\fBselect\fP() is usually used is to block while waiting for a "change of
status" on one or more of the file descriptors.
A "change of status" is
when more characters become available from the file descriptor, \fIor\fP
when space becomes available within the kernel's internal buffers for
more to be written to the file descriptor, \fIor\fP when a file
descriptor goes into error (in the case of a socket or pipe this is
when the other end of the connection is closed).
In summary, \fBselect\fP() just watches multiple file descriptors,
and is the standard Unix call to do so.
The arrays of file descriptors are called \fIfile descriptor sets\fP.
Each set is declared as type \fBfd_set\fP, and its contents can be
altered with the macros \fBFD_CLR\fP(), \fBFD_ISSET\fP(), \fBFD_SET\fP(), and
\fBFD_ZERO\fP(). \fBFD_ZERO\fP() is usually the first function to be used on
a newly declared set.
Thereafter, the individual file descriptors that
you are interested in can be added one by one with \fBFD_SET\fP().
\fBselect\fP() modifies the contents of the sets according to the rules
described below; after calling \fBselect\fP() you can test if your file
descriptor is still present in the set with the \fBFD_ISSET\fP() macro.
\fBFD_ISSET\fP() returns non-zero if the descriptor is present and zero if
it is not. \fBFD_CLR\fP() removes a file descriptor from the set.
.SH ARGUMENTS
.TP
\fIreadfds\fP
This set is watched to see if data is available for reading from any of
its file descriptors.
After \fBselect\fP() has returned, \fIreadfds\fP will be
cleared of all file descriptors except for those file descriptors that
are immediately available for reading with a \fBrecv\fP() (for sockets) or
\fBread\fP() (for pipes, files, and sockets) call.
.TP
\fIwritefds\fP
This set is watched to see if there is space to write data to any of
its file descriptors.
After \fBselect\fP() has returned, \fIwritefds\fP will be
cleared of all file descriptors except for those file descriptors that
are immediately available for writing with a \fBsend\fP() (for sockets) or
\fBwrite\fP() (for pipes, files, and sockets) call.
.TP
\fIexceptfds\fP
This set is watched for exceptions or errors on any of the file
descriptors.
However, that is actually just a rumor.
How you use
\fIexceptfds\fP is to watch for \fIout\-of\-band\fP (OOB) data.
OOB data
is data sent on a socket using the \fBMSG_OOB\fP flag, and hence
\fIexceptfds\fP only really applies to sockets.
See \fBrecv\fP(2) and
\fBsend\fP(2) about this.
After \fBselect\fP() has returned,
\fIexceptfds\fP will be cleared of all file descriptors except for those
descriptors that are available for reading OOB data.
You can only ever
read one byte of OOB data though (which is done with \fBrecv\fP()), and
writing OOB data (done with \fBsend\fP()) can be done at any time and will
not block.
Hence there is no need for a fourth set to check if a socket
is available for writing OOB data.
.TP
\fInfds\fP
This is an integer one more than the maximum of any file descriptor in
any of the sets.
In other words, while you are busy adding file descriptors
to your sets, you must calculate the maximum integer value of all of
them, then increment this value by one, and then pass this as \fInfds\fP to
\fBselect\fP().
.TP
\fIutimeout\fP
.RS
This is the longest time \fBselect\fP() must wait before returning, even
if nothing interesting happened.
If this value is passed as NULL,
then \fBselect\fP() blocks indefinitely waiting for an event.
\fIutimeout\fP can be set to zero seconds, which causes \fBselect\fP() to
return immediately.
The structure \fBstruct timeval\fP is defined as,
.PP
.nf
struct timeval {
time_t tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
.fi
.RE
.TP
\fIntimeout\fP
.RS
This argument has the same meaning as \fIutimeout\fP but \fIstruct timespec\fP
has nanosecond precision as follows,
.PP
.nf
struct timespec {
long tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
.fi
.RE
.TP
\fIsigmask\fP
This argument holds a set of signals to allow while performing a
\fBpselect\fP() call (see \fBsigaddset\fP(3) and \fBsigprocmask\fP(2)).
It can be passed
as NULL, in which case it does not modify the set of allowed signals on
entry and exit to the function.
It will then behave just like \fBselect\fP().
.SH COMBINING SIGNAL AND DATA EVENTS
\fBpselect\fP() must be used if you are waiting for a signal as well as
data from a file descriptor.
Programs that receive signals as events
normally use the signal handler only to raise a global flag.
The global
flag will indicate that the event must be processed in the main loop of
the program.
A signal will cause the \fBselect\fP() (or \fBpselect\fP())
call to return with \fIerrno\fP set to \fBEINTR\fP.
This behavior is
essential so that signals can be processed in the main loop of the
program, otherwise \fBselect\fP() would block indefinitely.
Now, somewhere
in the main loop will be a conditional to check the global flag.
So we
must ask: what if a signal arrives after the conditional, but before the
\fBselect\fP() call? The answer is that \fBselect\fP() would block
indefinitely, even though an event is actually pending.
This race
condition is solved by the \fBpselect\fP() call.
This call can be used to
mask out signals that are not to be received except within the
\fBpselect\fP() call.
For instance, let us say that the event in question
was the exit of a child process.
Before the start of the main loop, we
would block \fBSIGCHLD\fP using \fBsigprocmask\fP().
Our \fBpselect\fP()
call would enable \fBSIGCHLD\fP by using the virgin signal mask.
Our
program would look like:
.PP
.nf
int child_events = 0;
void
child_sig_handler(int x)
{
child_events++;
signal(SIGCHLD, child_sig_handler);
}
int
main(int argc, char **argv)
{
sigset_t sigmask, orig_sigmask;
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &sigmask, &orig_sigmask);
signal(SIGCHLD, child_sig_handler);
for (;;) { /* main loop */
for (; child_events > 0; child_events\-\-) {
/* do event work here */
}
r = pselect(nfds, &rd, &wr, &er, 0, &orig_sigmask);
/* main body of program */
}
}
.fi
.SH PRACTICAL
So what is the point of \fBselect\fP()? Can't I just read and write to my
descriptors whenever I want?
The point of \fBselect\fP() is that it watches
multiple descriptors at the same time and properly puts the process to
sleep if there is no activity.
It does this while enabling you to handle
multiple simultaneous pipes and sockets.
Unix programmers often find
themselves in a position where they have to handle I/O from more than one
file descriptor where the data flow may be intermittent.
If you were to
merely create a sequence of \fBread\fP() and \fBwrite\fP() calls, you would
find that one of your calls may block waiting for data from/to a file
descriptor, while another file descriptor is unused though available
for data. \fBselect\fP() efficiently copes with this situation.
A simple example of the use of
.BR select ()
can be found in the
.BR select (2)
manual page.
.SH PORT FORWARDING EXAMPLE
Here is an example that better demonstrates the true utility of
\fBselect\fP().
The listing below is a TCP forwarding program that forwards
from one TCP port to another.
.PP
.nf
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
static int forward_port;
#undef max
#define max(x,y) ((x) > (y) ? (x) : (y))
static int
listen_socket(int listen_port)
{
struct sockaddr_in a;
int s;
int yes;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return \-1;
}
yes = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(char *) &yes, sizeof(yes)) < 0) {
perror("setsockopt");
close(s);
return \-1;
}
memset(&a, 0, sizeof(a));
a.sin_port = htons(listen_port);
a.sin_family = AF_INET;
if (bind(s, (struct sockaddr *) &a, sizeof(a)) < 0) {
perror("bind");
close(s);
return \-1;
}
printf("accepting connections on port %d\\n", listen_port);
listen(s, 10);
return s;
}
static int
connect_socket(int connect_port, char *address)
{
struct sockaddr_in a;
int s;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
close(s);
return \-1;
}
memset(&a, 0, sizeof(a));
a.sin_port = htons(connect_port);
a.sin_family = AF_INET;
if (!inet_aton(address, (struct in_addr *) &a.sin_addr.s_addr)) {
perror("bad IP address format");
close(s);
return \-1;
}
if (connect(s, (struct sockaddr *) &a, sizeof(a)) < 0) {
perror("connect()");
shutdown(s, SHUT_RDWR);
close(s);
return \-1;
}
return s;
}
#define SHUT_FD1 { \\
if (fd1 >= 0) { \\
shutdown(fd1, SHUT_RDWR); \\
close(fd1); \\
fd1 = \-1; \\
} \\
}
#define SHUT_FD2 { \\
if (fd2 >= 0) { \\
shutdown(fd2, SHUT_RDWR); \\
close(fd2); \\
fd2 = \-1; \\
} \\
}
#define BUF_SIZE 1024
int
main(int argc, char **argv)
{
int h;
int fd1 = \-1, fd2 = \-1;
char buf1[BUF_SIZE], buf2[BUF_SIZE];
int buf1_avail, buf1_written;
int buf2_avail, buf2_written;
if (argc != 4) {
fprintf(stderr,
"Usage\\n\\tfwd <listen-port> "
"<forward-to-port> <forward-to-ip-address>\\n");
exit(1);
}
signal(SIGPIPE, SIG_IGN);
forward_port = atoi(argv[2]);
h = listen_socket(atoi(argv[1]));
if (h < 0)
exit(1);
for (;;) {
int r, nfds = 0;
fd_set rd, wr, er;
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&er);
FD_SET(h, &rd);
nfds = max(nfds, h);
if (fd1 > 0 && buf1_avail < BUF_SIZE) {
FD_SET(fd1, &rd);
nfds = max(nfds, fd1);
}
if (fd2 > 0 && buf2_avail < BUF_SIZE) {
FD_SET(fd2, &rd);
nfds = max(nfds, fd2);
}
if (fd1 > 0
&& buf2_avail \- buf2_written > 0) {
FD_SET(fd1, &wr);
nfds = max(nfds, fd1);
}
if (fd2 > 0
&& buf1_avail \- buf1_written > 0) {
FD_SET(fd2, &wr);
nfds = max(nfds, fd2);
}
if (fd1 > 0) {
FD_SET(fd1, &er);
nfds = max(nfds, fd1);
}
if (fd2 > 0) {
FD_SET(fd2, &er);
nfds = max(nfds, fd2);
}
r = select(nfds + 1, &rd, &wr, &er, NULL);
if (r == \-1 && errno == EINTR)
continue;
if (r < 0) {
perror("select()");
exit(1);
}
if (FD_ISSET(h, &rd)) {
unsigned int l;
struct sockaddr_in client_address;
memset(&client_address, 0, l = sizeof(client_address));
r = accept(h, (struct sockaddr *) &client_address, &l);
if (r < 0) {
perror("accept()");
} else {
SHUT_FD1;
SHUT_FD2;
buf1_avail = buf1_written = 0;
buf2_avail = buf2_written = 0;
fd1 = r;
fd2 =
connect_socket(forward_port, argv[3]);
if (fd2 < 0) {
SHUT_FD1;
} else
printf("connect from %s\\n",
inet_ntoa(client_address.sin_addr));
}
}
/* NB: read oob data before normal reads */
if (fd1 > 0)
if (FD_ISSET(fd1, &er)) {
char c;
errno = 0;
r = recv(fd1, &c, 1, MSG_OOB);
if (r < 1) {
SHUT_FD1;
} else
send(fd2, &c, 1, MSG_OOB);
}
if (fd2 > 0)
if (FD_ISSET(fd2, &er)) {
char c;
errno = 0;
r = recv(fd2, &c, 1, MSG_OOB);
if (r < 1) {
SHUT_FD1;
} else
send(fd1, &c, 1, MSG_OOB);
}
if (fd1 > 0)
if (FD_ISSET(fd1, &rd)) {
r =
read(fd1, buf1 + buf1_avail,
BUF_SIZE \- buf1_avail);
if (r < 1) {
SHUT_FD1;
} else
buf1_avail += r;
}
if (fd2 > 0)
if (FD_ISSET(fd2, &rd)) {
r =
read(fd2, buf2 + buf2_avail,
BUF_SIZE \- buf2_avail);
if (r < 1) {
SHUT_FD2;
} else
buf2_avail += r;
}
if (fd1 > 0)
if (FD_ISSET(fd1, &wr)) {
r =
write(fd1, buf2 + buf2_written,
buf2_avail \- buf2_written);
if (r < 1) {
SHUT_FD1;
} else
buf2_written += r;
}
if (fd2 > 0)
if (FD_ISSET(fd2, &wr)) {
r =
write(fd2, buf1 + buf1_written,
buf1_avail \- buf1_written);
if (r < 1) {
SHUT_FD2;
} else
buf1_written += r;
}
/* check if write data has caught read data */
if (buf1_written == buf1_avail)
buf1_written = buf1_avail = 0;
if (buf2_written == buf2_avail)
buf2_written = buf2_avail = 0;
/* one side has closed the connection, keep
writing to the other side until empty */
if (fd1 < 0 && buf1_avail \- buf1_written == 0) {
SHUT_FD2;
}
if (fd2 < 0 && buf2_avail \- buf2_written == 0) {
SHUT_FD1;
}
}
return 0;
}
.fi
.PP
The above program properly forwards most kinds of TCP connections
including OOB signal data transmitted by \fBtelnet\fP servers.
It
handles the tricky problem of having data flow in both directions
simultaneously.
You might think it more efficient to use a \fBfork\fP()
call and devote a thread to each stream.
This becomes more tricky than
you might suspect.
Another idea is to set non-blocking I/O using an
\fBioctl\fP() call.
This also has its problems because you end up having
to have inefficient timeouts.
The program does not handle more than one simultaneous connection at a
time, although it could easily be extended to do this with a linked list
of buffers \(em one for each connection.
At the moment, new
connections cause the current connection to be dropped.
.SH SELECT LAW
Many people who try to use \fBselect\fP() come across behavior that is
difficult to understand and produces non-portable or borderline
results.
For instance, the above program is carefully written not to
block at any point, even though it does not set its file descriptors to
non-blocking mode at all (see \fBioctl\fP(2)).
It is easy to introduce
subtle errors that will remove the advantage of using \fBselect\fP(),
hence I will present a list of essentials to watch for when using the
\fBselect\fP() call.
.TP
\fB1.\fP
You should always try to use \fBselect\fP() without a timeout.
Your program
should have nothing to do if there is no data available.
Code that
depends on timeouts is not usually portable and is difficult to debug.
.TP
\fB2.\fP
The value \fInfds\fP must be properly calculated for efficiency as
explained above.
.TP
\fB3.\fP
No file descriptor must be added to any set if you do not intend
to check its result after the \fBselect\fP() call, and respond
appropriately.
See next rule.
.TP
\fB4.\fP
After \fBselect\fP() returns, all file descriptors in all sets
should be checked to see if they are ready.
.\" mtk, May 2006: the following isn't really true.
.\" Any file descriptor that is available
.\" for writing \fImust\fP be written to, and any file descriptor
.\" available for reading \fImust\fP be read, etc.
.TP
\fB5.\fP
The functions \fBread\fP(), \fBrecv\fP(), \fBwrite\fP(), and
\fBsend\fP() do \fInot\fP necessarily read/write the full amount of data
that you have requested.
If they do read/write the full amount, its
because you have a low traffic load and a fast stream.
This is not
always going to be the case.
You should cope with the case of your
functions only managing to send or receive a single byte.
.TP
\fB6.\fP
Never read/write only in single bytes at a time unless your are really
sure that you have a small amount of data to process.
It is extremely
inefficient not to read/write as much data as you can buffer each time.
The buffers in the example above are 1024 bytes although they could
easily be made larger.
.TP
\fB7.\fP
The functions \fBread\fP(), \fBrecv\fP(), \fBwrite\fP(), and
\fBsend\fP() as well as the \fBselect\fP() call can return \-1 with
.I errno
set to \fBEINTR\fP,
or with
.I errno
set to \fBEAGAIN\fP (\fBEWOULDBLOCK\fP).
These results must be properly managed (not done properly
above).
If your program is not going to receive any signals then
it is unlikely you will get \fBEINTR\fP.
If your program does not
set non-blocking I/O, you will not get \fBEAGAIN\fP.
Nonetheless
you should still cope with these errors for completeness.
.TP
\fB8.\fP
Never call \fBread\fP(), \fBrecv\fP(), \fBwrite\fP(), or \fBsend\fP()
with a buffer length of zero.
.TP
\fB9.\fP
If the functions \fBread\fP(),
\fBrecv\fP(), \fBwrite\fP(), and \fBsend\fP() fail
with errors other than those listed in \fB7.\fP,
or one of the input functions returns 0, indicating end of file,
then you should \fInot\fP pass that descriptor to
.BR select ()
again.
In the above example,
I close the descriptor immediately, and then set it to \-1
to prevent it being included in a set.
.TP
\fB10.\fP
The timeout value must be initialized with each new call to \fBselect\fP(),
since some operating systems modify the structure. \fBpselect\fP()
however does not modify its timeout structure.
.TP
\fB11.\fP
I have heard that the Windows socket layer does not cope with OOB data
properly.
It also does not cope with \fBselect\fP() calls when no file
descriptors are set at all.
Having no file descriptors set is a useful
way to sleep the process with sub-second precision by using the timeout.
(See further on.)
.SH USLEEP EMULATION
On systems that do not have a \fBusleep\fP() function, you can call
\fBselect\fP() with a finite timeout and no file descriptors as
follows:
.PP
.nf
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 200000; /* 0.2 seconds */
select(0, NULL, NULL, NULL, &tv);
.fi
.PP
This is only guaranteed to work on Unix systems, however.
.SH RETURN VALUE
On success, \fBselect\fP() returns the total number of file descriptors
still present in the file descriptor sets.
If \fBselect\fP() timed out, then
the return value will be zero.
The file descriptors set should be all
empty (but may not be on some systems).
A return value of \-1 indicates an error, with \fIerrno\fP being
set appropriately.
In the case of an error, the returned sets and
the timeout struct contents are undefined and should not be used.
\fBpselect\fP() however never modifies \fIntimeout\fP.
.SH NOTES
Generally speaking, all operating systems that support sockets, also
support \fBselect\fP().
Many types of programs become
extremely complicated without the use of
.BR select ().
\fBselect\fP() can be used to solve
many problems in a portable and efficient way that naive programmers try
to solve in a more complicated manner using
threads, forking, IPCs, signals, memory sharing, and so on.
.PP
The
.BR poll (2)
system call has the same functionality as \fBselect\fP(),
and is somewhat more efficient when monitoring sparse
file descriptor sets.
It is nowadays widely available,
but historically was less portable than \fBselect\fP().
.PP
The Linux-specific
.BR epoll (7)
API provides an interface that is more efficient than
.BR select (2)
and
.BR poll (2)
when monitoring large numbers of file descriptors.
.SH SEE ALSO
.BR accept (2),
.BR connect (2),
.BR ioctl (2),
.BR poll (2),
.BR read (2),
.BR recv (2),
.BR select (2),
.BR send (2),
.BR sigprocmask (2),
.BR write (2),
.BR sigaddset (3),
.BR sigdelset (3),
.BR sigemptyset (3),
.BR sigfillset (3),
.BR sigismember (3),
.BR epoll (7)
.\" .SH AUTHORS
.\" This man page was written by Paul Sheer.
|