两个线程拷贝文件
#include <myhead.h>
/*****************定义求文件长度函数********************/
int getlen(const char *srcfile, const char *dstfile)
{
//定义两个文件描述符
int fd1, fd2;
//以只读的形式打开源文件
if((fd1=open(srcfile, O_RDONLY)) == -1)
{
perror("srcfile open error");
return -1;
}
//以只写的形式打开目标文件
if((fd2 = open(dstfile, O_WRONLY|O_CREAT|O_TRUNC, 0664)) == -1)
{
perror("dstfile open error");
return -1;
}
//求源文件的长度
int len = lseek(fd1, 0, SEEK_END);
//关闭文件
close(fd1);
close(fd2);
return len;
}
/**********************定义文件拷贝函数***********************************/
void copy_file(const char *srcfile, const char *dstfile, int start, int len)
{
//定义两个文件描述符
int fd1, fd2;
//以只读的形式打开源文件
if((fd1 = open(srcfile, O_RDONLY)) == -1)
{
perror("srcfile open error");
return ;
}
//以只写的形式打开目标文件
if((fd2 = open(dstfile, O_RDWR|O_CREAT|O_APPEND)) == -1)
{
perror("dstfile open error");
return ;
}
//将光标定位在开始拷贝的位置
lseek(fd1, start, SEEK_SET);
lseek(fd2, start, SEEK_SET);
//开始进行拷贝工作
char buf[128] = ""; //数据搬运工
int sum = 0; //记录搬运的总个数
while(1)
{
//从源文件中读取数据
int ret = read(fd1, buf, sizeof(buf));
sum += ret; //记录每次搬运的个数
if(ret==0 || sum>len)
{
write(fd2, buf, ret-(sum-len)); //将最后一次不满128的数据拷贝到新文件中
break;
}
//将数据写入目标文件
write(fd2, buf, ret);
}
printf("拷贝成功\n");
}
/**********************定义线程体函数**************************/
//定义线程体函数
void *task1(void *arg)
{
int len = * (int *)arg;
copy_file("./work12.c","./testwork.c",0,len/2);
pthread_exit(NULL);
}
void *task2(void *arg)
{
sleep(1);
int len =* (int *)arg;
copy_file("./work12.c","./testwork.c",len/2,len-len/2);
pthread_exit(NULL);
}
/**********************主函数********************************/
int main(int argc, const char *argv[])
{
/* if(argc != 3)
{
printf("input file error\n");
printf("usage:./a.out srcfile dstfile\n");
return -1;
}*/
//求文件的长度
int len = getlen("./work12.c","./testwork.c");
//定义线程号
pthread_t tid1;
pthread_t tid2;
//创建线程1
if(pthread_create(&tid1,NULL,task1,&len))
{
printf("pthread_create error");
return -1;
}
//创建线程2
if(pthread_create(&tid2,NULL,task2,&len))
{
printf("pthread_create error");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
使用三个线程拷贝文件
#include <myhead.h>
/*****************定义求文件长度函数********************/
int getlen(const char *srcfile, const char *dstfile)
{
//定义两个文件描述符
int fd1, fd2;
//以只读的形式打开源文件
if((fd1=open(srcfile, O_RDONLY)) == -1)
{
perror("srcfile open error");
return -1;
}
//以只写的形式打开目标文件
if((fd2 = open(dstfile, O_WRONLY|O_CREAT|O_TRUNC, 0664)) == -1)
{
perror("dstfile open error");
return -1;
}
//求源文件的长度
int len = lseek(fd1, 0, SEEK_END);
//关闭文件
close(fd1);
close(fd2);
return len;
}
/**********************定义文件拷贝函数***********************************/
void copy_file(const char *srcfile, const char *dstfile, int start, int len)
{
//定义两个文件描述符
int fd1, fd2;
//以只读的形式打开源文件
if((fd1 = open(srcfile, O_RDONLY)) == -1)
{
perror("srcfile open error");
return ;
}
//以只写的形式打开目标文件
if((fd2 = open(dstfile, O_RDWR|O_CREAT|O_APPEND)) == -1)
{
perror("dstfile open error");
return ;
}
//将光标定位在开始拷贝的位置
lseek(fd1, start, SEEK_SET);
lseek(fd2, start, SEEK_SET);
//开始进行拷贝工作
char buf[128] = ""; //数据搬运工
int sum = 0; //记录搬运的总个数
while(1)
{
//从源文件中读取数据
int ret = read(fd1, buf, sizeof(buf));
sum += ret; //记录每次搬运的个数
if(ret==0 || sum>len)
{
write(fd2, buf, ret-(sum-len)); //将最后一次不满128的数据拷贝到新文件中
break;
}
//将数据写入目标文件
write(fd2, buf, ret);
}
printf("拷贝成功\n");
}
/**********************定义线程体函数**************************/
//定义线程体函数
void *task1(void *arg)
{
int len = * (int *)arg;
copy_file("./work12.c","./testwork.c",0,len/3);
pthread_exit(NULL);
}
void *task2(void *arg)
{
sleep(1);
int len =* (int *)arg;
copy_file("./work12.c","./testwork.c",len/3,len/3*2-len/3);
pthread_exit(NULL);
}
void *task3(void *arg)
{
sleep(2);
int len =* (int *)arg;
copy_file("./work12.c","./testwork.c",len/3*2,len-len/3*2);
pthread_exit(NULL);
}
/**********************主函数********************************/
int main(int argc, const char *argv[])
{
//求文件的长度
int len = getlen("./work12.c","./testwork.c");
//定义线程号
pthread_t tid1;
pthread_t tid2;
pthread_t tid3;
//创建线程1
if(pthread_create(&tid1,NULL,task1,&len))
{
printf("pthread_create error");
return -1;
}
//创建线程2
if(pthread_create(&tid2,NULL,task2,&len))
{
printf("pthread_create error");
return -1;
}
//创建线程3
if(pthread_create(&tid3,NULL,task3,&len))
{
printf("pthread_create error");
return -1;
}
sleep(3);
pthread_detach(tid1);
pthread_detach(tid2);
pthread_detach(tid3);
return 0;
}