[root@bogon mycode]# cat writev.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/uio.h>
int main()
{
char *str1="linux
";
char *str2="windows
";
struct iovec iov[2];
iov[0].iov_base=str1;
iov[0].iov_len=strlen(str1);
iov[1].iov_base=str2;
iov[1].iov_len=strlen(str2);
writev(1,iov,2);
return 0;
}
[root@bogon mycode]# gcc writev.c
[root@bogon mycode]# ./a.out
linux
windows
[root@bogon mycode]#
其中iovec结构体如下
struct iovec {
void *iov_base;
size_t iov_len;
};
[root@bogon mycode]# gcc readv.c
[root@bogon mycode]# ./a.out
linuxokno
str1 is linuxok
str2 is no
[root@bogon mycode]# cat readv.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/uio.h>
int main()
{
char buf1[8]={0};
char buf2[8]={0};
struct iovec iov[2];
iov[0].iov_base=buf1;
iov[0].iov_len=sizeof(buf1)-1;
iov[1].iov_base=buf2;
iov[1].iov_len=sizeof(buf2)-1;
readv(0, iov, 2);
printf("str1 is %s
",buf1);
printf("str2 is %s
",buf2);
return 0;
}
[root@bogon mycode]#