This commit is contained in:
Michael Kerrisk 2007-12-23 22:20:16 +00:00
parent 218dfacd71
commit 7670ac9d8e
2 changed files with 16 additions and 14 deletions

View File

@ -365,8 +365,8 @@ subtle errors that will remove the advantage of using
hence I will present a list of essentials to watch for when using the
.BR select ()
call.
.TP
\fB1.\fP
.TP 4
1.
You should always try to use
.BR select ()
without a timeout.
@ -375,11 +375,11 @@ 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
2.
The value \fInfds\fP must be properly calculated for efficiency as
explained above.
.TP
\fB3.\fP
3.
No file descriptor must be added to any set if you do not intend
to check its result after the
.BR select ()
@ -387,7 +387,7 @@ call, and respond
appropriately.
See next rule.
.TP
\fB4.\fP
4.
After
.BR select ()
returns, all file descriptors in all sets
@ -397,7 +397,7 @@ should be checked to see if they are ready.
.\" for writing \fImust\fP be written to, and any file descriptor
.\" available for reading \fImust\fP be read, etc.
.TP
\fB5.\fP
5.
The functions
.BR read (2),
.BR recv (2),
@ -413,7 +413,7 @@ 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
6.
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
@ -421,7 +421,7 @@ 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
7.
The functions
.BR read (2),
.BR recv (2),
@ -445,7 +445,7 @@ set non-blocking I/O, you will not get \fBEAGAIN\fP.
Nonetheless
you should still cope with these errors for completeness.
.TP
\fB8.\fP
8.
Never call
.BR read (2),
.BR recv (2),
@ -454,7 +454,7 @@ or
.BR send (2)
with a buffer length of zero.
.TP
\fB9.\fP
9.
If the functions
.BR read (2),
.BR recv (2),
@ -471,14 +471,14 @@ 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
10.
The timeout value must be initialized with each new call to
.BR select (),
since some operating systems modify the structure.
.BR pselect ()
however does not modify its timeout structure.
.TP
\fB11.\fP
11.
I have heard that the Windows socket layer does not cope with OOB data
properly.
It also does not cope with
@ -583,6 +583,7 @@ 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;
@ -612,6 +613,7 @@ 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);

View File

@ -156,7 +156,7 @@ Otherwise
(the count of processes waiting until this semaphore's value becomes zero)
is incremented by one and the process sleeps until
one of the following occurs:
.IP \(bu
.IP \(bu 3
.I semval
becomes 0, at which time the value of
.I semzcnt
@ -230,7 +230,7 @@ Otherwise
(the counter of processes waiting for this semaphore's value to increase)
is incremented by one and the process sleeps until
one of the following occurs:
.IP \(bu
.IP \(bu 3
.I semval
becomes greater than or equal to the absolute value of
.IR sem_op ,