struct iovec 结构体定义与使用

1. struct iovec概述

struct iovec定义了一个向量元素。通常,这个结构用作一个多元素的数组。对于每一个传输的元素,指针成员iov_base指向一个缓冲区,这个缓冲区是存放的是readv所接收的数据或是writev将要发送的数据。成员iov_len在各种情况下分别确定了接收的最大长度以及实际写入的长度。且iovec结构是用于scatter/gather IO的。readv和writev函数用于在一次函数调用中读、写多个非连续缓冲区。有时也将这两个函数称为散布读(scatter read)和聚集写(gather write)。

Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作。

  • 分散(scatter)从Channel中读取是指在读操作时将读取的数据写入多个buffer中。因此,Channel将从Channel中读取的数据“分散(scatter)”到多个Buffer中。
  • 聚集(gather)写入Channel是指在写操作时将多个buffer的数据写入同一个Channel,因此,Channel 将多个Buffer中的数据“聚集(gather)”后发送到Channel。
 #include <sys/uio.h>
 /* Structure for scatter/gather I/O. */

struct iovec {
    ptr_t iov_base; /* Starting address */
    size_t iov_len; /* Length in bytes */
};

下面主要介绍readv和writev两个函数;readv(2)与writev(2)函数都使用一个I/O向量的概念;在头文件中定义了struct iovec 结构体,其定义和各成员如下:

ssize_t readv(int fd, const struct iovec *iov, int iovcnt);

ssize_t writev(int fd, const struct iovec *iov, int iovcnt);

这两个函数需要三个参数:

  1. 要在其上进行读或是写的文件描述符fd
  2. 读或写所用的I/O向量(vector)
  3. 要使用的向量元素个数(count)

这些函数的返回值是readv所读取的字节数或是writev所写入的字节数。如果有错误发生,就会返回-1,而errno存有错误代码。注意,也其他I/O函数类似,可以返回错误码EINTR来表明他被一个信号所中断。

2. 测试程序

readv:从标准输入读数据,存放在buf1和buf2:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/uio.h>

#define BUF_SIZE 100

int main()
{
    char buf1[BUF_SIZE] = { 0 };
    char buf2[BUF_SIZE] = { 0 };
    char buf3[BUF_SIZE] = { 0 };
    struct iovec iov[3];
    ssize_t nread;

    iov[0].iov_base = buf1;
    iov[0].iov_len = 5;
    iov[1].iov_base = buf2;
    iov[1].iov_len = 8;
    iov[2].iov_base = buf3;
    iov[2].iov_len = BUF_SIZE;

    nread = readv(STDIN_FILENO, iov, 3);
    printf("%ld bytes read.\n", nread);
    printf("buf1: %s\n", buf1);
    printf("buf2: %s\n", buf2);
    printf("buf3: %s", buf3);
    
    return 0;
}

执行结果:

[root@192 mytest]# ./readv
12345HelloWorldIamJason
24 bytes read.
buf1: 12345
buf2: HelloWor
buf3: ldIamJason

writev:将"I" ," am" ," happy.\n", 拼接起来输出的标准输出窗口。

#include<stdio.h>
#include<sys/uio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<string.h>

void sys_err(const char *ptr,int num)
{
    perror(ptr);
    exit(-1);
}

int main(int argc,char **argv)
{
    struct iovec iov[3];
    char *p1 = "I";
    char *p2 = " am";
    char *p3 = " happy.\n";
    iov[0].iov_base = p1;
    iov[0].iov_len = strlen(p1);

    iov[1].iov_base = p2;
    iov[1].iov_len = strlen(p2);

    iov[2].iov_base = p3;
    iov[2].iov_len = strlen(p3);
    ssize_t ret = writev(STDOUT_FILENO,iov,3);
    if(ret < 0)
    {
        sys_err("writev",-1);
    }
    return 0;
}

执行结果:

[root@192 mytest]# ./writev
I am happy.

读写文件:从a.txt读入数据,写入b.txt

#include <stdio.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char buf1[5], buf2[10];
    struct iovec iov[2];
    iov[0].iov_base = buf1;
    iov[0].iov_len = 5;
    iov[1].iov_base = buf2;
    iov[1].iov_len = 10;

    int fd = open("a.txt", O_RDWR);
    if (fd < 0)
    {
        perror("open");
        return -1;
    }
    int rsize = readv(fd, iov, 2); // 从文件a.txt中读取数据,存到iov[2]中的buf1、buf2
    printf("rsize = %d\n", rsize);

    close(fd);

    fd = open("b.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd < 0)
    {
        perror("open");
        return -1;
    }

    int wsize = writev(fd, iov, 2); // 将iov[2]中的buf1、buf2,写入到文件b.txt
    printf("wsize = %d\n", wsize);

    close(fd);
    return 0;
}

执行结果:

[root@192 mytest]# ./rw_test
rsize = 15
wsize = 15

 

  • 13
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值