READ(2)
π READ(2) β read input π
okay bestie, welcome to section 2 of the system calls manual, the part where we talk to the kernel directly and it actually answers. this is READ(2), aka the four functions that get bytes out of a file descriptor and into your little buffer. group chat is open, let's go π«Ά
β¨ NAME β¨
read, readv, pread, preadv β read input
π LIBRARY π
Standard C Library (libc, -lc)
πΈ SYNOPSIS πΈ
#include <unistd.h>
ssize_t
read(int fd, void *buf, size_t nbytes);
ssize_t
pread(int fd, void *buf, size_t nbytes, off_t offset);
#include <sys/uio.h>
ssize_t
readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t
preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
π DESCRIPTION π
so. the read() system call is out here trying to read nbytes of data from the object that the descriptor fd points at, and it drops that data into the buffer that buf points at. that's it, that's the whole job. iconic behavior honestly π
then readv() does the exact same thing but it scatters β the input data gets spread across the iovcnt buffers listed in the members of the iov array: iov[0], iov[1], ..., iov[iovcnt-1]. it's giving "one call, many destinations", we love a multitasker β¨
and pread() + preadv() do those same functions but they read from the specified position in the file without touching the file pointer. no pointer drama. none. π
π¦ the iovec situation π¦
for readv() and preadv(), the iovec structure is defined like this:
struct iovec {
void *iov_base; /* Base address. */
size_t iov_len; /* Length. */
};
every single iovec entry says "here's the base address and here's the length of a chunk of memory where data should go". and readv() will always fill an area completely before it moves on to the next one. no half-filled vibes, no crumbs, full commitment only. π―
π the pointer lore π
on objects that are capable of seeking, read() starts at the position given by the pointer associated with fd β go check lseek(2) for that whole saga. and once read() returns, that pointer gets incremented by however many bytes were actually read. real ones know π
objects that are not capable of seeking? they always read from the current position, and the value of the pointer associated with such an object is undefined. don't ask, it's undefined, we don't look at it. π
β the return guarantee β
upon successful completion, read(), readv(), pread() and preadv() all return the number of bytes actually read and placed in the buffer.
here's the tea though: 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. and in no other case. that's the whole guarantee, bestie, don't extrapolate. π
π atomicity, because we're serious now π
in accordance with IEEE Std 1003.1-2004 (βPOSIX.1β), both read(2) and write(2) syscalls are atomic with respect to each other in the effects on file content, when they operate on regular files.
if two threads each call one of the read(2) or write(2) syscalls, each call will see either all of the changes of the other call, or none of them. no in-between, no "oops i saw half a write". the FreeBSD kernel implements this guarantee by locking the file ranges affected by the calls. periodt. π
π RETURN VALUES π
if it worked, you get the number of bytes actually read. if you hit end-of-file, you get zero. otherwise you get a -1 and the global variable errno is set to tell you what went wrong. classic. π
π ERRORS π
the read(), readv(), pread() and preadv() system calls will succeed unless:
[EBADF]β thefdargument is not a valid file or socket descriptor open for reading. like, bestie, that's not a thing you can read from.[ECONNRESET]β thefdargument refers to a socket, and the remote socket end is forcibly closed. the other side just hung up on you. rude. π€[EFAULT]β thebufargument points outside the allocated address space. you pointed at nothing, girl.[EIO]β an I/O error occurred while reading from the file system.[EINTEGRITY]β corrupted data was detected while reading from the file system. the data arrived and it was wrong. π[EBUSY]β failed to read from a file, e.g./proc/<pid>/regswhile<pid>is not stopped.[EINTR]β a read from a slow device (i.e. one that might block for an arbitrary amount of time) was interrupted by the delivery of a signal before any data arrived. not you getting jumpscared by a signal mid-read π[EINVAL]β the pointer associated withfdwas negative.[EAGAIN]β the file was marked for non-blocking I/O, and no data were ready to be read. nothing's here yet, come back later.[EISDIR]β the file descriptor is associated with a directory. directories may only be read directly if the filesystem supports it and thesecurity.bsd.allow_read_dirsysctl MIB is set to a non-zero value. for most scenarios, thereaddir(3)function should be used instead. read the room, read the directory with the right function π[EOPNOTSUPP]β the file descriptor is associated with a file system and file type that do not allow regular read operations on it.[EOVERFLOW]β the file descriptor is associated with a regular file,nbytesis greater than 0,offsetis before the end-of-file, andoffsetis greater than or equal to the offset maximum established for this file system.[EINVAL]β the valuenbytesis greater thanINT_MAX.
π and also, for readv() and preadv() π
in addition, readv() and preadv() may return one of the following errors:
[EINVAL]β theiovcntargument was less than or equal to 0, or greater thanIOV_MAX.[EINVAL]β one of theiov_lenvalues in theiovarray was negative.[EINVAL]β the sum of theiov_lenvalues in theiovarray overflowed a 32-bit integer.[EFAULT]β part of theiovarray points outside the process's allocated address space.
π§ and for pread() and preadv() π§
the pread() and preadv() system calls may also return the following errors:
[EINVAL]β theoffsetvalue was negative.[ESPIPE]β the file descriptor is associated with a pipe, socket, or FIFO. no seeking for you, babe.
π«Ά SEE ALSO π«Ά
dup(2), fcntl(2), getdirentries(2), open(2), pipe(2), select(2), socket(2), socketpair(2), fread(3), readdir(3)
π STANDARDS π
the read() system call is expected to conform to ISO/IEC 9945-1:1990 (βPOSIX.1β).
the readv() and pread() system calls are expected to conform to X/Open Portability Guide Issue 4, Version 2 (βXPG4.2β).
π°οΈ HISTORY π°οΈ
- the
preadv()system call appeared in FreeBSD 6.0. - the
pread()function appeared in AT&T System V Release 4 UNIX. - the
readv()system call appeared in 4.2BSD. - the
read()function appeared in Version 1 AT&T UNIX.
okay that's the whole page, bestie, you ate it up π
this one came from the Debian crew, dated March 30, 2020, and it's filed right here in READ(2). go forth and read responsibly, mwah πβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms