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

struct iovec {
    void *iov_base;  /* Starting address */
    size_t iov_len;  /* Number of bytes to transfer */
};

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").

```

```cpp
    #include <unistd.h>
    #include <sys/uio.h>
    #include <stdio.h>
    #include <fcntl.h>
    
    int main(int argc, char *argv[])
    {
        ssize_t iSize;
        char acBuf1[9];
        char acBuf2[9];
        struct iovec iov[2];
    
        int iFd1 = open(argv[1], O_RDONLY);
        int iFd2 = open(argv[2], O_RDONLY);
        int iFd3 = open(argv[3], O_WRONLY);
    
        iSize = read(iFd1, acBuf1, sizeof(acBuf1));
        iSize = read(iFd2, acBuf2, sizeof(acBuf2));
    
        iov[0].iov_base = acBuf1;
        iov[0].iov_len = sizeof(acBuf1);
        iov[1].iov_base = acBuf2;
        iov[1].iov_len = sizeof(acBuf2);
    
        iSize = writev(iFd3, iov, 2);
    
        close(iFd1);
        close(iFd2);
        close(iFd3);
        
        return 0;
    }
    
```

建立3个文件(test1, test2, test3)
test1写入12345
test2写入asdfghi
test3为空

运行
./a.out test1 test2 test3

writev/readv的更多相关文章

  1. 套接字I/O函数write/read writev/readv send/recv sendto/recvfrom sendmsg/recvmsg

    函数原型 read/write系原型 #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); #include ...

  2. IO流程中IO向量iovec

    作者:Younger Liu,本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可. 为了提高从磁盘读取数据到内存的效率,引入了IO向量机制,IO向量即struct ...

  3. PHP Socket 编程进阶指南

    学习准备 Linux 或者 Mac 环境: 安装有 Sockets 扩展: 了解 TCP/IP 协议. socket函数只是PHP扩展的一部分,编译PHP时必须在配置中添加 --enable-sock ...

  4. build high performance server 转载

    http://blog.ci123.com/wobushizhanghua/entry/246311 先后查看了haproxy,l7sw和lighttpd的 相关源码,无一例外,他们一致认为多路复用是 ...

  5. Linux----文件I/O

    1.文件描写叙述符:每次我们打开一个文件,就会得到一个相应于该文件的较小的整数,这个整数就是这个文件的文件描写叙述符. 在shell操作中,0,1,2这三个文件描写叙述附总是打开的.一般是指向shel ...

  6. Linux I/O 进阶

    非阻塞I/O 阻塞I/O对应于低速的系统调用,可能会使进程永远阻塞.非阻塞I/O可以使我们发出open.read.write这样的I/O操作,并使这些操作不会永远阻塞.如果这种操作不能完成,则调用立即 ...

  7. apue学习笔记(第十四章 高级I/O)

    本章涵盖了从多概念和函数:非阻塞I/O.记录锁.I/O多路转换.异步I/O.readv和writev函数以及存储映射I/O 非阻塞I/O 非阻塞I/O使我们可以发出open.read和write这样的 ...

  8. 高级I/O之readv和writev函数

    readv和writev函数用于在一次函数调用中读.写多个非连续缓冲区.有时也将这两个函数称为散布读(scatter read)和聚集写(gather write). #include <sys ...

  9. readv和writev函数

    readv 和 writev 函数用于在一次函数调用中读.写多个非连续缓冲区.有时也将这两个函数称为散布读和聚集写. #include <sys/uio.h> ssize_t readv( ...

随机推荐

  1. AS 断点调试 debug

    debug面板 点击下图工具栏开启调试会话 此种调试方式是通过冻结应用运行的状态,仿佛时间停止了一般,然后我们逐一观察此时程序的各个参数是否符合我们的预期. 这种调试方法适用于对时间不敏感的程序.也就 ...

  2. jQuery事件与动画

    一 事件 1 加载DOM事件 $(document).ready():执行时机:DOM元素准备就绪  执行次数:多次  简单写法:原:$(document).ready(function(){})  ...

  3. java里面List和Array的区别是什么?

    java里面的List和Array的区别是什么? 1:数组是定长,list是自动增长.2:数组效率高,list效率低.总结:数组牺牲功能增加效率,list牺牲效率增加功能. http://bbs.cs ...

  4. innodb的innodb_buffer_pool_size和MyISAM的key_buffer_size

    一. key_buffer_size 对MyISAM表来说非常重要. 如果只是使用MyISAM表,可以把它设置为可用内存的 30-40%.合理的值取决于索引大小.数据量以及负载 -- 记住,MyISA ...

  5. 方形布局SquareLayout

    public class SquareLayout extends RelativeLayout { public SquareLayout(Context context, AttributeSet ...

  6. Python基础练习

    近日,因工作需要要学习Python.为了不在语言细节中无法自拔,我按照网上广为流传的<程序员技术练级攻略>中python部分的学习计划,做了三个简单的练习,算是对python有了初步的了解 ...

  7. 'swap file "xx" exists' linux

    solution: 1)swap to another tty, kill processes using 'sudo kill -9 pid' 2)'Recover' the 'warn-openn ...

  8. 微信移动客户端内部浏览器分享到朋友圈,QQ空间代码

    http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html <script src="http://re ...

  9. clang: error: unable to execute command: Segmentation fault: 11

    我在Archive的时候出现了上面这个错误, 解决方法很简单: After huge trying I have disabled the Bitcode in Project's Target-&g ...

  10. pod install后出现: [!] `<PBXResourcesBuildPhase UUID=`xxxx`>` attempted to initialize an object with an unknown UUID

    [!] `<PBXResourcesBuildPhase UUID=`xxx`>` attempted to initialize an object with an unknown UU ...