linux 为什么splice()在我的系统上执行得如此糟糕?

0yg35tkg  于 5个月前  发布在  Linux
关注(0)|答案(1)|浏览(66)

我想测试一下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的数据

spilce()似乎没有提高性能,也没有减少CPU时间,为什么?我的内核版本是2.6.35-28,ubuntu 10.10。

tquggr8v

tquggr8v1#

你确定,你的描述符之一实际上是一个管道?因为,man splice说:
.它从文件描述符fd_in向文件描述符fd_out传输多达len字节的数据,其中一个描述符必须引用管道。

相关问题