我想测试一下splice()系统调用的性能,和传统的读/写比较一下。
/* wr.cpp
* it use read/write
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
char buf[BUF_SIZE];
int in = open("1.rmvb",O_RDONLY);
int out = open("1.cp.rmvb",O_WRONLY|O_CREAT,0766);
ssize_t nread;
while( (nread = read(in,buf,BUF_SIZE)) > 0 )
{
write(out,buf,nread);
}
return 0;
}
字符串
//
/* splice.cpp
* it use splice
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
int p[2];
pipe (p);
int in = open("1.rmvb",O_RDONLY);
int out = open("1.cp.rmvb",O_WRONLY|O_CREAT,0766);
ssize_t nread;
while( (nread = splice(in,NULL,p[1],NULL,BUF_SIZE,0)) > 0)
splice(p[0],NULL,out,NULL,BUF_SIZE,0);
return 0;
}
型
结果如下:
x1c 0d1x的数据
1条答案
按热度按时间tquggr8v1#
你确定,你的描述符之一实际上是一个管道?因为,
man splice
说:.它从文件描述符fd_in向文件描述符fd_out传输多达len字节的数据,其中一个描述符必须引用管道。