#include <sys/uio.h>

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

unix高级环境编程中的定义:

【ssize_t readv(int filedes,const struct iovec iov[ ],int iovcnt) ;
ssize_t writev(int filedes,const struct iovec iov[ ],int iovcnt) ; 】

----------------------------------------------------------------------------

1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <sys/uio.h>
6 #include <unistd.h>
7
8 int main()
9 {
10 struct iovec iov[2];
11 char buf[12],str[12];
12 int fd = open("1.txt",O_RDONLY);
13 if(fd == -1)
14 return -1;
15 iov[0].iov_base = buf;
16 iov[0].iov_len = 12;
17 iov[1].iov_base = str;
18 iov[1].iov_len = 12;
19 int n = readv(fd, iov, 2);

buf[11] = 0;//添加这个

20 printf("n:%d\n", n);
21 //printf("buf:%s\n",buf);
22 //printf("str:%s\n",str);
23 puts(buf);
24 puts(str);
25 close(fd);
26 return 0;
27 }

----------------------------------------------------------------------------

运行结果很奇怪:第一个缓冲区溢出了

tarena@ubuntu:~/mon$ ./a.out
n:23
buf:aaaaaaaaaaaabbbbbbbbbb

str:bbbbbbbbbb

解决办法:

int main()
9 {
10 struct iovec iov[3];
11 char buf[13],str[13],str2[13];
12 int fd = open("1.txt",O_RDONLY);
13 if(fd == -1)
14 return -1;
15 iov[0].iov_base = buf;
16 iov[0].iov_len = 12;
17 iov[1].iov_base = str;
18 iov[1].iov_len = 12;
19 iov[2].iov_base = str2;
20 iov[2].iov_len = 12;
21 int n = readv(fd, iov, 3);
22 buf[12] = 0;
23 str[12] = 0;
24 printf("n:%d\n", n);
25 printf("buf:%s\n",buf);
26 printf("str:%s\n",str);
27 printf("str2:%s\n",str2);
28 close(fd);
29 return 0;
30 }

当你要读入12个的时候申请13个存储空间读入的长度iov_len = 12,并且手动在末尾添加字符串结束标志。

----------------------------------------------------------------------------

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

----------------------------------------------------------------------------

1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <sys/uio.h>
6 #include <unistd.h>
7 #include <string.h>
8 int main()
9 {
10 struct iovec iov[2];
11 char buf[12] = "hello,lyy",str[12] = "nihao,lyy";
12 int fd = open("2.txt",O_WRONLY | O_CREAT);
13 if(fd == -1)
14 return -1;
15 iov[0].iov_base = buf;
16 iov[0].iov_len = strlen(buf);
17 iov[1].iov_base = str;
18 iov[1].iov_len = strlen(str);
19 int n = writev(fd, iov, 2);
20 printf("n:%d\n", n);
21 close(fd);
22 return 0;
23 }

----------------------------------------------------------------------------

运行结果正确,但是为什么需要超级用户才能查看呢?

tarena@ubuntu:~/mon$ sudo cat 2.txt
hello,lyynihao,lyytarena@ubuntu:~/mon$

----------------------------------------------------------------------------

ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);

1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <sys/uio.h>
6 #include <unistd.h>
7 int main()
8 {
9 int fd = open("4.txt",O_RDWR|O_CREAT);
10 if(fd == -1) return -1;
11 struct iovec iov[3];
12 char str1[12],str2[12],str3[12];
13 iov[0].iov_base = str1;
14 iov[0].iov_len = 3;
15 iov[1].iov_base = str2;
16 iov[1].iov_len = 4;
17 iov[2].iov_base = str3;
18 iov[2].iov_len = 5;
19 preadv(fd, iov, 3, 4);
20 str1[iov[0].iov_len] = 0;
21 str2[iov[1].iov_len] = 0;
22 str3[iov[2].iov_len] = 0;
23 printf("str1:%s\nstr2:%s\nstr3:%s\n",str1,str2,str3);
24 close(fd);
25 return 0;
26 }

ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);

1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <sys/uio.h>
6 #include <unistd.h>
7 #include <string.h>
8 int main()
9 {
10 int fd = open("4.txt",O_RDWR|O_CREAT);
11 if(fd == -1) return -1;
12 struct iovec iov[3];
13 char str1[12] = "hello,lyy",str2[12] = "nihao,lyy",str3[36] = "nice to meet you";
14 iov[0].iov_base = str1;
15 iov[0].iov_len = strlen(str1);
16 iov[1].iov_base = str2;
17 iov[1].iov_len = strlen(str2);
18 iov[2].iov_base = str3;
19 iov[2].iov_len = strlen(str3);
20 pwritev(fd, iov, 3, 3);
21 close(fd);
22 return 0;
23 }

