updates for 4.3 from lapsley
[unix-history] / usr / src / share / doc / psd / 21.ipc / 2.t
CommitLineData
d60e8dff
MK
1.\" Copyright (c) 1986 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
5.\" @(#)2.t 1.2 (Berkeley) %G%
6.\"
200989e9
MK
7.ds RH "Basics
8.bp
9.nr H1 2
10.nr H2 0
11.bp
12.LG
13.B
14.ce
152. BASICS
16.sp 2
17.R
18.NL
19.PP
20The basic building block for communication is the \fIsocket\fP.
21A socket is an endpoint of communication to which a name may
22be \fIbound\fP. Each socket in use has a \fItype\fP
23and one or more associated processes. Sockets exist within
24\fIcommunication domains\fP.
25A communication domain is an
26abstraction introduced to bundle common properties of
27processes communicating through sockets.
28One such property is the scheme used to name sockets. For
29example, in the UNIX communication domain sockets are
30named with UNIX path names; e.g. a
31socket may be named \*(lq/dev/foo\*(rq. Sockets normally
32exchange data only with
33sockets in the same domain (it may be possible to cross domain
34boundaries, but only if some translation process is
35performed). The
d60e8dff
MK
364.3BSD IPC facilities support three separate communication domains:
37the UNIX domain, for on-system communication;
38the Internet domain, which is used by
200989e9 39processes which communicate
d60e8dff
MK
40using the the DARPA standard communication protocols;
41and the NS domain, which is used by processes which
42communicate using the Xerox standard communication
43protocols*.
44.FS
45* See \fIInternet Transport Protocols\fP, Xerox System Integration
46Standard (XSIS)028112 for more information. This document is
47almost a necessity for one trying to write NS applications.
48.FE
200989e9
MK
49The underlying communication
50facilities provided by these domains have a significant influence
51on the internal system implementation as well as the interface to
52socket facilities available to a user. An example of the
53latter is that a socket \*(lqoperating\*(rq in the UNIX domain
d60e8dff
MK
54sees a subset of the error conditions which are possible
55when operating in the Internet (or NS) domain.
200989e9
MK
56.NH 2
57Socket types
58.PP
59Sockets are
60typed according to the communication properties visible to a
61user.
62Processes are presumed to communicate only between sockets of
63the same type, although there is
64nothing that prevents communication between sockets of different
65types should the underlying communication
66protocols support this.
67.PP
d60e8dff 68Four types of sockets currently are available to a user.
200989e9
MK
69A \fIstream\fP socket provides for the bidirectional, reliable,
70sequenced, and unduplicated flow of data without record boundaries.
71Aside from the bidirectionality of data flow, a pair of connected
d60e8dff 72stream sockets provides an interface nearly identical to that of pipes\(dg.
200989e9 73.FS
d60e8dff 74\(dg In the UNIX domain, in fact, the semantics are identical and,
200989e9
MK
75as one might expect, pipes have been implemented internally
76as simply a pair of connected stream sockets.
77.FE
78.PP
79A \fIdatagram\fP socket supports bidirectional flow of data which
80is not promised to be sequenced, reliable, or unduplicated.
81That is, a process
82receiving messages on a datagram socket may find messages duplicated,
83and, possibly,
84in an order different from the order in which it was sent.
85An important characteristic of a datagram
86socket is that record boundaries in data are preserved. Datagram
87sockets closely model the facilities found in many contemporary
88packet switched networks such as the Ethernet.
89.PP
90A \fIraw\fP socket provides users access to
91the underlying communication
92protocols which support socket abstractions.
93These sockets are normally datagram oriented, though their
94exact characteristics are dependent on the interface provided by
95the protocol. Raw sockets are not intended for the general user; they
96have been provided mainly for those interested in developing new
97communication protocols, or for gaining access to some of the more
98esoteric facilities of an existing protocol. The use of raw sockets
99is considered in section 5.
100.PP
d60e8dff
MK
101A \fIsequenced packet\fP socket is similar to a stream socket,
102with the exception that record boundaries are preserved. This
103interface is provided only as part of the NS socket abstraction,
104and is very important in most serious NS applications.
105Sequenced-packet sockets allow the user to manipulate the
106SPP or IDP headers on a packet or a group of packets either
107by writing a prototype header along with whatever data is
108to be sent, or by specifying a default header to be used with
109all outgoing data, and allows the user to receive the headers
110on incoming packets. The use of these options is considered in
111section 5.
112.PP
113Another potential socket type which has interesting properties is
114the \fIreliably delivered
115message\fP socket.
200989e9
MK
116The reliably delivered message socket has
117similar properties to a datagram socket, but with
d60e8dff
MK
118reliable delivery. There is currently no support for this
119type of socket, but a reliably delivered message protocol
120similar to Xerox's Packet Exchange Protocol (PEX) may be
121simulated at the user level. More information on this topic
122can be found in section 5.
200989e9
MK
123.NH 2
124Socket creation
125.PP
126To create a socket the \fIsocket\fP system call is used:
127.DS
128s = socket(domain, type, protocol);
129.DE
130This call requests that the system create a socket in the specified
131\fIdomain\fP and of the specified \fItype\fP. A particular protocol may
132also be requested. If the protocol is left unspecified (a value
133of 0), the system will select an appropriate protocol from those
134protocols which comprise the communication domain and which
135may be used to support the requested socket type. The user is
136returned a descriptor (a small integer number) which may be used
137in later system calls which operate on sockets. The domain is specified as
138one of the manifest constants defined in the file <\fIsys/socket.h\fP>.
139For the UNIX domain the constant is AF_UNIX*; for the Internet
140.FS
141* The manifest constants are named AF_whatever as they indicate
142the ``address format'' to use in interpreting names.
143.FE
d60e8dff
MK
144domain AF_INET; and for the NS domain, AF_NS.
145The socket types are also defined in this file
146and one of SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, or SOCK_SEQPACKET
147must be specified.
200989e9
MK
148To create a stream socket in the Internet domain the following
149call might be used:
150.DS
151s = socket(AF_INET, SOCK_STREAM, 0);
152.DE
153This call would result in a stream socket being created with the TCP
154protocol providing the underlying communication support. To
d60e8dff 155create a datagram socket for on-machine use the call might
200989e9
MK
156be:
157.DS
158s = socket(AF_UNIX, SOCK_DGRAM, 0);
159.DE
160.PP
d60e8dff
MK
161The default protocol (used when the \fIprotocol\fP argument to the
162\fIsocket\fP call is 0) should be correct for most every
163situation. However, it is possible to specify a protocol
164other than the default; this will be covered in
165section 5.
200989e9
MK
166.PP
167There are several reasons a socket call may fail. Aside from
168the rare occurrence of lack of memory (ENOBUFS), a socket
169request may fail due to a request for an unknown protocol
170(EPROTONOSUPPORT), or a request for a type of socket for
171which there is no supporting protocol (EPROTOTYPE).
172.NH 2
d60e8dff 173Binding local names
200989e9
MK
174.PP
175A socket is created without a name. Until a name is bound
176to a socket, processes have no way to reference it and, consequently,
d60e8dff
MK
177no messages may be received on it.
178Communicating processes are bound
179by an \fIassociation\fP. In the Internet and NS domains,
180an association
181is composed of local and foreign
182addresses, and local and foreign ports,
183while in the UNIX domain, an association is composed of
184local and foreign path names (the phrase ``foreign pathname''
185means a pathname created by a foreign process, not a pathname
186on a foreign system).
187Associations are always unique. That is, in the Internet domain, there
188may never be duplicate <protocol, local address, local port, foreign
189address, foreign port> tuples. Similarly, in the UNIX domain,
190there may never be duplicate <protocol, local pathname, foreign
191pathname> tuples, and the pathnames must (in 4.3; the situation
192may change in future releases) be unique with respect to the files
193already existing on the system.
194.PP
195The \fIbind\fP system call allows a process to specify half of
196an association, <local address, local port>
197(or <local pathname>), while the \fIconnect\fP
198and \fIaccept\fP primitives are used to complete a socket's association.
199.PP
200In the Internet domain,
201binding names to sockets can be fairly complex.
202Fortunately, it is usually not necessary to specifically bind an
203address and port number to a socket, because the
204\fIconnect\fP and \fIsend\fP calls will automatically
205bind an appropriate address if they are used with an
206unbound socket. The process of binding names to NS
207sockets is similar in most ways to that of
208binding names to Internet sockets.
209While binding names to sockets in the
210UNIX domain is less complex, the \fIconnect\fP and \fIsend\fP
211calls can still be used to automatically bind local names.
212.PP
213The \fIbind\fP system call is used as follows:
200989e9
MK
214.DS
215bind(s, name, namelen);
216.DE
217The bound name is a variable length byte string which is interpreted
218by the supporting protocol(s). Its interpretation may vary from
219communication domain to communication domain (this is one of
d60e8dff
MK
220the properties which comprise the \*(lqdomain\*(rq).
221As mentioned, in the
222Internet domain names contain an Internet address and port
223number. NS domain names contain a NS address and
224port number. In the UNIX domain, names contain a path name and
225a family, which is always AF_UNIX. If one wanted to bind
226the name \*(lq/tmp/foo\*(rq to a UNIX domain socket, the
227following code would be used*:
228.FS
229* Note that, although the tendency here is to call the \*(lqaddr\*(rq
230structure \*(lqsun\*(rq, doing so would cause problems if the code
231were ever ported to a Sun workstation.
232.FE
200989e9 233.DS
d60e8dff
MK
234#include <sys/un.h>
235 ...
236struct sockaddr_un addr;
237 ...
238strcpy(addr.sun_path, "/tmp/foo");
239addr.sun_family = AF_UNIX;
240bind(s, (struct sockaddr *) &addr, strlen(addr.sun_path) +
241 sizeof (addr.sun_family));
200989e9 242.DE
d60e8dff
MK
243Note that in determining the size of a UNIX domain address null
244bytes are not counted, which is why \fIstrlen\fP is used. In
245the current implementation of UNIX domain IPC under 4.3BSD,
246the file name
247referred to in \fIaddr.sun_path\fP is created as a socket
248in the system file space.
249The caller must, therefore, have
250write permission in the directory where
251\fIaddr.sun_path\fP is to reside, and this file should be deleted by the
252caller when it is no longer needed. Future versions of 4BSD
253may not create this file.
254.PP
255In binding an Internet address things become more
256complicated. The actual call is similar,
200989e9
MK
257.DS
258#include <sys/types.h>
259#include <netinet/in.h>
260 ...
261struct sockaddr_in sin;
262 ...
d60e8dff 263bind(s, (struct sockaddr *) &sin, sizeof (sin));
200989e9
MK
264.DE
265but the selection of what to place in the address \fIsin\fP
266requires some discussion. We will come back to the problem
267of formulating Internet addresses in section 3 when
268the library routines used in name resolution are discussed.
d60e8dff
MK
269.PP
270Binding a NS address to a socket is even more
271difficult,
272especially since the Internet library routines do not
273work with NS hostnames. The actual call is again similar:
274.DS
275#include <sys/types.h>
276#include <netns/ns.h>
277 ...
278struct sockaddr_ns sns;
279 ...
280bind(s, (struct sockaddr *) &sns, sizeof (sns));
281.DE
282Again, discussion of what to place in a \*(lqstruct sockaddr_ns\*(rq
283will be deferred to section 3.
200989e9
MK
284.NH 2
285Connection establishment
286.PP
287With a bound socket it is possible to rendezvous with
288an unrelated process. This operation is usually asymmetric
289with one process a \*(lqclient\*(rq and the other a \*(lqserver\*(rq.
290The client requests services from the server by initiating a
291\*(lqconnection\*(rq to the server's socket. The server, when
292willing to offer its advertised services, passively \*(lqlistens\*(rq
293on its socket. On the client side the \fIconnect\fP call is
294used to initiate a connection. Using the UNIX domain, this
295might appear as,
296.DS
d60e8dff
MK
297struct sockaddr_un server;
298 ...
299connect(s, (struct sockaddr *)&server, strlen(server.sun_path) +
300 sizeof (server.sun_family));
200989e9
MK
301.DE
302while in the Internet domain,
303.DS
304struct sockaddr_in server;
d60e8dff
MK
305 ...
306connect(s, (struct sockaddr *)&server, sizeof (server));
307.DE
308and in the NS domain,
309.DS
310struct sockaddr_ns server;
311 ...
312connect(s, (struct sockaddr *)&server, sizeof (server));
200989e9 313.DE
d60e8dff
MK
314where \fIserver\fP in the example above would contain either the UNIX
315pathname, Internet address and port number, or NS address and
316port number of the server to which the
317client process wishes to speak.
200989e9
MK
318If the client process's socket is unbound at the time of
319the connect call,
320the system will automatically select and bind a name to
321the socket; c.f. section 5.4.
d60e8dff
MK
322This is the usual way that local addresses are bound
323to a socket.
324.PP
325An error is returned if the connection was unsuccessful
200989e9
MK
326(any name automatically bound by the system, however, remains).
327Otherwise, the socket is associated with the server and
d60e8dff
MK
328data transfer may begin. Some of the more common errors returned
329when a connection attempt fails are:
200989e9
MK
330.IP ETIMEDOUT
331.br
332After failing to establish a connection for a period of time,
333the system decided there was no point in retrying the
334connection attempt any more. This usually occurs because
335the destination host is down, or because problems in
336the network resulted in transmissions being lost.
337.IP ECONNREFUSED
338.br
d60e8dff
MK
339The host refused service for some reason.
340This is usually
200989e9
MK
341due to a server process
342not being present at the requested name.
343.IP "ENETDOWN or EHOSTDOWN"
344.br
345These operational errors are
346returned based on status information delivered to
347the client host by the underlying communication services.
348.IP "ENETUNREACH or EHOSTUNREACH"
349.br
350These operational errors can occur either because the network
351or host is unknown (no route to the network or host is present),
352or because of status information returned by intermediate
353gateways or switching nodes. Many times the status returned
354is not sufficient to distinguish a network being down from a
d60e8dff 355host being down, in which case the system
200989e9
MK
356indicates the entire network is unreachable.
357.PP
358For the server to receive a client's connection it must perform
359two steps after binding its socket.
360The first is to indicate a willingness to listen for
361incoming connection requests:
362.DS
363listen(s, 5);
364.DE
365The second parameter to the \fIlisten\fP call specifies the maximum
366number of outstanding connections which may be queued awaiting
d60e8dff
MK
367acceptance by the server process; this number
368may be limited by the system. Should a connection be
200989e9
MK
369requested while the queue is full, the connection will not be
370refused, but rather the individual messages which comprise the
371request will be ignored. This gives a harried server time to
372make room in its pending connection queue while the client
373retries the connection request. Had the connection been returned
374with the ECONNREFUSED error, the client would be unable to tell
375if the server was up or not. As it is now it is still possible
376to get the ETIMEDOUT error back, though this is unlikely. The
d60e8dff 377backlog figure supplied with the listen call is currently limited
200989e9
MK
378by the system to a maximum of 5 pending connections on any
379one queue. This avoids the problem of processes hogging system
380resources by setting an infinite backlog, then ignoring
381all connection requests.
382.PP
383With a socket marked as listening, a server may \fIaccept\fP
384a connection:
385.DS
d60e8dff
MK
386struct sockaddr_in from;
387 ...
200989e9 388fromlen = sizeof (from);
d60e8dff 389newsock = accept(s, (struct sockaddr *)&from, &fromlen);
200989e9 390.DE
d60e8dff
MK
391(For the UNIX domain, \fIfrom\fP would be declared as a
392\fIstruct sockaddr_un\fP, and for the NS domain, \fIfrom\fP
393would be declared as a \fIstruct sockaddr_ns\fP,
394but nothing different would need
395to be done as far as \fIfromlen\fP is concerned. In the examples
396which follow, only Internet routines will be discussed.) A new
397descriptor is returned on receipt of a connection (along with
200989e9
MK
398a new socket). If the server wishes to find out who its client is,
399it may supply a buffer for the client socket's name. The value-result
400parameter \fIfromlen\fP is initialized by the server to indicate how
401much space is associated with \fIfrom\fP, then modified on return
402to reflect the true size of the name. If the client's name is not
d60e8dff 403of interest, the second parameter may be a null pointer.
200989e9 404.PP
d60e8dff 405\fIAccept\fP normally blocks. That is, \fIaccept\fP
200989e9
MK
406will not return until a connection is available or the system call
407is interrupted by a signal to the process. Further, there is no
408way for a process to indicate it will accept connections from only
409a specific individual, or individuals. It is up to the user process
410to consider who the connection is from and close down the connection
411if it does not wish to speak to the process. If the server process
412wants to accept connections on more than one socket, or not block
d60e8dff 413on the accept call, there are alternatives; they will be considered
200989e9
MK
414in section 5.
415.NH 2
416Data transfer
417.PP
418With a connection established, data may begin to flow. To send
419and receive data there are a number of possible calls.
420With the peer entity at each end of a connection
421anchored, a user can send or receive a message without specifying
422the peer. As one might expect, in this case, then
d60e8dff 423the normal \fIread\fP and \fIwrite\fP system calls are usable,
200989e9
MK
424.DS
425write(s, buf, sizeof (buf));
426read(s, buf, sizeof (buf));
427.DE
428In addition to \fIread\fP and \fIwrite\fP,
429the new calls \fIsend\fP and \fIrecv\fP
430may be used:
431.DS
432send(s, buf, sizeof (buf), flags);
433recv(s, buf, sizeof (buf), flags);
434.DE
435While \fIsend\fP and \fIrecv\fP are virtually identical to
436\fIread\fP and \fIwrite\fP,
d60e8dff
MK
437the extra \fIflags\fP argument is important. The flags,
438defined in \fI<sys/socket.h>\fP, may be
200989e9
MK
439specified as a non-zero value if one or more
440of the following is required:
441.DS
442.TS
443l l.
d60e8dff
MK
444MSG_OOB send/receive out of band data
445MSG_PEEK look at data without reading
446MSG_DONTROUTE send data without routing packets
200989e9
MK
447.TE
448.DE
449Out of band data is a notion specific to stream sockets, and one
450which we will not immediately consider. The option to have data
451sent without routing applied to the outgoing packets is currently
452used only by the routing table management process, and is
453unlikely to be of interest to the casual user. The ability
d60e8dff 454to preview data is, however, of interest. When MSG_PEEK
200989e9
MK
455is specified with a \fIrecv\fP call, any data present is returned
456to the user, but treated as still \*(lqunread\*(rq. That
457is, the next \fIread\fP or \fIrecv\fP call applied to the socket will
458return the data previously previewed.
459.NH 2
460Discarding sockets
461.PP
462Once a socket is no longer of interest, it may be discarded
463by applying a \fIclose\fP to the descriptor,
464.DS
465close(s);
466.DE
467If data is associated with a socket which promises reliable delivery
468(e.g. a stream socket) when a close takes place, the system will
469continue to attempt to transfer the data.
470However, after a fairly long period of
471time, if the data is still undelivered, it will be discarded.
472Should a user have no use for any pending data, it may
473perform a \fIshutdown\fP on the socket prior to closing it.
474This call is of the form:
475.DS
476shutdown(s, how);
477.DE
478where \fIhow\fP is 0 if the user is no longer interested in reading
479data, 1 if no more data will be sent, or 2 if no data is to
d60e8dff 480be sent or received.
200989e9
MK
481.NH 2
482Connectionless sockets
483.PP
484To this point we have been concerned mostly with sockets which
485follow a connection oriented model. However, there is also
486support for connectionless interactions typical of the datagram
487facilities found in contemporary packet switched networks.
488A datagram socket provides a symmetric interface to data
489exchange. While processes are still likely to be client
490and server, there is no requirement for connection establishment.
491Instead, each message includes the destination address.
492.PP
493Datagram sockets are created as before, and each should
494have a name bound to it in order that the recipient of
495a message may identify the sender. To send data,
496the \fIsendto\fP primitive is used,
497.DS
d60e8dff 498sendto(s, buf, buflen, flags, (struct sockaddr *)&to, tolen);
200989e9
MK
499.DE
500The \fIs\fP, \fIbuf\fP, \fIbuflen\fP, and \fIflags\fP
501parameters are used as before.
502The \fIto\fP and \fItolen\fP
503values are used to indicate the intended recipient of the
d60e8dff
MK
504message. When
505using an unreliable datagram interface, it is
200989e9
MK
506unlikely any errors will be reported to the sender. Where
507information is present locally to recognize a message which may
508never be delivered (for instance when a network is unreachable),
509the call will return \-1 and the global value \fIerrno\fP will
510contain an error number.
511.PP
512To receive messages on an unconnected datagram socket, the
513\fIrecvfrom\fP primitive is provided:
514.DS
d60e8dff 515recvfrom(s, buf, buflen, flags, (struct sockaddr *)&from, &fromlen);
200989e9
MK
516.DE
517Once again, the \fIfromlen\fP parameter is handled in
518a value-result fashion, initially containing the size of
d60e8dff
MK
519the \fIfrom\fP buffer, and modified on return to indicate
520the actual size of the from address.
200989e9
MK
521.PP
522In addition to the two calls mentioned above, datagram
523sockets may also use the \fIconnect\fP call to associate
d60e8dff 524a socket with a specific destination address. In this case, any
200989e9
MK
525data sent on the socket will automatically be addressed
526to the connected peer, and only data received from that
527peer will be delivered to the user. Only one connected
528address is permitted for each socket (i.e. no multi-casting).
529Connect requests on datagram sockets return immediately,
530as this simply results in the system recording
531the peer's address (as compared to a stream socket where a
532connect request initiates establishment of an end to end
d60e8dff
MK
533connection). \fIAccept\fP and \fIlisten\fP are not
534used with datagram sockets.
200989e9
MK
535Other of the less
536important details of datagram sockets are described
537in section 5.
538.NH 2
539Input/Output multiplexing
540.PP
541One last facility often used in developing applications
542is the ability to multiplex i/o requests among multiple
543sockets and/or files. This is done using the \fIselect\fP
544call:
545.DS
d60e8dff
MK
546#define FD_SETSIZE 128 /* How many file descriptors we're interested in */
547 ...
548#include <sys/time.h>
549#include <sys/types.h>
550 ...
551
552fd_set readmask, writemask, exceptmask;
553struct timeval timeout;
554 ...
555select(nfds, &readmask, &writemask, &exceptmask, &timeout);
200989e9 556.DE
d60e8dff 557\fISelect\fP takes as arguments pointers to three sets, one for
200989e9
MK
558the set of file descriptors for which the caller wishes to
559be able to read data on, one for those descriptors to which
560data is to be written, and one for which exceptional conditions
d60e8dff
MK
561are pending; out-of-band data is the only
562exceptional condition currently implemented by the socket
563abstraction.
564If it is known that the
565that the maximum number of open file descriptors will be less than
566a given value,
567then this number should be used as the definition of FD_SETSIZE
568(which must be done before \fI<sys/types>\fP is included).
569Otherwise, it is acceptable to let FD_SETSIZE default to the
570value specified in \fI<sys/types.h>\fP.
571.PP
572Each set is actually a structure containing an array of
573long integers; the length of the array is implicitly set
574by the definition of FD_SETSIZE, and the array will be
575long enough to hold one bit for each of FD_SETSIZE file descriptors.
576If the user is not interested
577in certain conditions (i.e., read, write, or exceptions),
578the corresponding argument to the \fIselect\fP should
579be a null pointer.
580.PP
581The macros \fIFD_SET(fd, &mask)\fP and
582\fIFD_CLR(fd, &mask)\fP
583have been provided for adding and removing file descriptor
584\fIfd\fP in the set \fImask\fP. The
585set should be zeroed before use, and
586the macro \fIFD_ZERO(&mask)\fP has been provided
587to clear the set \fImask\fP.
588The parameter \fInfds\fP in the \fIselect\fP call specifies the range
200989e9 589of file descriptors (i.e. one plus the value of the largest
d60e8dff 590descriptor) specified in a set.
200989e9
MK
591.PP
592A timeout value may be specified if the selection
593is not to last more than a predetermined period of time. If
d60e8dff
MK
594the fields in \fItimeout\fP are set to 0, the selection takes
595the form of a
200989e9
MK
596\fIpoll\fP, returning immediately. If the last parameter is
597a null pointer, the selection will block indefinitely*.
598.FS
599* To be more specific, a return takes place only when a
600descriptor is selectable, or when a signal is received by
601the caller, interrupting the system call.
602.FE
d60e8dff
MK
603\fISelect\fP normally returns the number of file descriptors selected;
604if the \fIselect\fP call returns due to the timeout expiring, then
605the value 0 is returned.
606If the \fIselect\fP terminates because of an error, a \-1 is returned
607with the error number in \fIerrno\fP.
608.PP
609Assuming a successful return, the three sets will
610indicate which
611file descriptors are ready to be read from, written to, or
612have exceptional conditions pending.
613The status of a file descriptor in a select mask may be
614tested with the \fIFD_ISSET(fd, &mask)\fP macro, which
615returns a non-zero value if \fIfd\fP is a member of the set
616\fImask\fP, and 0 if it is not.
617.PP
618To determine if there are connections waiting
619on a socket to be used with an \fIaccept\fP call,
620\fIselect\fP can be used, followed by
621a \fIFD_ISSET(fd, &mask)\fP macro to check for read
622readiness on the appropriate socket. If \fIFD_ISSET\fP
623returns a non-zero value, indicating permission to read, then a
624connection is pending on the socket.
625.PP
626As an example, to read data from two sockets, \fIs1\fP and
627\fIs2\fP as it is available from each and with a one-second
628timeout, the following code
629might be used:
630.DS
631#include <sys/time.h>
632#include <sys/types.h>
633 ...
634fd_set read_template;
635struct timeval wait;
636 ...
637for (;;) {
638 wait.tv_sec = 1; /* one second */
639 wait.tv_usec = 0;
640
641 FD_ZERO(&read_template);
642
643 FD_SET(s1, &read_template);
644 FD_SET(s2, &read_template);
645
646 nb = select(FD_SETSIZE, &read_template, (fd_set *) 0, (fd_set *) 0, &wait);
647 if (nb <= 0) {
648 \fIAn error occurred during the \fPselect\fI, or
649 the \fPselect\fI timed out.\fP
650 }
651
652 if (FD_ISSET(s1, &read_template)) {
653 \fISocket #1 is ready to be read from.\fP
654 }
655
656 if (FD_ISSET(s2, &read_template)) {
657 \fISocket #2 is ready to be read from.\fP
658 }
659}
660.DE
661.PP
662In 4.2, the arguments to \fIselect\fP were pointers to integers
663instead of pointers to \fIfd_set\fPs. This type of call
664will still work as long as the number of file descriptors
665being examined is less than the number of bits in an
666integer; however, the methods illustrated above should
667be used in all current programs.
200989e9
MK
668.PP
669\fISelect\fP provides a synchronous multiplexing scheme.
670Asynchronous notification of output completion, input availability,
671and exceptional conditions is possible through use of the
672SIGIO and SIGURG signals described in section 5.