HP-UX Reference (11i v1 05/09) - 2 System Calls (vol 5)
r
read(2) read(2)
byte-stream mode, read() retrieves data from the
STREAM until as many bytes as were requested are
transferred, or until there is no more data to be retrieved. Byte-stream mode ignores message boundaries.
In
STREAMS message-nondiscard mode, read() retrieves data until as many bytes as were requested are
transferred, or until a message boundary is reached. If
read() does not retrieve all the data in a mes-
sage, the remaining data is left on the
STREAM, and can be retrieved by the next read() call.
Message-discard mode also retrieves data until as many bytes as were requested are transferred, or a mes-
sage boundary is reached. However, unread data remaining in a message after the
read() returns is dis-
carded, and is not available for a subsequent
read(), readv(),orgetmsg() call.
How
read() handles zero-byte STREAMS messages is determined by the current read mode setting. In
byte-stream mode, read() accepts data until it has read nbyte bytes, or until there is no more data to
read, or until a zero-byte message block is encountered. The
read() function then returns the number of
bytes read, and places the zero-byte message back on the
STREAM to be retrieved by the next read(),
readv(),orgetmsg(). In message-nondiscard mode or message-discard mode, a zero-byte message
returns 0 and the message is removed from the
STREAM. When a zero-byte message is read as the first
message on a
STREAM, the message is removed from the
STREAM and 0 is returned, regardless of the read
mode.
A
read() from a STREAMS file returns the data in the message at the front of the
STREAM head read
queue, regardless of the priority band of the message.
By default,
STREAMs are in control-normal mode, in which a read() from a STREAMS
file can only pro-
cess messages that contain a data part but do not contain a control part. The
read() fails if a message
containing a control part is encountered at the
STREAM head. This default action can be changed by plac-
ing the STREAM in either control-data mode or control-discard mode with the
I_SRDOPT ioctl() com-
mand. In control-data mode,
read() converts any control part to data and passes it to the application
before passing any data part originally present in the same message. In control-discard mode, read()
dis-
cards message control parts but returns to the process any data part in the message.
In addition,
read() and readv() will fail if the STREAM head had processed an asynchronous error
before the call. In this case, the value of errno does not reflect the result of
read() or readv() but
reflects the prior error. If a hangup occurs on the
STREAM being read, read()
continues to operate nor-
mally until the
STREAM head read queue is empty. Thereafter, it returns 0.
The readv() function is equivalent to read(), but places the input data into the iovcnt buffers specified
by the members of the iov array: iov[0], iov[1],
..., iov[iovcnt−1]. The iovcnt argument is valid if greater
than 0 and less than or equal to
{IOV_MAX}.
Each iovec entry specifies the base address and length of an area in memory where data should be placed.
The readv() function always fills an area completely before proceeding to the next.
Upon successful completion, readv() marks for update the st_atime field of the file.
The pread() function performs the same action as read(), except that it reads from a given position in
the file without changing the file pointer. The first three arguments of pread() are the same as
read()
with the addition of a fourth argument offset for the desired position inside the file. An attempt to per-
form a pread() on a file that is incapable of seeking results in an error.
RETURN VALUE
Upon successful completion, read(), pread(), and readv() return a non-negative integer indicating
the number of bytes actually read. Otherwise, the functions return −1 and set errno to indicate the error.
ERRORS
The read(), pread() and readv() functions will fail if:
[EAGAIN] The O_NONBLOCK flag is set for the file descriptor and the process would be delayed
in read(), pread() or readv().
[EBADF] The fildes argument is not a valid file descriptor open for reading.
[EBADMSG] The file is a STREAM file that is set to control-normal mode and the message waiting
to be read includes a control part.
[EINTR] The read operation was terminated due to the receipt of a signal, and no data was
transferred.
[EINVAL] The starting file offset associated with fildes is greater than the maximum supported
file size.
Section 2−−266 Hewlett-Packard Company − 2 − HP-UX 11i Version 1: September 2005