您的位置:首页 > 其它

缓冲区大小对read,write系统调用效率的影响

2011-09-18 15:11 645 查看
#include <stdio.h>

#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>

//#define SIZE 10

int mycopy(int argcc, char *argvv[], int bufsize);

int main(int argc, char *argv[])
{
int i=0;
int size=10;

for(i=0; i<100; i++)
{
mycopy(argc,argv,size);
size+=20;
}
return 0;
}

int mycopy(int argcc, char *argvv[], int bufsize)
{
int src_file,dest_file;
int nread;
clock_t begin, end;
double cost;
// char buf[SIZE];
char *buf = (char *)malloc(sizeof(char)*bufsize);
if(NULL == buf)
{
perror("fail to malloc");
exit(0);
}
begin = clock();

if(argcc < 3)
{printf("arg is not enough, please input 3 args\n");
exit(0);
}

src_file = open(argvv[1],O_RDONLY);

dest_file = open(argvv[2],O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);

while( (nread = read(src_file, buf, bufsize ))!=0 )
{
if(nread != -1)
{
write(dest_file, buf, nread);
}
else
{perror("fali");}
}
free(buf);
buf=NULL;
close(src_file);
close(dest_file);
end = clock();
cost = (double)(end - begin)/CLOCKS_PER_SEC;

printf("buf size is %5d, ",bufsize);
printf("used %f seconds\n",cost);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息