pread(2)

πŸ’– pread(2) β€” the system call manual, but make it cute πŸ’–

okay bestie, welcome to the System Calls Manual page for pread(2) ✨ this is the Linux man-pages version, and we're gonna get you so comfy with it. grab a boba, sit down, let's go πŸ§‹πŸ’…


✨ NAME ✨

pread, pwrite β€” read from or write to a file descriptor at a given offset 🎯

so like. you read or you write. but at a specific offset. the way that's iconic honestly 😌


πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) 🫢


🌸 SYNOPSIS 🌸

#include <unistd.h>

ssize_t pread(size_t count;
              int fd, void buf[count], size_t count,
              off_t offset);
ssize_t pwrite(size_t count;
               int fd, const void buf[count], size_t count,
               off_t offset);

πŸŽ€ Feature Test Macro Requirements for glibc πŸŽ€

(see feature_test_macros(7))

  • pread(), pwrite():
    • _XOPEN_SOURCE >= 500
    • || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L

like, if you want these bad boys you GOTTA define the right macros first, it's giving ✨gatekeeping✨ but whatever, that's just how glibc is πŸ’€


πŸ¦‹ DESCRIPTION πŸ¦‹

pread() reads up to count bytes from file descriptor fd at offset offset (from the start of the file) into the buffer starting at buf. The file offset is not changed. πŸ’– that's the whole point, bestie, it does NOT touch your file offset, she's respectful like that.

pwrite() writes up to count bytes from the buffer starting at buf to the file descriptor fd at offset offset. The file offset is not changed. same energy, same slay πŸ”₯

the file referenced by fd must be capable of seeking. no seeking, no service, periodt πŸ™…β€β™€οΈ


πŸ’« RETURN VALUE πŸ’«

on success, pread() returns the number of bytes read (and a return of zero means end of file, so don't panic 🫢) and pwrite() returns the number of bytes written βœ…

ngl it is not an error for a successful call to transfer fewer bytes than you asked for β€” check read(2) and write(2) for the full tea β˜•

on error, -1 is returned and errno is set to indicate the error 😭


πŸ’€ ERRORS πŸ’€

  • pread() can fail and set errno to any error specified for read(2) or lseek(2).
  • pwrite() can fail and set errno to any error specified for write(2) or lseek(2).

like, it's not giving you a custom error list, it's borrowing from its friends. iykyk πŸ’…


πŸ‘‘ STANDARDS πŸ‘‘

POSIX.1-2024. certified, official, main character energy fr fr 🌟


🌈 HISTORY 🌈

POSIX.1-2001. the origin story 🎬

added in Linux 2.1.60; the entries in the i386 system call table were added in Linux 2.1.69. C library support (including emulation using lseek(2) on older kernels without the system calls) was added in glibc 2.1. so she's been around a minute, a real veteran 🫑

🧚 C library/kernel differences 🧚

on Linux, the underlying system calls were renamed in Linux 2.6: pread() became pread64(), and pwrite() became pwrite64(). The system call numbers remained the same. the glibc pread() and pwrite() wrapper functions transparently deal with the change ✨ lowkey magic.

on some 32-bit architectures, the calling signature for these system calls differ, for the reasons described in syscall(2). noted πŸ“


⭐ NOTES ⭐

the pread() and pwrite() system calls are especially useful in multithreaded applications. they let multiple threads perform I/O on the same file descriptor without being affected by changes to the file offset by other threads 🧡✨

like no cap, this is THE reason to use them. your threads stop fighting over one shared offset and honestly? peace at last πŸ•ŠοΈ


πŸ™ˆ BUGS πŸ™ˆ

POSIX requires that opening a file with the O_APPEND flag should have no effect on the location at which pwrite() writes data. However, on Linux, if a file is opened with O_APPEND, pwrite() appends data to the end of the file, regardless of the value of offset. πŸ’€

⚠️ bestie listen to me. on Linux, O_APPEND will OVERRIDE your offset in pwrite(). your carefully calculated offset? ignored. she's appending. do NOT get caught lacking by this one 😭

πŸ’œ SEE ALSO πŸ’œ


that's the whole page babes, go forth and pread() responsibly πŸ¦‹βœ¨

this manual page came from Linux man-pages 6.20, dated 2026-02-08, and the page is, obviously, pread(2) 🌸 mwah, bye bestie πŸ’–πŸ’…πŸ‘‘

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms