|
|
1.1 root 1: .\" Copyright (c) 1983 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.1.t 6.2 (Berkeley) 5/12/86
6: .\"
7: .sh "Generic operations
8: .PP
9: .PP
10: Many system abstractions support the
11: operations \fIread\fP, \fIwrite\fP and \fIioctl\fP. We describe
12: the basics of these common primitives here.
13: Similarly, the mechanisms whereby normally synchronous operations
14: may occur in a non-blocking or asynchronous fashion are
15: common to all system-defined abstractions and are described here.
16: .NH 3
17: Read and write
18: .PP
19: The \fIread\fP and \fIwrite\fP system calls can be applied
20: to communications channels, files, terminals and devices.
21: They have the form:
22: .DS
23: cc = read(fd, buf, nbytes);
24: result int cc; int fd; result caddr_t buf; int nbytes;
25:
26: cc = write(fd, buf, nbytes);
27: result int cc; int fd; caddr_t buf; int nbytes;
28: .DE
29: The \fIread\fP call transfers as much data as possible from the
30: object defined by \fIfd\fP to the buffer at address \fIbuf\fP of
31: size \fInbytes\fP. The number of bytes transferred is
32: returned in \fIcc\fP, which is \-1 if a return occurred before
33: any data was transferred because of an error or use of non-blocking
34: operations.
35: .PP
36: The \fIwrite\fP call transfers data from the buffer to the
37: object defined by \fIfd\fP. Depending on the type of \fIfd\fP,
38: it is possible that the \fIwrite\fP call will accept some portion
39: of the provided bytes; the user should resubmit the other bytes
40: in a later request in this case.
41: Error returns because of interrupted or otherwise incomplete operations
42: are possible.
43: .PP
44: Scattering of data on input or gathering of data for output
45: is also possible using an array of input/output vector descriptors.
46: The type for the descriptors is defined in \fI<sys/uio.h>\fP as:
47: .DS
48: ._f
49: struct iovec {
50: caddr_t iov_msg; /* base of a component */
51: int iov_len; /* length of a component */
52: };
53: .DE
54: The calls using an array of descriptors are:
55: .DS
56: cc = readv(fd, iov, iovlen);
57: result int cc; int fd; struct iovec *iov; int iovlen;
58:
59: cc = writev(fd, iov, iovlen);
60: result int cc; int fd; struct iovec *iov; int iovlen;
61: .DE
62: Here \fIiovlen\fP is the count of elements in the \fIiov\fP array.
63: .NH 3
64: Input/output control
65: .PP
66: Control operations on an object are performed by the \fIioctl\fP
67: operation:
68: .DS
69: ioctl(fd, request, buffer);
70: int fd, request; caddr_t buffer;
71: .DE
72: This operation causes the specified \fIrequest\fP to be performed
73: on the object \fIfd\fP. The \fIrequest\fP parameter specifies
74: whether the argument buffer is to be read, written, read and written,
75: or is not needed, and also the size of the buffer, as well as the
76: request.
77: Different descriptor types and subtypes within descriptor types
78: may use distinct \fIioctl\fP requests. For example,
79: operations on terminals control flushing of input and output
80: queues and setting of terminal parameters; operations on
81: disks cause formatting operations to occur; operations on tapes
82: control tape positioning.
83: .PP
84: The names for basic control operations are defined in \fI<sys/ioctl.h>\fP.
85: .NH 3
86: Non-blocking and asynchronous operations
87: .PP
88: A process that wishes to do non-blocking operations on one of
89: its descriptors sets the descriptor in non-blocking mode as
90: described in section 1.5.4. Thereafter the \fIread\fP call will
91: return a specific EWOULDBLOCK error indication if there is no data to be
92: \fIread\fP. The process may
93: \fIselect\fP the associated descriptor to determine when a read is
94: possible.
95: .PP
96: Output attempted when a descriptor can accept less than is requested
97: will either accept some of the provided data, returning a shorter than normal
98: length, or return an error indicating that the operation would block.
99: More output can be performed as soon as a \fIselect\fP call indicates
100: the object is writeable.
101: .PP
102: Operations other than data input or output
103: may be performed on a descriptor in a non-blocking fashion.
104: These operations will return with a characteristic error indicating
105: that they are in progress
106: if they cannot complete immediately. The descriptor
107: may then be \fIselect\fPed for \fIwrite\fP to find out
108: when the operation has been completed. When \fIselect\fP indicates
109: the descriptor is writeable, the operation has completed.
110: Depending on the nature of the descriptor and the operation,
111: additional activity may be started or the new state may be tested.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.