readvのヘルプ・マニュアル
日本語 英語
readv --help
man readv
READV(2) Linux Programmer’s Manual READV(2)
名前
readv, writev - 複数のバッファへの読み書きを行なう
書式
#include
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
説明
readv() 関数は、ファイルディスクリプタ fd に関連付けられたファイルから
、 iovcnt 個のバッファ分のデータを読み込み、 iov で指定されたバッファに
格納する ("scatter input";「ばらまき入力」)。
writev() 関数は、 iov で指定されたバッファから最大 iovcnt 個のバッファ
分のデータを取り出し、ファイルディスクリプタ fd に関連付けられたファ イ
ルに書き込む ("gather output";「かき集め出力」)。
ポ イ ンタ iov は iovec 構造体の配列へのポインタである。 iovec 構造体は
で以下のように定義されている:
struct iovec {
void *iov_base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
readv() 関数は、複数のバッファにデータを読み込む点を除いて read(2) と全
く同様の動作を行う。
writev() 関数は、複数のバッファのデータを書き出す点以外は write(2) と全
く同様の動作を行う。
バッファは配列の順序で処理される。これは、 readv() が iov[0] が完全に一
杯 になるまでデータを詰めてから、 iov[0] などに進むことを意味する (デー
タが十分ない場合は、 iov が指すバッファのいずれも一杯にならない)。同 様
に、 writev() は iov[0] の内容を全部書き出してから iov[1] などに進む。
readv() と writev() によるデータ転送は atomic に行われる。つまり、
writev() によるデータ書き込みは一つのブロックとして行われ、他のプロセス
の write による書き込みと混ざり合うことはない (例外に関しては pipe(7)
を参照のこと)。同様に、 readv() はファイルから連続するデータブロック が
読 み出すことが保証され、同じファイル記述 (file description; open(2) 参
照) を参照するファイルディスクリプタを持つ他のスレッドやプロセスが実 行
した read 操作の影響を受けることはない。
返り値
成功した場合、 readv() 関数は読み込んだバイト数を返し、 writev() 関数は
書き込んだバイト数を返す。エラーの場合 -1 を返し、errno を適切に設定 す
る。
エラー
read(2) や write(2) と同じエラーが定義されている。さらに以下のエラーが
定義されている:
EINVAL iov_len の合計が ssize_t の範囲をオーバーフローした。もしくは 、
ベ クタ数 iovcnt が 0 より小さいか許可された最大値よりも大きかっ
た。
準拠
4.4BSD (readv() と writev() 関 数 は 4.2BSD で 最 初 に 現 わ れ た)
、POSIX.1-2001。 Linux libc5 では iovcnt 引き数の型として size_t を、こ
れらの関数の返り値として int を使用していた。
注意
Linux での注意
POSIX.1-2001 では、 iov で渡すことができる要素数に上限を設ける実装が 認
められている。実装は、 の IOV_MAX を定義することや、実行時に
sysconf(_SC_IOV_MAX) の返り値経由で、この上限を広告することができ る 。
Linux では、この仕組みにより広告される上限は 1024 であり、この値はカー
ネルでの上限そのものである。一方で、glibc のラッパー関数は、その関数 の
内 部で呼ばれるカーネル・システムコールがこの上限を超過して失敗したこと
を検出すると、追加の動作をする。 readv() の場合、ラッパー関数は iov で
指 定 さ れ た 全 て の要素を格納できる大きさの一時バッファを割り当て、
read(2) を呼び出す際にそのバッファを渡し、そのバッファのデータを iov の
各 要素の iov_base フィールドが指定する場所にコピーしてから、そのバッフ
ァを解放する。 writev() のラッパー関数も、同じように一時バッファを使 っ
て write(2) を呼び出す。
バグ
readv() や writev() のようなファイルディスクリプタに対する操作を行う関
数と、標準入出力ライブラリの関数をごちゃまぜにして呼ぶのはお薦めしな い
。 どんな結果になるかは定義されておらず、おそらく欲しい結果は得られない
だろう。
例
以下のサンプルコードは writev() の使用方法を示すものである。
char *str0 = "hello ";
char *str1 = "world\n";
struct iovec iov[2];
ssize_t nwritten;
iov[0].iov_base = str0;
iov[0].iov_len = strlen(str0);
iov[1].iov_base = str1;
iov[1].iov_len = strlen(str1);
nwritten = writev(STDOUT_FILENO, iov, 2);
関連項目
read(2), write(2)
Linux 2002-10-17 READV(2)
READV(2) Linux Programmer’s Manual READV(2)
NAME
readv, writev, preadv, pwritev - read or write data into multiple
buffers
SYNOPSIS
#include
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t preadv(int fd, const struct iovec *iov, int iovcnt,
off_t offset);
ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt,
off_t offset);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
preadv(), pwritev(): _BSD_SOURCE
DESCRIPTION
The readv() system call reads iovcnt buffers from the file associated
with the file descriptor fd into the buffers described by iov ("scatter
input").
The writev() system call writes iovcnt buffers of data described by iov
to the file associated with the file descriptor fd ("gather output").
The pointer iov points to an array of iovec structures, defined in
as:
struct iovec {
void *iov_base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
The readv() system call works just like read(2) except that multiple
buffers are filled.
The writev() system call works just like write(2) except that multiple
buffers are written out.
Buffers are processed in array order. This means that readv() com-
pletely fills iov[0] before proceeding to iov[1], and so on. (If there
is insufficient data, then not all buffers pointed to by iov may be
filled.) Similarly, writev() writes out the entire contents of iov[0]
before proceeding to iov[1], and so on.
The data transfers performed by readv() and writev() are atomic: the
data written by writev() is written as a single block that is not
intermingled with output from writes in other processes (but see
pipe(7) for an exception); analogously, readv() is guaranteed to read a
contiguous block of data from the file, regardless of read operations
performed in other threads or processes that have file descriptors
referring to the same open file description (see open(2)).
preadv() and pwritev()
The preadv() system call combines the functionality of readv() and
pread(2). It performs the same task as readv(), but adds a fourth
argument, offset, which specifies the file offset at which the input
operation is to be performed.
The pwritev() system call combines the functionality of writev() and
pwrite(2). It performs the same task as writev(), but adds a fourth
argument, offset, which specifies the file offset at which the output
operation is to be performed.
The file offset is not changed by these system calls. The file
referred to by fd must be capable of seeking.
RETURN VALUE
On success, readv() and preadv() return the number of bytes read;
writev() and pwritev() return the number of bytes written. On error,
-1 is returned, and errno is set appropriately.
ERRORS
The errors are as given for read(2) and write(2). Furthermore,
preadv() and pwritev() can also fail for the same reasons as lseek(2).
Additionally, the following error is defined:
EINVAL The sum of the iov_len values overflows an ssize_t value. Or,
the vector count iovcnt is less than zero or greater than the
permitted maximum.
VERSIONS
preadv() and pwritev() first appeared in Linux 2.6.30; library support
was added in glibc 2.10.
CONFORMING TO
readv(), writev(): 4.4BSD (these system calls first appeared in
4.2BSD), POSIX.1-2001. Linux libc5 used size_t as the type of the
iovcnt argument, and int as the return type.
preadv(), pwritev(): nonstandard, but present also on the modern BSDs.
NOTES
Linux Notes
POSIX.1-2001 allows an implementation to place a limit on the number of
items that can be passed in iov. An implementation can advertise its
limit by defining IOV_MAX in or at run time via the return
value from sysconf(_SC_IOV_MAX). On Linux, the limit advertised by
these mechanisms is 1024, which is the true kernel limit. However, the
glibc wrapper functions do some extra work if they detect that the
underlying kernel system call failed because this limit was exceeded.
In the case of readv() the wrapper function allocates a temporary
buffer large enough for all of the items specified by iov, passes that
buffer in a call to read(2), copies data from the buffer to the loca-
tions specified by the iov_base fields of the elements of iov, and then
frees the buffer. The wrapper function for writev() performs the anal-
ogous task using a temporary buffer and a call to write(2).
BUGS
It is not advisable to mix calls to readv() or writev(), which operate
on file descriptors, with the functions from the stdio library; the
results will be undefined and probably not what you want.
EXAMPLE
The following code sample demonstrates the use of writev():
char *str0 = "hello ";
char *str1 = "world\n";
struct iovec iov[2];
ssize_t nwritten;
iov[0].iov_base = str0;
iov[0].iov_len = strlen(str0);
iov[1].iov_base = str1;
iov[1].iov_len = strlen(str1);
nwritten = writev(STDOUT_FILENO, iov, 2);
SEE ALSO
pread(2), read(2), write(2)
COLOPHON
This page is part of release 3.32 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2010-11-17 READV(2)