```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. nginx 3.nginx+fastcgi

    死磕nginx 3.nginx+fastcgi 互联网服务器有个非常典型的架构lamp(linux+apache+mysql+php),由于其开源和强大的兼容性而风靡一时,不过随着nginx的横空出世 ...

  2. nginx 2.基本配置

    死磕nginx 2.基本配置 鉴于深入浅出的原理,我们先从一个简单的配置了解nginx的配置 1.一个典型配置 nginx的配置文件默认在nginx安装目录的conf二级目录下面,主配置文件为 ngi ...

  3. poj 1988 Cube Stacking && codevs 1540 银河英雄传说(加权并茶几)

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...

  4. Android布局文件-错误

    View requires API level 14 (current min is 8): <?xml version="1.0" encoding="utf-8 ...

  5. 产品经理(PM)常用原型图设计工具

    本文转贴自:http://www.zhangping.name/2010/08/28/pm-wireframes-design-tools/ ,尽管都是一些商业软件,但对设计的确非常有帮助. 天天和产 ...

  6. access 2007 vba 开发中学到的知识(二)

    文件的导入和导出 excel 'excel导入Private Sub btnInExcel_Click() Dim strSelectFile As StringWith Application.Fi ...

  7. uva10020 贪心

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. QT5-控件-QScrollArea-可以用于把一个窗口分割为多个-比如根据图片大小显示滚动条

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #incl ...

  9. Java导入证书失败Keystore was tampered with, or password was incorrect

    keytool 错误: java.io.IOException: Keystore was tampered with, or  password was incorrect   在进行证书相关操作, ...

  10. 发布一款仿天猫产品放大镜JQuery插件

    效果如下图: 1.原图 2.放大镜效果: 插件源码如下: /* * * JQUERY 简洁无极放大镜插件-zoomer * Author:盛世游侠 * QQ:418873053 * Date:2013 ...