多线程实现多个文件同时拷贝

实现:我这里是实现的是和linux终端中的cp指令类似的功能,将多个文件同时拷贝到指定的目录中。也就是说利用main函数传参,在执行程序的命令后输入拷贝的文件和目标目录。可以看最后的运行效果图。

代码实现:

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
/*目标缓冲区要足够大,不然sprintf有警告*/
char buff[2][2048];

typedef struct filepath{
	char src[1024];
	char dest[1024];
}PATH;

pthread_t pth1,pth2;
/*多线程公用函数*/
void *pthread_task(void *argv){
	PATH *p = (PATH*)argv;
	int fd1 = open(p->src,O_RDONLY);
	if(fd1 < 0){
		perror("open");
		pthread_exit(NULL);
	}
	int fd2 = open(p->dest,O_CREAT|O_WRONLY|O_TRUNC,0777);
	if(fd2 < 0){
		perror("open");
		pthread_exit(NULL);
	}
	char c;
	int i = 0;
	struct stat s;
	stat(p->src,&s);
	while(read(fd1,&c,sizeof(char))){
		if(write(fd2,&c,sizeof(char)) > 0){
			if(pthread_self() == pth1){
				sprintf(buff[0],"%s拷贝中[%.1lf%%]",p->src,(double)i++/s.st_size*100);
			}
			else if(pthread_self() == pth2){
				sprintf(buff[1],"%s拷贝中[%.1lf%%]",p->src,(double)i++/s.st_size*100);
			}
			printf("%s,%s\r",buff[0],buff[1]);
			continue;
		}
		if(pthread_self() == pth1){
			sprintf(buff[0],"%s拷贝失败",p->src);
		}
		else if(pthread_self() == pth2){
			sprintf(buff[1],"%s拷贝失败",p->src);
		}
		printf("%s,%s\r",buff[0],buff[1]);
		close(fd1);
		close(fd2);
		pthread_exit(NULL);
	}

	if(pthread_self() == pth1){
		sprintf(buff[0],"%s拷贝完成[%.1lf%%]",p->src,(double)i++/s.st_size*100);
	}
	else if(pthread_self() == pth2){
		sprintf(buff[1],"%s拷贝完成[%.1lf%%]",p->src,(double)i++/s.st_size*100);
	}
	printf("%s,%s\r",buff[0],buff[1]);

	close(fd1);
	close(fd2);
	pthread_exit(NULL);
}

int main(int argc,char *argv[]){
	PATH p1;
	strcpy(p1.src,argv[1]);
	strcpy(p1.dest,argv[3]);
	char *find = strrchr(p1.src,'/');
	strcat(p1.dest,find);
	if(0 != pthread_create(&pth1,NULL,pthread_task,&p1)){
		perror("pthread_create");
		return -1;
	}
	PATH p2;
	strcpy(p2.src,argv[2]);
	strcpy(p2.dest,argv[3]);
	find = strrchr(p2.src,'/');
	strcat(p2.dest,find);
	if(0 != pthread_create(&pth2,NULL,pthread_task,&p2)){
		perror("pthread_create");
		return -1;
	}
	pthread_join(pth1,NULL);
	pthread_join(pth2,NULL);
	putchar('\n');
	return 0;
}

运行效果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多线程文件拷贝是通过利用多个线程同时进行文件读取和写入操作来提高拷贝效率的一种方式。下面是一个简单的实现示例: 1. 首先,确定要拷贝的源文件和目标文件路径。 2. 创建一个用于存储线程的列表。 3. 计算源文件的大小,并确定每个线程需要拷贝的字节数。 4. 创建指定数量的线程,每个线程负责一部分数据的拷贝。 5. 在每个线程中,打开源文件和目标文件,并将读取位置和写入位置设置为对应的偏移量。 6. 在每个线程中,使用循环读取源文件的数据块,并将数据块写入目标文件。 7. 等待所有线程完成拷贝操作。 8. 关闭源文件和目标文件。 下面是一个简化的示例代码,假设要拷贝文件大小为 fileSize,线程数为 threadNum: ```python import threading def copy_data(source_file, target_file, start, end): with open(source_file, 'rb') as sf: with open(target_file, 'rb+') as tf: sf.seek(start) tf.seek(start) data = sf.read(end - start) tf.write(data) source_file = 'path/to/source/file' target_file = 'path/to/target/file' fileSize = os.path.getsize(source_file) threadNum = 4 threads = [] for i in range(threadNum): start = int(i * fileSize / threadNum) end = int((i + 1) * fileSize / threadNum) t = threading.Thread(target=copy_data, args=(source_file, target_file, start, end)) threads.append(t) t.start() for t in threads: t.join() print("文件拷贝完成!") ``` 在上述示例中,我们通过创建多个线程,每个线程负责拷贝文件的一部分数据到目标文件中,从而实现多线程文件拷贝。请注意,该示例仅为演示多线程文件拷贝的概念,实际应用中可能还需要处理异常、进度显示等其他功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值