上截图:拷贝1个G文件 用时4秒
Fast-cp项目设计
1,记录当前系统时间
2,发起AIO读
3,等待AIO读结束后调用回调函数,发起AIO写
4,等待AIO写结束后调用回调函数,计算拷贝时间
5,拷贝完成,显示拷贝时间
文件操作
1文件的状态
2.文件属性
3.文件类型
4.文件权限
核心IO模块
辅助模块
<sys/types.h>
<sys/stat.h>
struct stat {
mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //inode节点号
dev_t st_dev; //设备号码
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件状态改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //伟建内容对应的块数量
};
部分函数
clock_gettime(CLOCK_MONOTONIC,struct timespec * time); //获取当前时间
实现代码
#include "stdio.h"
#include "errno.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <aio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
//#include <aiocb.h>
#define BUFF_SIZE 1024*1024
void aio_call_read(sigval_t sigval);
void aio_call_write(sigval_t sigval);
char * buf;
struct aiocb aiostr;
int filesize;
int offset;
int readfile,writefile = 0;
int sum,sumsur=0;
int wstat = 1;
int main(int argc,char *argv[])
{
struct stat filestat;
struct timespec tp;
offset = 0;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp);
printf("time:%lds%ld\n",tp.tv_sec,tp.tv_nsec);
if(stat(argv[1],&filestat)==-1)
{
perror("readfile");
exit(0);
}
filesize = filestat.st_size;
sumsur = filesize%(BUFF_SIZE);
sum = filesize/(BUFF_SIZE);
// printf("file size :%d---%d\n",sumsur,sum);
buf = (char *)malloc(BUFF_SIZE+1);
memset(buf,'\0',sizeof(buf));
if((readfile = open(argv[1],O_RDWR))==-1)
{
perror("readfile");
exit(0);
}
if((writefile = open(argv[2],O_RDWR|O_CREAT))==-1)
{
perror("readfile");
exit(0);
}
bzero(&aiostr,sizeof(struct aiocb));
aiostr.aio_fildes = readfile;
aiostr.aio_buf = buf;
aiostr.aio_nbytes = BUFF_SIZE;
aiostr.aio_lio_opcode = LIO_READ;
aiostr.aio_offset = offset;
aiostr.aio_sigevent.sigev_notify = SIGEV_THREAD;
aiostr.aio_sigevent.sigev_notify_function = aio_call_read;
aiostr.aio_sigevent.sigev_notify_attributes = NULL;
aiostr.aio_sigevent.sigev_value.sival_ptr = &aiostr;
aio_read(&aiostr);
while(wstat){
sleep(1);
}
free(buf);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp);
printf("time:%ld s %ld\n",tp.tv_sec,tp.tv_nsec);
return 0;
}
void aio_call_read(sigval_t sigval){
struct aiocb * aio = (struct aiocb *)(sigval.sival_ptr);
if(aio_error(aio)==0)
{
aio_return(aio);
aiostr.aio_fildes = writefile;
aiostr.aio_buf = buf;
//aiostr.aio_nbytes = BUFF_SIZE;
aiostr.aio_lio_opcode = LIO_WRITE;
aiostr.aio_offset = offset;
aiostr.aio_sigevent.sigev_notify = SIGEV_THREAD;
aiostr.aio_sigevent.sigev_notify_function = aio_call_write;
aiostr.aio_sigevent.sigev_notify_attributes = NULL;
aiostr.aio_sigevent.sigev_value.sival_ptr = &aiostr;
aio_write(&aiostr);
}
else{
wstat = 0;
}
}
void aio_call_write(sigval_t sigval){
struct aiocb * aio = (struct aiocb *)(sigval.sival_ptr);
if(aio_error(aio)==0)
{
aio_return(aio);
sum--;
if(sum>0)
{
aiostr.aio_nbytes = BUFF_SIZE;
}else if(sum==0)
{
aiostr.aio_nbytes =sumsur;
}else
{
close(readfile);
close(writefile);
wstat = 0;
return;
}
offset+=BUFF_SIZE;
aiostr.aio_fildes = readfile;
aiostr.aio_buf = buf;
// aiostr.aio_nbytes = BUFF_SIZE;
aiostr.aio_lio_opcode = LIO_READ;
aiostr.aio_offset = offset;
aiostr.aio_sigevent.sigev_notify = SIGEV_THREAD;
aiostr.aio_sigevent.sigev_notify_function = aio_call_read;
aiostr.aio_sigevent.sigev_notify_attributes = NULL;
aiostr.aio_sigevent.sigev_value.sival_ptr = &aiostr;
aio_read(&aiostr);
}
else{
wstat = 0;
}
}