来自man writev
readv, writev, preadv, pwritev - read or write data into multiple buffers
读写数据到多个缓冲去
#include <sys/uio.h>
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
The readv() system call reads iovcnt buffers from the file associated with the file descriptor fd into the buffers described by
iov (“scatter input”).
readv()系统调用,从与fd相关的文件描述符,读iovcnt的缓冲数量到iov描述的缓冲区(分散的输入)
The writev() system call writes iovcnt buffers of data described by iov to the file associated with the file descriptor fd
(“gather output”).
writev()系统调用,写iovcnt的缓冲数量(被iov描述的缓冲),通过fd相关的文件描述符。 (聚集输出)
指针iov指向了iovec结构体数组,定义在sys/uio.h文件中。
The pointer iov points to an array of iovec structures,defined in 《sys/uio.h》
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() completely 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.
缓冲区被数组方式的有序化处理。 readv()先处理iov[0],完成处理后,再处理iov[1].如果数据不完整,不是所有的被iov指向的缓冲区都能被填充。 writev()也是类似的, 写完iov[0], 才会轮到iov[1]。
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)).“`
数据被readv()与writev()原子的转换。 数据被writev()作为一个单独的块写入,不会与其他进程中write写入的数据混合。
类似的,readv()从文件描述符中读取一个连续的数据块,而不管其他线程或是进程执行读操作 是否涉及到相同的文件描述符。
[root@net01 test]# cat writev.c
#include <stdio.h>
#include <string.h>
#include <sys/uio.h>
#define STDOUT_FILENO 1
int main()
{
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);
}
[root@net01 test]# gcc writev.c -o wrv
[root@net01 test]# ./wrv
hello world
[root@net01 test]#