-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.html
742 lines (580 loc) · 23.7 KB
/
select.html
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
733
734
735
736
737
738
739
740
741
742
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:27:01 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>SELECT</title>
</head>
<body>
<h1 align="center">SELECT</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#VERSIONS">VERSIONS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">select,
pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O
multiplexing</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">/* According to
POSIX.1-2001, POSIX.1-2008 */ <b><br>
#include <sys/select.h></b></p>
<p style="margin-left:11%; margin-top: 1em">/* According to
earlier standards */ <b><br>
#include <sys/time.h> <br>
#include <sys/types.h> <br>
#include <unistd.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
select(int</b> <i>nfds</i><b>, fd_set
*</b><i>readfds</i><b>, fd_set *</b><i>writefds</i><b>, <br>
fd_set *</b><i>exceptfds</i><b>, struct timeval
*</b><i>timeout</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>void
FD_CLR(int</b> <i>fd</i><b>, fd_set *</b><i>set</i><b>);
<br>
int FD_ISSET(int</b> <i>fd</i><b>, fd_set
*</b><i>set</i><b>); <br>
void FD_SET(int</b> <i>fd</i><b>, fd_set
*</b><i>set</i><b>); <br>
void FD_ZERO(fd_set *</b><i>set</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<sys/select.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
pselect(int</b> <i>nfds</i><b>, fd_set
*</b><i>readfds</i><b>, fd_set *</b><i>writefds</i><b>, <br>
fd_set *</b><i>exceptfds</i><b>, const struct timespec
*</b><i>timeout</i><b>, <br>
const sigset_t *</b><i>sigmask</i><b>);</b></p>
<p style="margin-left:5%; margin-top: 1em">Feature Test
Macro Requirements for glibc (see
<b>feature_test_macros</b>(7)):</p>
<p style="margin-left:11%; margin-top: 1em"><b>pselect</b>():
_POSIX_C_SOURCE >= 200112L</p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>select</b>()
and <b>pselect</b>() allow a program to monitor multiple
file descriptors, waiting until one or more of the file
descriptors become "ready" for some class of I/O
operation (e.g., input possible). A file descriptor is
considered ready if it is possible to perform a
corresponding I/O operation (e.g., <b>read</b>(2), or a
sufficiently small <b>write</b>(2)) without blocking.</p>
<p style="margin-left:11%; margin-top: 1em"><b>select</b>()
can monitor only file descriptors numbers that are less than
<b>FD_SETSIZE</b>; <b>poll</b>(2) does not have this
limitation. See BUGS.</p>
<p style="margin-left:11%; margin-top: 1em">The operation
of <b>select</b>() and <b>pselect</b>() is identical, other
than these three differences:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p>(i)</p></td>
<td width="4%"></td>
<td width="78%">
<p><b>select</b>() uses a timeout that is a <i>struct
timeval</i> (with seconds and microseconds), while
<b>pselect</b>() uses a <i>struct timespec</i> (with seconds
and nanoseconds).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p>(ii)</p></td>
<td width="4%"></td>
<td width="78%">
<p><b>select</b>() may update the <i>timeout</i> argument
to indicate how much time was left. <b>pselect</b>() does
not change this argument.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p>(iii)</p></td>
<td width="4%"></td>
<td width="78%">
<p><b>select</b>() has no <i>sigmask</i> argument, and
behaves as <b>pselect</b>() called with NULL
<i>sigmask</i>.</p> </td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Three
independent sets of file descriptors are watched. The file
descriptors listed in <i>readfds</i> will be watched to see
if characters become available for reading (more precisely,
to see if a read will not block; in particular, a file
descriptor is also ready on end-of-file). The file
descriptors in <i>writefds</i> will be watched to see if
space is available for write (though a large write may still
block). The file descriptors in <i>exceptfds</i> will be
watched for exceptional conditions. (For examples of some
exceptional conditions, see the discussion of <b>POLLPRI</b>
in <b>poll</b>(2).)</p>
<p style="margin-left:11%; margin-top: 1em">On exit, each
of the file descriptor sets is modified in place to indicate
which file descriptors actually changed status. (Thus, if
using <b>select</b>() within a loop, the sets must be
reinitialized before each call.)</p>
<p style="margin-left:11%; margin-top: 1em">Each of the
three file descriptor sets may be specified as NULL if no
file descriptors are to be watched for the corresponding
class of events.</p>
<p style="margin-left:11%; margin-top: 1em">Four macros are
provided to manipulate the sets. <b>FD_ZERO</b>() clears a
set. <b>FD_SET</b>() and <b>FD_CLR</b>() add and remove a
given file descriptor from a set. <b>FD_ISSET</b>() tests to
see if a file descriptor is part of the set; this is useful
after <b>select</b>() returns.</p>
<p style="margin-left:11%; margin-top: 1em"><i>nfds</i>
should be set to the highest-numbered file descriptor in any
of the three sets, plus 1. The indicated file descriptors in
each set are checked, up to this limit (but see BUGS).</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>timeout</i> argument specifies the interval that
<b>select</b>() should block waiting for a file descriptor
to become ready. The call will block until either:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p style="margin-top: 1em">*</p></td>
<td width="3%"></td>
<td width="73%">
<p style="margin-top: 1em">a file descriptor becomes
ready;</p> </td>
<td width="12%">
</td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="73%">
<p>the call is interrupted by a signal handler; or</p></td>
<td width="12%">
</td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="73%">
<p>the timeout expires.</p></td>
<td width="12%">
</td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Note that the
<i>timeout</i> interval will be rounded up to the system
clock granularity, and kernel scheduling delays mean that
the blocking interval may overrun by a small amount. If both
fields of the <i>timeval</i> structure are zero, then
<b>select</b>() returns immediately. (This is useful for
polling.) If <i>timeout</i> is NULL (no timeout),
<b>select</b>() can block indefinitely.</p>
<p style="margin-left:11%; margin-top: 1em"><i>sigmask</i>
is a pointer to a signal mask (see <b>sigprocmask</b>(2));
if it is not NULL, then <b>pselect</b>() first replaces the
current signal mask by the one pointed to by <i>sigmask</i>,
then does the "select" function, and then restores
the original signal mask.</p>
<p style="margin-left:11%; margin-top: 1em">Other than the
difference in the precision of the <i>timeout</i> argument,
the following <b>pselect</b>() call:</p>
<p style="margin-left:17%; margin-top: 1em">ready =
pselect(nfds, &readfds, &writefds, &exceptfds,
<br>
timeout, &sigmask);</p>
<p style="margin-left:11%; margin-top: 1em">is equivalent
to <i>atomically</i> executing the following calls:</p>
<p style="margin-left:17%; margin-top: 1em">sigset_t
origmask;</p>
<p style="margin-left:17%; margin-top: 1em">pthread_sigmask(SIG_SETMASK,
&sigmask, &origmask); <br>
ready = select(nfds, &readfds, &writefds,
&exceptfds, timeout); <br>
pthread_sigmask(SIG_SETMASK, &origmask, NULL);</p>
<p style="margin-left:11%; margin-top: 1em">The reason that
<b>pselect</b>() is needed is that if one wants to wait for
either a signal or for a file descriptor to become ready,
then an atomic test is needed to prevent race conditions.
(Suppose the signal handler sets a global flag and returns.
Then a test of this global flag followed by a call of
<b>select</b>() could hang indefinitely if the signal
arrived just after the test but just before the call. By
contrast, <b>pselect</b>() allows one to first block
signals, handle the signals that have come in, then call
<b>pselect</b>() with the desired <i>sigmask</i>, avoiding
the race.)</p>
<p style="margin-left:11%; margin-top: 1em"><b>The
timeout</b> <br>
The time structures involved are defined in
<i><sys/time.h></i> and look like</p>
<p style="margin-left:17%; margin-top: 1em">struct timeval
{ <br>
long tv_sec; /* seconds */ <br>
long tv_usec; /* microseconds */ <br>
};</p>
<p style="margin-left:11%; margin-top: 1em">and</p>
<p style="margin-left:17%; margin-top: 1em">struct timespec
{ <br>
long tv_sec; /* seconds */ <br>
long tv_nsec; /* nanoseconds */ <br>
};</p>
<p style="margin-left:11%; margin-top: 1em">(However, see
below on the POSIX.1 versions.)</p>
<p style="margin-left:11%; margin-top: 1em">Some code calls
<b>select</b>() with all three sets empty, <i>nfds</i> zero,
and a non-NULL <i>timeout</i> as a fairly portable way to
sleep with subsecond precision.</p>
<p style="margin-left:11%; margin-top: 1em">On Linux,
<b>select</b>() modifies <i>timeout</i> to reflect the
amount of time not slept; most other implementations do not
do this. (POSIX.1 permits either behavior.) This causes
problems both when Linux code which reads <i>timeout</i> is
ported to other operating systems, and when code is ported
to Linux that reuses a <i>struct timeval</i> for multiple
<b>select</b>()s in a loop without reinitializing it.
Consider <i>timeout</i> to be undefined after
<b>select</b>() returns.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success,
<b>select</b>() and <b>pselect</b>() return the number of
file descriptors contained in the three returned descriptor
sets (that is, the total number of bits that are set in
<i>readfds</i>, <i>writefds</i>, <i>exceptfds</i>) which may
be zero if the timeout expires before anything interesting
happens. On error, -1 is returned, and <i>errno</i> is set
to indicate the error; the file descriptor sets are
unmodified, and <i>timeout</i> becomes undefined.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EBADF</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">An invalid file descriptor was
given in one of the sets. (Perhaps a file descriptor that
was already closed, or one on which an error has occurred.)
However, see BUGS.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINTR</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>A signal was caught; see <b>signal</b>(7).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>nfds</i> is negative or exceeds the
<b>RLIMIT_NOFILE</b> resource limit (see
<b>getrlimit</b>(2)).</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The value contained within <i>timeout</i> is
invalid.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Unable to allocate memory for internal tables.</p></td></tr>
</table>
<h2>VERSIONS
<a name="VERSIONS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>pselect</b>()
was added to Linux in kernel 2.6.16. Prior to this,
<b>pselect</b>() was emulated in glibc (but see BUGS).</p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>select</b>()
conforms to POSIX.1-2001, POSIX.1-2008, and 4.4BSD
(<b>select</b>() first appeared in 4.2BSD). Generally
portable to/from non-BSD systems supporting clones of the
BSD socket layer (including System V variants).
However, note that the System V variant typically sets
the timeout variable before exit, but the BSD variant does
not.</p>
<p style="margin-left:11%; margin-top: 1em"><b>pselect</b>()
is defined in POSIX.1g, and in POSIX.1-2001 and
POSIX.1-2008.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">An
<i>fd_set</i> is a fixed size buffer. Executing
<b>FD_CLR</b>() or <b>FD_SET</b>() with a value of <i>fd</i>
that is negative or is equal to or larger than
<b>FD_SETSIZE</b> will result in undefined behavior.
Moreover, POSIX requires <i>fd</i> to be a valid file
descriptor.</p>
<p style="margin-left:11%; margin-top: 1em">The operation
of <b>select</b>() and <b>pselect</b>() is not affected by
the <b>O_NONBLOCK</b> flag.</p>
<p style="margin-left:11%; margin-top: 1em">On some other
UNIX systems, <b>select</b>() can fail with the error
<b>EAGAIN</b> if the system fails to allocate
kernel-internal resources, rather than <b>ENOMEM</b> as
Linux does. POSIX specifies this error for <b>poll</b>(2),
but not for <b>select</b>(). Portable programs may wish to
check for <b>EAGAIN</b> and loop, just as with
<b>EINTR</b>.</p>
<p style="margin-left:11%; margin-top: 1em">On systems that
lack <b>pselect</b>(), reliable (and more portable) signal
trapping can be achieved using the self-pipe trick. In this
technique, a signal handler writes a byte to a pipe whose
other end is monitored by <b>select</b>() in the main
program. (To avoid possibly blocking when writing to a pipe
that may be full or reading from a pipe that may be empty,
nonblocking I/O is used when reading from and writing to the
pipe.)</p>
<p style="margin-left:11%; margin-top: 1em">Concerning the
types involved, the classical situation is that the two
fields of a <i>timeval</i> structure are typed as
<i>long</i> (as shown above), and the structure is defined
in <i><sys/time.h></i>. The POSIX.1 situation is</p>
<p style="margin-left:17%; margin-top: 1em">struct timeval
{ <br>
time_t tv_sec; /* seconds */ <br>
suseconds_t tv_usec; /* microseconds */ <br>
};</p>
<p style="margin-left:11%; margin-top: 1em">where the
structure is defined in <i><sys/select.h></i> and the
data types <i>time_t</i> and <i>suseconds_t</i> are defined
in <i><sys/types.h></i>.</p>
<p style="margin-left:11%; margin-top: 1em">Concerning
prototypes, the classical situation is that one should
include <i><time.h></i> for <b>select</b>(). The
POSIX.1 situation is that one should include
<i><sys/select.h></i> for <b>select</b>() and
<b>pselect</b>().</p>
<p style="margin-left:11%; margin-top: 1em">Under glibc
2.0, <i><sys/select.h></i> gives the wrong prototype
for <b>pselect</b>(). Under glibc 2.1 to 2.2.1, it gives
<b>pselect</b>() when <b>_GNU_SOURCE</b> is defined. Since
glibc 2.2.2, the requirements are as shown in the
SYNOPSIS.</p>
<p style="margin-left:11%; margin-top: 1em"><b>Correspondence
between select() and poll() notifications</b> <br>
Within the Linux kernel source, we find the following
definitions which show the correspondence between the
readable, writable, and exceptional condition notifications
of <b>select</b>() and the event notifications provided by
<b>poll</b>(2) (and <b>epoll</b>(7)):</p>
<p style="margin-left:17%; margin-top: 1em">#define
POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP |
<br>
POLLERR) <br>
/* Ready for reading */ <br>
#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT |
POLLERR) <br>
/* Ready for writing */ <br>
#define POLLEX_SET (POLLPRI) <br>
/* Exceptional condition */</p>
<p style="margin-left:11%; margin-top: 1em"><b>Multithreaded
applications</b> <br>
If a file descriptor being monitored by <b>select</b>() is
closed in another thread, the result is unspecified. On some
UNIX systems, <b>select</b>() unblocks and returns, with an
indication that the file descriptor is ready (a subsequent
I/O operation will likely fail with an error, unless another
process reopens file descriptor between the time
<b>select</b>() returned and the I/O operation is
performed). On Linux (and some other systems), closing the
file descriptor in another thread has no effect on
<b>select</b>(). In summary, any application that relies on
a particular behavior in this scenario must be considered
buggy.</p>
<p style="margin-left:11%; margin-top: 1em"><b>C
library/kernel differences</b> <br>
The Linux kernel allows file descriptor sets of arbitrary
size, determining the length of the sets to be checked from
the value of <i>nfds</i>. However, in the glibc
implementation, the <i>fd_set</i> type is fixed in size. See
also BUGS.</p>
<p style="margin-left:11%; margin-top: 1em">The
<b>pselect</b>() interface described in this page is
implemented by glibc. The underlying Linux system call is
named <b>pselect6</b>(). This system call has somewhat
different behavior from the glibc wrapper function.</p>
<p style="margin-left:11%; margin-top: 1em">The Linux
<b>pselect6</b>() system call modifies its <i>timeout</i>
argument. However, the glibc wrapper function hides this
behavior by using a local variable for the timeout argument
that is passed to the system call. Thus, the glibc
<b>pselect</b>() function does not modify its <i>timeout</i>
argument; this is the behavior required by POSIX.1-2001.</p>
<p style="margin-left:11%; margin-top: 1em">The final
argument of the <b>pselect6</b>() system call is not a
<i>sigset_t *</i> pointer, but is instead a structure
of the form:</p>
<p style="margin-left:17%; margin-top: 1em">struct { <br>
const kernel_sigset_t *ss; /* Pointer to signal set */ <br>
size_t ss_len; /* Size (in bytes) of object <br>
pointed to by ’ss’ */ <br>
};</p>
<p style="margin-left:11%; margin-top: 1em">This allows the
system call to obtain both a pointer to the signal set and
its size, while allowing for the fact that most
architectures support a maximum of 6 arguments to a system
call. See <b>sigprocmask</b>(2) for a discussion of the
difference between the kernel and libc notion of the signal
set.</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">POSIX allows an
implementation to define an upper limit, advertised via the
constant <b>FD_SETSIZE</b>, on the range of file descriptors
that can be specified in a file descriptor set. The Linux
kernel imposes no fixed limit, but the glibc implementation
makes <i>fd_set</i> a fixed-size type, with
<b>FD_SETSIZE</b> defined as 1024, and the <b>FD_*</b>()
macros operating according to that limit. To monitor file
descriptors greater than 1023, use <b>poll</b>(2)
instead.</p>
<p style="margin-left:11%; margin-top: 1em">The
implementation of the <i>fd_set</i> arguments as
value-result arguments means that they must be reinitialized
on each call to <b>select</b>(). This design error is
avoided by <b>poll</b>(2), which uses separate structure
fields for the input and output of the call.</p>
<p style="margin-left:11%; margin-top: 1em">According to
POSIX, <b>select</b>() should check all specified file
descriptors in the three file descriptor sets, up to the
limit <i>nfds-1</i>. However, the current implementation
ignores any file descriptor in these sets that is greater
than the maximum file descriptor number that the process
currently has open. According to POSIX, any such file
descriptor that is specified in one of the sets should
result in the error <b>EBADF</b>.</p>
<p style="margin-left:11%; margin-top: 1em">Glibc 2.0
provided a version of <b>pselect</b>() that did not take a
<i>sigmask</i> argument.</p>
<p style="margin-left:11%; margin-top: 1em">Starting with
version 2.1, glibc provided an emulation of <b>pselect</b>()
that was implemented using <b>sigprocmask</b>(2) and
<b>select</b>(). This implementation remained vulnerable to
the very race condition that <b>pselect</b>() was designed
to prevent. Modern versions of glibc use the (race-free)
<b>pselect</b>() system call on kernels where it is
provided.</p>
<p style="margin-left:11%; margin-top: 1em">Under Linux,
<b>select</b>() may report a socket file descriptor as
"ready for reading", while nevertheless a
subsequent read blocks. This could for example happen when
data has arrived but upon examination has wrong checksum and
is discarded. There may be other circumstances in which a
file descriptor is spuriously reported as ready. Thus it may
be safer to use <b>O_NONBLOCK</b> on sockets that should not
block.</p>
<p style="margin-left:11%; margin-top: 1em">On Linux,
<b>select</b>() also modifies <i>timeout</i> if the call is
interrupted by a signal handler (i.e., the <b>EINTR</b>
error return). This is not permitted by POSIX.1. The Linux
<b>pselect</b>() system call has the same behavior, but the
glibc wrapper hides this behavior by internally copying the
<i>timeout</i> to a local variable and passing that variable
to the system call.</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">#include
<stdio.h> <br>
#include <stdlib.h> <br>
#include <sys/time.h> <br>
#include <sys/types.h> <br>
#include <unistd.h></p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(void) <br>
{ <br>
fd_set rfds; <br>
struct timeval tv; <br>
int retval;</p>
<p style="margin-left:11%; margin-top: 1em">/* Watch stdin
(fd 0) to see when it has input. */</p>
<p style="margin-left:11%; margin-top: 1em">FD_ZERO(&rfds);
<br>
FD_SET(0, &rfds);</p>
<p style="margin-left:11%; margin-top: 1em">/* Wait up to
five seconds. */</p>
<p style="margin-left:11%; margin-top: 1em">tv.tv_sec = 5;
<br>
tv.tv_usec = 0;</p>
<p style="margin-left:11%; margin-top: 1em">retval =
select(1, &rfds, NULL, NULL, &tv); <br>
/* Don’t rely on the value of tv now! */</p>
<p style="margin-left:11%; margin-top: 1em">if (retval ==
-1) <br>
perror("select()"); <br>
else if (retval) <br>
printf("Data is available now.\n"); <br>
/* FD_ISSET(0, &rfds) will be true. */ <br>
else <br>
printf("No data within five seconds.\n");</p>
<p style="margin-left:11%; margin-top: 1em">exit(EXIT_SUCCESS);
<br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>accept</b>(2),
<b>connect</b>(2), <b>poll</b>(2), <b>read</b>(2),
<b>recv</b>(2), <b>restart_syscall</b>(2), <b>send</b>(2),
<b>sigprocmask</b>(2), <b>write</b>(2), <b>epoll</b>(7),
<b>time</b>(7)</p>
<p style="margin-left:11%; margin-top: 1em">For a tutorial
with discussion and examples, see <b>select_tut</b>(2).</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>