nginxのwritev関数を利用するサンプルコード

説明:
#include <sys/uio.h>
ssize_t readv(int fd, const struct iovec *vector, int count);
ssize_t writev(int fd, const struct iovec *vector, int count);

コード:
#include <stdio.h>
#include <fcntl.h>
#include <sys/uio.h>
int main(int argc,char *argv[])
{
int fd1,fd2,fd3;
ssize_t size;
char buf1[9],buf2[9];
struct iovec iov[2];

fd1=open(argv[1],O_RDONLY);
fd2=open(argv[2],O_RDONLY);
fd3=open(argv[3],O_RDWR);

size=read(fd1,buf1,sizeof(buf1));
printf(“%s size is : %d\n",argv[1],size);

size=read(fd2,buf2,sizeof(buf2));
printf(“%s size is : %d\n",argv[2],size);

iov[0].iov_base=buf1;
iov[0].iov_len=sizeof(buf1);
iov[1].iov_base=buf2;
iov[1].iov_len=sizeof(buf2);
size=writev(fd3,iov,2);
printf(“%s size is :%d\n",argv[3],size);
close(fd1);
close(fd2);
close(fd3);
}

C++

Posted by arkgame