.\" Copyright (c) 1983, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\" %sccs.include.redist.roff%
.\" @(#)1.5.t 8.5 (Berkeley) %G%
.Sh 3 "The reference table
Each process has access to resources through
\fIdescriptors\fP. Each descriptor is a handle allowing
processes to reference objects such as files, devices
and communications links.
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,
containing pointers to the actual descriptors.
The descriptors themselves therefore may have multiple references,
and are reference counted by the system.
Each process has a limited size descriptor reference table, where
the current size is returned by the
.Fd getdtablesize 0 "get descriptor table size
and guaranteed to be at least 64.
The maximum number of descriptors is a resource limit (see section
The entries in the descriptor reference
table are referred to by small integers; for example if there
are 20 slots they are numbered 0 to 19.
.Sh 3 "Descriptor properties
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.
For those types that support random access, the current file offset
is stored in the descriptor.
The generic operations applying to many of these types are described
Naming contexts, files and directories are described in section
describes communications domains and sockets.
Terminals and (structured and unstructured) devices are described in section
.Sh 3 "Managing descriptor references
A duplicate of a descriptor reference may be made by doing:
.Fd dup 1 "duplicate an existing file descriptor
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
smallest unused descriptor reference slot.
A copy of a descriptor reference may be made in a specific slot
.Fd dup2 2 "duplicate an existing file descriptor
call causes the system to deallocate the descriptor reference
current occupying slot \fInew\fP, if any, replacing it with a reference
to the same descriptor as old.
Descriptors are deallocated by:
.Fd close 1 "delete a descriptor
.Sh 3 "Multiplexing requests
synchronous and asynchronous multiplexing of operations.
Synchronous multiplexing is performed by using the
call to examine the state of multiple descriptors simultaneously,
and to wait for state changes on those descriptors.
Sets of descriptors of interest are specified as bit masks,
.Fd select 5 "synchronous I/O multiplexing
nds = select(nd, in, out, except, tvp);
result int nds; int nd; result fd_set *in, *out, *except;
FD_COPY(&fdset, &fdset2);
int fd; fs_set fdset, fdset2;
call examines the descriptors specified by the
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
A descriptor selects for input if an input oriented operation
is possible, or if a connection request may be accepted (see sections
A descriptor selects for output if an output oriented operation
is possible, or if an operation
that was ``in progress'', such as connection establishment,
has completed (see sections
A descriptor selects for an exceptional condition if a condition
that would cause a SIGURG signal to be generated exists (see section
or other device-specific events have occurred.
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
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.
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.
If \fItvp\fP is given as NULL, the
Options affecting I/O on a descriptor
may be read and set by the call:
.Fd fcntl 3 "file control
dopt = fcntl(d, cmd, arg);
result int dopt; int d, cmd, arg;
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 */
The F_SETFD \fIcmd\fP can be used to set the close-on-exec
flag for a file descriptor.
The F_SETFL \fIcmd\fP may be used to set a descriptor in
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
group to be signaled when using the latter mode of operation
or when urgent indications arise.
system call also provides POSIX-compliant byte-range locking on files.
However the semantics of unlocking on every
rather than last close makes them useless.
Much better semantics and faster locking are provided by the
locks can be used concurrently;
they will serialize against each other properly.
Operations on non-blocking descriptors will
either complete immediately,
note an error EWOULDBLOCK,
partially complete an input or output operation returning a partial count,
or return an error EINPROGRESS noting that the requested operation is
A descriptor which has signalling enabled will cause the specified process
be signaled, with a SIGIO for input, output, or in-progress
a SIGURG for exceptional conditions.
For example, when writing to a terminal
using non-blocking output,
the system will accept only as much data as there is buffer space,
When making a connection on a \fIsocket\fP, the operation may
return indicating that the connection establishment is ``in progress''.
facility can be used to determine when further
output is possible on the terminal, or when the connection establishment