📄 timewrite.c
字号:
#include "apue.h"
#include <sys/times.h>
#include <malloc.h>
#include <fcntl.h>
#include<string.h>
main(int argc,char *argv[])
{
off_t lenth;
char *buf;
int f_out,i,k,n;
long size;
struct tms start,end;
clock_t temp;
float utime,stime,ctime;
if(argc==2)
{
if((f_out=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,FILE_MODE))==-1)
{
printf("Open error\n");
exit(1);
}
}
else if(argc==3&&!strcmp(argv[2],"sync"))
{
if((f_out=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC|O_SYNC,FILE_MODE))==-1)
{
printf("Open error\n");
exit(1);
}
}
else
{
printf("Parameter input error!\n");
exit(1);
}
if((lenth=lseek(STDIN_FILENO,0,SEEK_END))==-1)
{
printf("Lseek error!\n");
}
if(lseek(STDIN_FILENO,0,SEEK_SET)==-1)
{
printf("Lseek error!\n");
}
if((buf=(char *)malloc(sizeof(char)*lenth))==NULL)
{
printf("Malloc error\n");
exit(1);
}
printf("\nThe length of file is %d\n",lenth);
if(read(STDIN_FILENO,buf,lenth)<0)
{
printf("Read error\n");
exit(1);
}
k=sysconf(_SC_CLK_TCK);
printf("BUFFSIZE\tUSER\t\tSYSTEM\t\tCLOCK\t\tLOOP\n");
for(size=1024;size<=131072;size*=2)
{
temp=times(&start);
n=lenth/size;
lseek(f_out,0,SEEK_SET);
for(i=0;i<n;i++)
write(f_out,buf,size);
if(lenth%size!=0)
write(f_out,buf,lenth%size);
ctime=(float)times(&end)-temp;
utime=(float)(end.tms_utime-start.tms_utime);
stime=(float)(end.tms_stime-start.tms_stime);
printf("%ld\t\t%.2f\t\t%.2f\t\t%.2f\t\t%d\n",size,utime/k,stime/k,ctime/k,n);
}
printf("\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -