HP-UX Reference (11i v3 07/02) - 2 System Calls (vol 5)
r
read(2) read(2)
For ordinary files, if the
O_RSYNC|O_DSYNC file status flag is set, the calling process blocks until the data
being read and all file attributes required to retrieve the data are the same as their image on disk. Writes
pending on the data to be read are executed before returning to the calling process. If the
O_RSYNC|O_SYNC file status flag is set, the behavior is identical to that for
O_RSYNC|O_DSYNC with
this addition: all file attributes changed by the read operation (including access time, modification time and
status change time) must also be the same as their image on disk. For block special files, if either the
O_RSYNC|O_DSYNC or O_RSYNC|O_SYNC status flag is set, the calling process blocks until the data
being read is an image of the data on the disk. Writes pending on the data to be read are executed before
returning to the calling process.
Upon successful completion, where nbyte is greater than 0,
read() will mark for update the st_atime field
of the file, and return the number of bytes read. This number will never be greater than nbyte. The value
returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the
read()
request was interrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer than nbyte
bytes immediately available for reading. For example, a
read() from a file associated with a terminal
may return one typed line of data.
If a
read() is interrupted by a signal before it reads any data, it will return −1 with
errno set to
[EINTR].
If a
read() is interrupted by a signal after it has successfully read some data, it will return the number of
bytes read.
A read() from a STREAMS file can read data in three different modes: byte-stream mode,
message-ondiscard mode, and message-discard mode. The default is byte-stream mode. This can be changed
using the I_SRDOPT ioctl() request, and can be tested with the I_GRDOPT ioctl().In
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. The iovec struc-
ture is defined in /usr/include/sys/uio.h.
HP-UX 11i Version 3: February 2007 − 2 − Hewlett-Packard Company 329