1998-01-09 09:54:27 +03:00
|
|
|
.\" $NetBSD: 1.5.t,v 1.2 1998/01/09 06:54:44 perry Exp $
|
|
|
|
.\"
|
1997-10-17 13:49:00 +04:00
|
|
|
.\" Copyright (c) 1983, 1993, 1994
|
1994-06-19 04:07:16 +04:00
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the University of
|
|
|
|
.\" California, Berkeley and its contributors.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
1997-10-17 13:49:00 +04:00
|
|
|
.\" @(#)1.5.t 8.6 (Berkeley) 5/29/94
|
1994-06-19 04:07:16 +04:00
|
|
|
.\"
|
1997-10-17 13:49:00 +04:00
|
|
|
.Sh 2 Descriptors
|
|
|
|
.Sh 3 "The reference table
|
1994-06-19 04:07:16 +04:00
|
|
|
.PP
|
|
|
|
Each process has access to resources through
|
|
|
|
\fIdescriptors\fP. Each descriptor is a handle allowing
|
1997-10-17 13:49:00 +04:00
|
|
|
processes to reference objects such as files, devices
|
1994-06-19 04:07:16 +04:00
|
|
|
and communications links.
|
|
|
|
.PP
|
|
|
|
Rather than allowing processes direct access to descriptors, the system
|
|
|
|
introduces a level of indirection, so that descriptors may be shared
|
|
|
|
between processes. Each process has a \fIdescriptor reference table\fP,
|
1997-10-17 13:49:00 +04:00
|
|
|
containing pointers to the actual descriptors.
|
|
|
|
The descriptors themselves therefore may have multiple references,
|
|
|
|
and are reference counted by the system.
|
1994-06-19 04:07:16 +04:00
|
|
|
.PP
|
1997-10-17 13:49:00 +04:00
|
|
|
Each process has a limited size descriptor reference table, where
|
|
|
|
the current size is returned by the
|
|
|
|
.Fn getdtablesize
|
|
|
|
call:
|
1994-06-19 04:07:16 +04:00
|
|
|
.DS
|
1997-10-17 13:49:00 +04:00
|
|
|
.Fd getdtablesize 0 "get descriptor table size
|
1994-06-19 04:07:16 +04:00
|
|
|
nds = getdtablesize();
|
|
|
|
result int nds;
|
|
|
|
.DE
|
1997-10-17 13:49:00 +04:00
|
|
|
and guaranteed to be at least 64.
|
|
|
|
The maximum number of descriptors is a resource limit (see section
|
|
|
|
.Xr 1.6.3 ).
|
|
|
|
The entries in the descriptor reference
|
1994-06-19 04:07:16 +04:00
|
|
|
table are referred to by small integers; for example if there
|
1997-10-17 13:49:00 +04:00
|
|
|
are 64 slots they are numbered 0 to 63.
|
|
|
|
.Sh 3 "Descriptor properties
|
1994-06-19 04:07:16 +04:00
|
|
|
.PP
|
|
|
|
Each descriptor has a logical set of properties maintained
|
|
|
|
by the system and defined by its \fItype\fP.
|
|
|
|
Each type supports a set of operations;
|
|
|
|
some operations, such as reading and writing, are common to several
|
|
|
|
abstractions, while others are unique.
|
1997-10-17 13:49:00 +04:00
|
|
|
For those types that support random access, the current file offset
|
|
|
|
is stored in the descriptor.
|
1994-06-19 04:07:16 +04:00
|
|
|
The generic operations applying to many of these types are described
|
1997-10-17 13:49:00 +04:00
|
|
|
in section
|
|
|
|
.Xr 2.1 .
|
|
|
|
Naming contexts, files and directories are described in section
|
|
|
|
.Xr 2.2 .
|
|
|
|
Section
|
|
|
|
.Xr 2.3
|
|
|
|
describes communications domains and sockets.
|
|
|
|
Terminals and (structured and unstructured) devices are described in section
|
|
|
|
.Xr 2.4 .
|
|
|
|
.Sh 3 "Managing descriptor references
|
|
|
|
.LP
|
|
|
|
A duplicate of a descriptor reference may be made by doing:
|
1994-06-19 04:07:16 +04:00
|
|
|
.DS
|
1997-10-17 13:49:00 +04:00
|
|
|
.Fd dup 1 "duplicate an existing file descriptor
|
1994-06-19 04:07:16 +04:00
|
|
|
new = dup(old);
|
|
|
|
result int new; int old;
|
|
|
|
.DE
|
1997-10-17 13:49:00 +04:00
|
|
|
returning a copy of descriptor reference \fIold\fP which is
|
|
|
|
indistinguishable from the original.
|
|
|
|
The value of \fInew\fP chosen by the system will be the
|
1994-06-19 04:07:16 +04:00
|
|
|
smallest unused descriptor reference slot.
|
|
|
|
A copy of a descriptor reference may be made in a specific slot
|
1997-10-17 13:49:00 +04:00
|
|
|
by doing:
|
1994-06-19 04:07:16 +04:00
|
|
|
.DS
|
1997-10-17 13:49:00 +04:00
|
|
|
.Fd dup2 2 "duplicate an existing file descriptor
|
1994-06-19 04:07:16 +04:00
|
|
|
dup2(old, new);
|
|
|
|
int old, new;
|
|
|
|
.DE
|
1997-10-17 13:49:00 +04:00
|
|
|
The
|
|
|
|
.Fn dup2
|
|
|
|
call causes the system to deallocate the descriptor reference
|
1994-06-19 04:07:16 +04:00
|
|
|
current occupying slot \fInew\fP, if any, replacing it with a reference
|
|
|
|
to the same descriptor as old.
|
1997-10-17 13:49:00 +04:00
|
|
|
.LP
|
|
|
|
Descriptors are deallocated by:
|
1994-06-19 04:07:16 +04:00
|
|
|
.DS
|
1997-10-17 13:49:00 +04:00
|
|
|
.Fd close 1 "delete a descriptor
|
1994-06-19 04:07:16 +04:00
|
|
|
close(old);
|
|
|
|
int old;
|
|
|
|
.DE
|
1997-10-17 13:49:00 +04:00
|
|
|
.Sh 3 "Multiplexing requests
|
1994-06-19 04:07:16 +04:00
|
|
|
.PP
|
|
|
|
The system provides a
|
|
|
|
standard way to do
|
|
|
|
synchronous and asynchronous multiplexing of operations.
|
1997-10-17 13:49:00 +04:00
|
|
|
Synchronous multiplexing is performed by using the
|
|
|
|
.Fn select
|
|
|
|
call to examine the state of multiple descriptors simultaneously,
|
1994-06-19 04:07:16 +04:00
|
|
|
and to wait for state changes on those descriptors.
|
|
|
|
Sets of descriptors of interest are specified as bit masks,
|
|
|
|
as follows:
|
|
|
|
.DS
|
1997-10-17 13:49:00 +04:00
|
|
|
.Fd select 5 "synchronous I/O multiplexing
|
1994-06-19 04:07:16 +04:00
|
|
|
nds = select(nd, in, out, except, tvp);
|
|
|
|
result int nds; int nd; result fd_set *in, *out, *except;
|
|
|
|
struct timeval *tvp;
|
|
|
|
|
|
|
|
FD_CLR(fd, &fdset);
|
1997-10-17 13:49:00 +04:00
|
|
|
FD_COPY(&fdset, &fdset2);
|
1994-06-19 04:07:16 +04:00
|
|
|
FD_ISSET(fd, &fdset);
|
1997-10-17 13:49:00 +04:00
|
|
|
FD_SET(fd, &fdset);
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
int fd; fs_set fdset, fdset2;
|
1994-06-19 04:07:16 +04:00
|
|
|
.DE
|
1997-10-17 13:49:00 +04:00
|
|
|
The
|
|
|
|
.Fn select
|
|
|
|
call examines the descriptors specified by the
|
1994-06-19 04:07:16 +04:00
|
|
|
sets \fIin\fP, \fIout\fP and \fIexcept\fP, replacing
|
|
|
|
the specified bit masks by the subsets that select true for input,
|
|
|
|
output, and exceptional conditions respectively (\fInd\fP
|
|
|
|
indicates the number of file descriptors specified by the bit masks).
|
|
|
|
If any descriptors meet the following criteria,
|
|
|
|
then the number of such descriptors is returned in \fInds\fP and the
|
|
|
|
bit masks are updated.
|
|
|
|
.if n .ds bu *
|
|
|
|
.if t .ds bu \(bu
|
|
|
|
.IP \*(bu
|
|
|
|
A descriptor selects for input if an input oriented operation
|
1997-10-17 13:49:00 +04:00
|
|
|
such as
|
|
|
|
.Fn read
|
|
|
|
or
|
|
|
|
.Fn receive
|
|
|
|
is possible, or if a connection request may be accepted (see sections
|
|
|
|
.Xr 2.1.3
|
|
|
|
and
|
|
|
|
.Xr 2.3.1.4 ).
|
1994-06-19 04:07:16 +04:00
|
|
|
.IP \*(bu
|
|
|
|
A descriptor selects for output if an output oriented operation
|
1997-10-17 13:49:00 +04:00
|
|
|
such as
|
|
|
|
.Fn write
|
|
|
|
or
|
|
|
|
.Fn send
|
|
|
|
is possible, or if an operation
|
1994-06-19 04:07:16 +04:00
|
|
|
that was ``in progress'', such as connection establishment,
|
1997-10-17 13:49:00 +04:00
|
|
|
has completed (see sections
|
|
|
|
.Xr 2.1.3
|
|
|
|
and
|
|
|
|
.Xr 2.3.1.5 ).
|
1994-06-19 04:07:16 +04:00
|
|
|
.IP \*(bu
|
|
|
|
A descriptor selects for an exceptional condition if a condition
|
1997-10-17 13:49:00 +04:00
|
|
|
that would cause a SIGURG signal to be generated exists (see section
|
|
|
|
.Xr 1.3.2 ),
|
1994-06-19 04:07:16 +04:00
|
|
|
or other device-specific events have occurred.
|
|
|
|
.LP
|
1997-10-17 13:49:00 +04:00
|
|
|
For these tests, an operation is considered to be possible if a call
|
|
|
|
to the operation would return without blocking (even if the O_NONBLOCK
|
|
|
|
flag were not set).
|
|
|
|
For example, a descriptor would test as ready for reading if a read
|
|
|
|
call would return immediately with data, an end-of-file indication,
|
|
|
|
or an error other than EWOULDBLOCK.
|
|
|
|
.LP
|
1994-06-19 04:07:16 +04:00
|
|
|
If none of the specified conditions is true, the operation
|
|
|
|
waits for one of the conditions to arise,
|
|
|
|
blocking at most the amount of time specified by \fItvp\fP.
|
1997-10-17 13:49:00 +04:00
|
|
|
If \fItvp\fP is given as NULL, the
|
|
|
|
.Fn select
|
|
|
|
waits indefinitely.
|
|
|
|
.LP
|
1994-06-19 04:07:16 +04:00
|
|
|
Options affecting I/O on a descriptor
|
|
|
|
may be read and set by the call:
|
|
|
|
.DS
|
1997-10-17 13:49:00 +04:00
|
|
|
.Fd fcntl 3 "file control
|
|
|
|
dopt = fcntl(d, cmd, arg);
|
1994-06-19 04:07:16 +04:00
|
|
|
result int dopt; int d, cmd, arg;
|
1997-10-17 13:49:00 +04:00
|
|
|
.DE
|
|
|
|
.DS
|
|
|
|
.TS
|
|
|
|
l s
|
|
|
|
l l.
|
|
|
|
/* command values */
|
1994-06-19 04:07:16 +04:00
|
|
|
|
1997-10-17 13:49:00 +04:00
|
|
|
F_DUPFD /* return a new descriptor */
|
|
|
|
F_GETFD /* get file descriptor flags */
|
|
|
|
F_SETFD /* set file descriptor flags */
|
|
|
|
F_GETFL /* get file status flags */
|
|
|
|
F_SETFL /* set file status flags */
|
|
|
|
F_GETOWN /* get SIGIO/SIGURG proc/pgrp */
|
|
|
|
F_SETOWN /* set SIGIO/SIGURG proc/pgrp */
|
|
|
|
F_GETLK /* get blocking lock */
|
|
|
|
F_SETLK /* set or clear lock */
|
|
|
|
F_SETLKW /* set lock with wait */
|
|
|
|
.TE
|
1994-06-19 04:07:16 +04:00
|
|
|
.DE
|
1997-10-17 13:49:00 +04:00
|
|
|
The F_DUPFD \fIcmd\fP provides identical functionality to
|
|
|
|
.Fn dup2 ;
|
|
|
|
it is provided solely for POSIX compatibility.
|
|
|
|
The F_SETFD \fIcmd\fP can be used to set the close-on-exec
|
|
|
|
flag for a file descriptor.
|
1994-06-19 04:07:16 +04:00
|
|
|
The F_SETFL \fIcmd\fP may be used to set a descriptor in
|
1997-10-17 13:49:00 +04:00
|
|
|
non-blocking I/O mode and/or enable signaling when I/O is possible.
|
|
|
|
F_SETOWN may be used to specify a process or process
|
1994-06-19 04:07:16 +04:00
|
|
|
group to be signaled when using the latter mode of operation
|
|
|
|
or when urgent indications arise.
|
1997-10-17 13:49:00 +04:00
|
|
|
The
|
|
|
|
.Fn fcntl
|
|
|
|
system call also provides POSIX-compliant byte-range locking on files.
|
|
|
|
However the semantics of unlocking on every
|
|
|
|
.Fn close
|
|
|
|
rather than last close makes them useless.
|
|
|
|
Much better semantics and faster locking are provided by the
|
|
|
|
.Fn flock
|
|
|
|
system call (see section
|
|
|
|
.Xr 2.2.7 ).
|
|
|
|
The
|
|
|
|
.Fn fcntl
|
|
|
|
and
|
|
|
|
.Fn flock
|
|
|
|
locks can be used concurrently;
|
|
|
|
they will serialize against each other properly.
|
1994-06-19 04:07:16 +04:00
|
|
|
.PP
|
|
|
|
Operations on non-blocking descriptors will
|
|
|
|
either complete immediately,
|
1997-10-17 13:49:00 +04:00
|
|
|
return the error EWOULDBLOCK,
|
1994-06-19 04:07:16 +04:00
|
|
|
partially complete an input or output operation returning a partial count,
|
|
|
|
or return an error EINPROGRESS noting that the requested operation is
|
|
|
|
in progress.
|
|
|
|
A descriptor which has signalling enabled will cause the specified process
|
|
|
|
and/or process group
|
|
|
|
be signaled, with a SIGIO for input, output, or in-progress
|
|
|
|
operation complete, or
|
|
|
|
a SIGURG for exceptional conditions.
|
|
|
|
.PP
|
|
|
|
For example, when writing to a terminal
|
|
|
|
using non-blocking output,
|
1997-10-17 13:49:00 +04:00
|
|
|
the system will accept only as much data as there is buffer space,
|
|
|
|
then return.
|
|
|
|
When making a connection on a \fIsocket\fP, the operation may
|
1994-06-19 04:07:16 +04:00
|
|
|
return indicating that the connection establishment is ``in progress''.
|
1997-10-17 13:49:00 +04:00
|
|
|
The
|
|
|
|
.Fn select
|
|
|
|
facility can be used to determine when further
|
1994-06-19 04:07:16 +04:00
|
|
|
output is possible on the terminal, or when the connection establishment
|
|
|
|
attempt is complete.
|