--------------------------------------------------------------------------------------------------------

这两个举例不太好举

ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);

flags:RWF_SYNC  RWF_DSYNC

ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);

flags:RWF_NOWAI

---------------------------------------------

RWF_HIPRI只用于下列条件

其中this feature is usable only on a file descriptor opened using the O_DIRECT flag.

---------------------------------------------------------------------------------------------------------

#include <unistd.h>

ssize_t pread(int fd, void *buf, size_t count, off_t offset);

从第offset个开始读

1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7
8 int main()
9 {
10 char buf[12];
11 int fd = open("3.txt",O_RDONLY);
12 int n = pread(fd, buf, 12,3);
13 buf[n] = 0;
14 printf("n = %d buf:%s\n",n,buf);
15 close(fd);
16 return 0;
17 }

返回值比实际读到的多一个,手动buf[n]置为0

-------------------------------------------------------------------------------------

ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

从第offset个开始写,从0开始;(略)

man(2) readv writev的更多相关文章

  1. readv writev示例程序

    当 readv() 时候,需要程序自己提供space,接收数据. #include <stdio.h> #include <stdlib.h> #include <str ...

  2. 套接字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 ...

  3. RMAN备份失败: ORA-19502 & ORA-27072: File I/O error

    早上检查一ORACLE数据库的RMAN备份的邮件时,发现出现了ORA-27072: File I/O error等错误,具体信息如下所示: channel ORA_DISK_1: starting p ...

  4. Linux Socket编程

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  5. Socket通信原理探讨(C++为例)

    一.网络中进程之间如何通信? 本地的进程间通信(IPC)有很多种方式,但可以总结为下面4类: 1.消息传递(管道.FIFO.消息队列) 2.同步(互斥量.条件变量.读写锁.文件和写记录锁.信号量) 3 ...

  6. Linux Socket编程(不限Linux)【转】

    转自:http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几 ...

  7. 字符设备驱动之Led驱动学习记录

    一.概述 Linux内核就是由各种驱动组成的,内核源码中大约有85%的各种渠道程序的代码.一般来说,编写Linux设备驱动大致流程如下: 1.查看原理图,数据手册,了解设备的操作方法. 2.在内核中找 ...

  8. [12]APUE:高级 I/O

    一.分散聚离(向量) I/O [a] readv / writev #include <sys/uio.h> ssize_t readv(int fd, const struct iove ...

  9. SOCKet 编程 简介

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

随机推荐

  1. 30 年前的圣诞节,Python 序章被谱写

    1989 年圣诞节期间,已经从阿姆斯特丹大学(University of Amsterdam)获得数学和计算机硕士学位的 Guido van Rossum,为了打发圣诞节的无趣,决心开发一个新语言解释 ...

  2. SpringBoot:运行原理探究

    西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringBoot ...

  3. TypeScript快速笔记(二)

    1) TypeScript中的判断语句,可以使用非0值代表true.如: function add1(a: number, b?:number): number{ // 注意b是可选参数 consol ...

  4. 【转】mackbook wifi卡死未响应的问题

    原文:http://tieba.baidu.com/p/6140144143?traceid= 1. wifi未响应了,紧急处理法:打开活动监视器,搜索airportd,结束掉进程 2. 彻底解决办法 ...

  5. Ajax初探

    一.AJAX准备知识:JSON 1.stringify与parse方法 2.和XML的比较 二.AJAX简介 AJAX常见应用情景 AJAX的优缺点 优点: 三.jQuery实现的AJAX $.aja ...

  6. shell脚本一一项目6

    主题:获取网卡的流量 ifconfig 查看流量 文件流量数据量 脚本内容 #!/bin/bash#name: mark# check network dev's liuliangnic=$1 ech ...

  7. dataframe指定位置插入行

    1 loc( ) 函数可以定位行后,并直接赋值插入. 如下可见loc函数对直接改变原来行的值 df = pd.DataFrame({ '动物' : ['狗','猫','兔'], '数量' : [ 3, ...

  8. jt获取鼠标指针的位置

    屏幕 screenX和screenY属性表示鼠标在整个显示屏的位置,从屏幕(而不是浏览器)的左上角开始计算的. 页面 pageX和pageY属性表示鼠标指针在这个页面的位置.页面的顶部可能在可见区域之 ...

  9. 在centos7.4 nginx mysql php部署 thinkphp5.0 项目

    系统 centos7  环境 php 7.1.3 nignx 1.12.2 mysql 5.5.6 我是通过lnmp 集成环境安装 fastcgi.conf 末尾添加 vim fastcig.conf ...

  10. 【MM系列】SAP MM模块-移动类型全部列表

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-移动类型全部列表 ...