Merge branch 'vendor/TCSH'
[dragonfly.git] / share / doc / psd / 05.sysman / 2.1.t
1 .\" Copyright (c) 1983, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)2.1.t       8.1 (Berkeley) 6/8/93
29 .\"
30 .sh "Generic operations
31 .PP
32 .PP
33 Many system abstractions support the
34 operations \fIread\fP, \fIwrite\fP and \fIioctl\fP.  We describe
35 the basics of these common primitives here.
36 Similarly, the mechanisms whereby normally synchronous operations
37 may occur in a non-blocking or asynchronous fashion are
38 common to all system-defined abstractions and are described here.
39 .NH 3
40 Read and write
41 .PP
42 The \fIread\fP and \fIwrite\fP system calls can be applied
43 to communications channels, files, terminals and devices.
44 They have the form:
45 .DS
46 cc = read(fd, buf, nbytes);
47 result int cc; int fd; result caddr_t buf; int nbytes;
48
49 cc = write(fd, buf, nbytes);
50 result int cc; int fd; caddr_t buf; int nbytes;
51 .DE
52 The \fIread\fP call transfers as much data as possible from the
53 object defined by \fIfd\fP to the buffer at address \fIbuf\fP of
54 size \fInbytes\fP.  The number of bytes transferred is
55 returned in \fIcc\fP, which is \-1 if a return occurred before
56 any data was transferred because of an error or use of non-blocking
57 operations.
58 .PP
59 The \fIwrite\fP call transfers data from the buffer to the
60 object defined by \fIfd\fP.  Depending on the type of \fIfd\fP,
61 it is possible that the \fIwrite\fP call will accept some portion
62 of the provided bytes; the user should resubmit the other bytes
63 in a later request in this case.
64 Error returns because of interrupted or otherwise incomplete operations
65 are possible.
66 .PP
67 Scattering of data on input or gathering of data for output
68 is also possible using an array of input/output vector descriptors.
69 The type for the descriptors is defined in \fI<sys/uio.h>\fP as:
70 .DS
71 ._f
72 struct iovec {
73         caddr_t iov_msg;        /* base of a component */
74         int     iov_len;        /* length of a component */
75 };
76 .DE
77 The calls using an array of descriptors are:
78 .DS
79 cc = readv(fd, iov, iovlen);
80 result int cc; int fd; struct iovec *iov; int iovlen;
81
82 cc = writev(fd, iov, iovlen);
83 result int cc; int fd; struct iovec *iov; int iovlen;
84 .DE
85 Here \fIiovlen\fP is the count of elements in the \fIiov\fP array.
86 .NH 3
87 Input/output control
88 .PP
89 Control operations on an object are performed by the \fIioctl\fP
90 operation:
91 .DS
92 ioctl(fd, request, buffer);
93 int fd, request; caddr_t buffer;
94 .DE
95 This operation causes the specified \fIrequest\fP to be performed
96 on the object \fIfd\fP.  The \fIrequest\fP parameter specifies
97 whether the argument buffer is to be read, written, read and written,
98 or is not needed, and also the size of the buffer, as well as the
99 request.
100 Different descriptor types and subtypes within descriptor types
101 may use distinct \fIioctl\fP requests.  For example,
102 operations on terminals control flushing of input and output
103 queues and setting of terminal parameters; operations on
104 disks cause formatting operations to occur; operations on tapes
105 control tape positioning.
106 .PP
107 The names for basic control operations are defined in \fI<sys/ioctl.h>\fP.
108 .NH 3
109 Non-blocking and asynchronous operations
110 .PP
111 A process that wishes to do non-blocking operations on one of
112 its descriptors sets the descriptor in non-blocking mode as
113 described in section 1.5.4.  Thereafter the \fIread\fP call will
114 return a specific EWOULDBLOCK error indication if there is no data to be
115 \fIread\fP.  The process may
116 \fIselect\fP the associated descriptor to determine when a read is
117 possible.
118 .PP
119 Output attempted when a descriptor can accept less than is requested
120 will either accept some of the provided data, returning a shorter than normal
121 length, or return an error indicating that the operation would block.
122 More output can be performed as soon as a \fIselect\fP call indicates
123 the object is writeable.
124 .PP
125 Operations other than data input or output
126 may be performed on a descriptor in a non-blocking fashion.
127 These operations will return with a characteristic error indicating
128 that they are in progress
129 if they cannot complete immediately.  The descriptor
130 may then be \fIselect\fPed for \fIwrite\fP to find out
131 when the operation has been completed.  When \fIselect\fP indicates
132 the descriptor is writeable, the operation has completed.
133 Depending on the nature of the descriptor and the operation,
134 additional activity may be started or the new state may be tested.