缓冲区大小对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;
}
#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;
}
相关文章推荐
- 常用系统调用write、read、ioctl
- linux中的read和write系统调用
- read和write系统调用以及getchar的实现
- 文件编程中的两套操作:标准C库(fread, fwrite,fclose,fopen...)和 系统调用(open, read, write...)
- 系统调用——open、write、read和close
- 标准IO流与系统调用write read区别
- 【Linux&Unix--open/close/write/read系统调用】
- 四种读写文件的方式:系统调用(open/read/write),C语言(fopen,fgets, fputs),C++(ifstream, ofstream getline,)泛型算法
- 系统调用read,write和标准库函数IO
- 简介几种系统调用函数:write、read、open、close、ioctl
- read和write系统调用
- 了解open/read/write/close等文件相关系统调用接口,纵向对比fd与FILE结构体
- linux系统编程之文件与IO(二):系统调用read和write
- Nachos-3.4 系统调用 实现Create Open Read Write Close
- 系统调用read,write和标准库函数IO
- 汇编语言调用Linux系统调用read和write
- Linux系统调用及用户编程接口(API) open close write read lseek
- eCryptfs - read/write系统调用
- 关于系统调用open,write,read,close及库函数fopen,fwrite,fread,fclose的使用
- Open、Read、Write、Lseek、Close、Create、Delete、chdir系统调用