READ
Section: System Calls (2)
Updated: August 1, 1992
Index
Return to Main Contents
NAME
read, readv - read input
BSD SYNOPSIS
int read(int fd, char *buf, int nbytes);
#include <sys/types.h>
#include <sys/uio.h>
int readv(int fd, struct iovec *iov, int iovcnt);
POSIX SYNOPSIS
size_t read(int fd, const void *buf, size_t nbytes);
DESCRIPTION
The
read
function attempts to read
nbytes
of data from the object
referenced by the descriptor
fd
into the buffer pointed to by
buf.
With objects capable of seeking,
read
starts at the position
given by the seek pointer
associated with
fd
(see
lseek(2)).
Upon return,
the seek pointer for
fd
is incremented by the number of bytes actually read.
Objects that are not capable of seeking
always read from their current position.
The value of the pointer associated
with such an object is undefined.
Upon successful completion,
read
returns the number of bytes
actually read and placed in the buffer.
The system guarantees to read
the number of bytes requested
if the descriptor references a normal file
that has that many bytes left before the end-of-file,
but in no other case.
If the returned value is 0,
then end-of-file has been reached.
When
read
returns greater than zero,
the st_atime field of the
file is updated.
The
readv
function performs a scattered read,
puting input data into the
iovcnt
buffers specified by the members of the
iov
array:
iov[0],
iov[1],
iov[
iovcnt
- 1].
The return value of
readv
is the same as that for
read.
The
iovec
structure is defined as
- struct iovec {
caddr_t iov_base;
int iov_len;
};
Each
iovec
entry specifies the base address and length of an area
in memory where data should be placed.
Readv
will always fill an area completely before proceeding
to the next.
POSIX applications should not use
readv.
POSIX applications may expect the following behavior
when attempting to
read
(but not
readv)
from an empty pipe or FIFO:
- (1)
-
If no process has the pipe open for writing,
read
returns zero to indicate end-of-file.
- (2)
-
If some process has the pipe open for writing
and O_NONBLOCK is set,
read
returns -1 and sets
errno
to [EAGAIN].
- (3)
-
If some process has the pipe open for writing
and O_NONBLOCK is clear,
read
will block until some data is written
or the pipe is closed by all
processes that had the pipe open for writing.
POSIX applications may expect the following behavior
when attempting to read a file
(other than a pipe or FIFO)
that supports non-blocking reads
and has no data currently available:
- (1)
-
If O_NONBLOCK is set, read returns -1 and sets errno to
[EAGAIN].
- (2)
-
If O_NONBLOCK is clear, read blocks until some data becomes available.
RETURN VALUE
If successful, the number of bytes actually read is returned. Otherwise, a
-1 is returned and the global variable errno is set to indicate the
error.
ERRORS
Read will fail if one or more of the following are true:
- [EAGAIN]
-
(POSIX only)
The O_NONBLOCK flag is set for the file descriptor and the process would be
delayed in the read operation.
- [EBADF]
-
fd is not a valid file or socket descriptor open for reading.
- [EFAULT]
-
buf points outside the allocated address space.
- [EIO]
-
An I/O error occurred while reading from the file system;
or,
the process is in a background process group and is attempting to read from its
controlling terminal, and either the process is ignoring or blocking the
SIGTTIN signal or the process group of the process is orphaned.
- [EINTR]
-
A read from a slow device was interrupted before
any data arrived by the delivery of a signal.
- [EINVAL]
-
The pointer associated with fd was negative;
or,
An attempt was made to read greater than SSIZE_MAX bytes.
- [EWOULDBLOCK]
-
The file was marked for non-blocking I/O by
fcntl(2),
and no data were ready to be read.
In addition,
the following errors may occur with
readv:
- [EFAULT]
-
Part of the iov points outside the process's allocated address space.
- [EINVAL]
-
Iovcnt
was less than or equal to 0, or greater than 16.;
or,
one of the
iov_len
values in the
iov
array was negative;
or,
the sum of the
iov_len
values in the
iov
array overflowed a 32-bit integer.
SEE ALSO
creat(2), dup(2), fcntl(2), lseek(2), open(2), pipe(2)
Index
- NAME
-
- BSD SYNOPSIS
-
- POSIX SYNOPSIS
-
- DESCRIPTION
-
- RETURN VALUE
-
- ERRORS
-
- SEE ALSO
-
This document was created by
man2html,
using the manual pages.
Time: 17:22:43 GMT, March 25, 2025