不是哥们,脑子都烧起来了
1、使用两个线程完成两个文件的拷贝,分支线程1拷贝前一半,分支线程2拷贝后一半,主线程回收两个分支线程的资源
思路:
 1、首先创建了求文件长度的函数,在创建函数的时候,顺便将目标文件创建完成,返回源文件的大小
2、定义了一个拷贝函数,首先移动光标位置,接下来就是正常的读写操作
3、定义了线程函数,执行了文件拷贝功能
4、主函数内先求出了源文件的大小,然后用结构体封装了需要传入线程里面的数据,通过结构体数组,分别传入拷贝文件前一半的参数与后一半的参数,分别存在buf[0]和buf[1]中。
#include <myhead.h>
struct Buf
{
	const char *srcfile;
	const char *destfile;
	int start;
	int len;
};
//定义求文件长度函数
int get_file_len(const char *srcfile,const char *destfile)
{
	int sfd = -1,dfd = -1;
	//以只读的形式打开源文件
	sfd = open(srcfile,O_RDONLY);
	if(sfd == -1)
	{
		perror("open srcfile error");
		return -1;
	}
	//以创建的形式打开目标文件
	dfd = open(destfile,O_WRONLY|O_CREAT|O_TRUNC,0664);
	if(dfd == -1)	
	{
		perror("open destfile error");
		return -1;
	}
	int len = lseek(sfd,0,SEEK_END);
	//关闭文件
	close(sfd);
	close(dfd);
	
	//返回文件大小
	return len;
}
//定义拷贝文件函数
void copy_file(const char *srcfile,const char *destfile,int start,int len)
{
	int sfd = -1,dfd = -1;
	//以只读的形式打开源文件
	sfd = open(srcfile,O_RDONLY);
	if(sfd == -1)
	{
		perror("open srcfile error");
		return ;
	}
	//以只写的形式打开目标文件
	dfd = open(destfile,O_WRONLY);
	if(dfd == -1)
	{
		perror("open destfile error");
		return ;
	}
	//移动光标位置
	lseek(sfd,start,SEEK_SET);
	lseek(dfd,start,SEEK_SET);
	char buf[128] = "";       //定义容器
	int sum = 0;
	while(1)
	{
		int res = read(sfd,buf,sizeof(buf));
		sum += res;
		if(res == 0 || sum > len)   //文件读取结束
		{
			write(dfd,buf,res-(sum-len));   //最后一次拷贝
			break;
		}
		write(dfd,buf,res);
	}
	close(sfd);
	close(dfd);
}
//定义线程体函数
void *task(void *arg)
{
	const char *srcfile = ((struct Buf*)arg)->srcfile;
	const char *destfile = ((struct Buf*)arg)->destfile;
	int start = ((struct Buf*)arg)->start;
	int len = ((struct Buf*)arg)->len;
	//文件拷贝
	copy_file(srcfile,destfile,start,len);
    
    //结束线程
    pthread_exit(NULL);
}
int main(int argc, 	const char *argv[])
{
	//判断输入是否合法
	if(argc != 3)
	{
		printf("input error\n");
		return -1;
	}
	//定义线程变量
	pthread_t tid1,tid2;
	
	//求出源文件大小
	int len = get_file_len(argv[1],argv[2]);
	printf("%d\n",len);
	//封装要传递的数据
	struct Buf buf[2];
	buf[0].srcfile = argv[1];
	buf[0].destfile = argv[2];
	buf[0].start = 0;
	buf[0].len = len/2;	
	buf[1].srcfile = argv[1];
	buf[1].destfile = argv[2];
	buf[1].start = len/2;
	buf[1].len = len-len/2;	
	
	//创建两个线程
	if(pthread_create(&tid1,NULL,task,&buf[0]) != 0)
	{
		printf("创建失败\n");
		return -1;
	}
	if(pthread_create(&tid2,NULL,task,&buf[1]) != 0)
	{
		printf("创建失败\n");
		return -1;
	}
	//阻塞等待线程的结束
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	return 0;
}
 
实现结果如下:可以看到已经成功拷贝了
![]()

2、思维导图




















