pread

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

pread, pwrite - 指定したオフセットでファイルディスクリプターを読み書きする  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#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);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

pread(), pwrite():

    _XOPEN_SOURCE >= 500
        || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
 

説明

pread() は、ファイルディスクリプター fd の (ファイルの先頭からの) オフセット offset から最大 count バイトをバッファー buf へ読み込む。ファイルオフセットは変化しない。

pwrite() は、バッファー buf から最大 count バイトをファイルディスクリプター fd のオフセット offset に書き込む。ファイルオフセットは変化しない。

fd で参照されるファイルはシーク (seek) 可能でなければならない。  

返り値

On success, pread() returns the number of bytes read (a return of zero indicates end of file) and pwrite() returns the number of bytes written.

Note that it is not an error for a successful call to transfer fewer bytes than requested (see read(2) and write(2)).

On error, -1 is returned and errno is set to indicate the error.  

エラー

pread() では、 read(2) および lseek(2) で規定された全てのエラーが発生する可能性があり、 errno にはエラーを示す値が設定される。 pwrite() では、 write(2) および lseek(2) で規定された全てのエラーが発生する可能性があり、 errno にはエラーを示す値が設定される。  

標準規格

POSIX.1-2024.  

歴史

POSIX.1-2001.

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.  

C ライブラリとカーネルの違い

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.

いくつかの 32 ビットアーキテクチャーでは、これらのシステムコールの呼び出し時のシグネチャーが違っています。理由は syscall(2) で説明されている通りです。  

注意

システムコール pread() と pwrite() は、特にマルチスレッドアプリケーションで役に立つ。 これらを使うと、 他のスレッドによるファイルオフセットの変更の影響を受けることなく、 複数のスレッドが同じファイルディスクリプターに対して入出力を行うことができる。  

バグ

POSIX では、O_APPEND フラグを指定してファイルをオープンした場合、 pwrite() がデータを書き込む位置に影響を及ぼさないことが 求められている。しかし、 Linux では、ファイルを O_APPEND 付きで オープンした場合、 offset の値に関わらず、 pwrite() はファイルの末尾にデータを追記する。  

関連項目

lseek(2), read(2), readv(2), write(2)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
標準規格
歴史
C ライブラリとカーネルの違い
注意
バグ
関連項目

This document was created by man2html, using the manual pages.