Linux之多进程拷贝
编写思路
- 假设我们想让n个进程共同完成对一个文件的拷贝
- 那我们首先得求出让每个进程拷贝多少字节,一般是均分,也可以自己设定,这里我采用均分。怎么求呢?先求出文件大小,再除以进程个数,这里要考虑到不能整除的情况,那我们直接向上取整即可
- 在创建进程后,我让父进程只进行文件指针的移动,子进程进行文件的拷贝
【让我们开始编写代码吧】
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
int cutting(char *src, int n)
{
int fd = open(src, O_RDONLY);
int copySize;
if(fd < 0)
{
perror("open read file error\n");
return -1;
}
int len = lseek(fd, 0, SEEK_END); //获取文件字节数
if(len % n == 0)
{
copySize = len / n;
}
else
copySize = (len / n) + 1;
return copySize;
}
void copy(char *src, char *dest, int pos, int copySize)
{
int rfd = open(src, O_RDONLY);
int wfd = open(dest, O_WRONLY | O_CREAT, 0666);
char buf[copySize];
if(rfd < 0 || wfd < 0)
{
perror("open file error\n");
return;
}
lseek(rfd, pos, SEEK_SET);
lseek(wfd, pos, SEEK_SET);
int len = read(rfd, buf, sizeof(buf));
write(wfd, buf, len);
}
void create(char *src, char *dest, int copySize, int n)
{
pid_t pid;
int pos = 0; //记录每次文件拷贝的位置
for(int i = 0; i < n; i++) //父进程进行文件指针的移动,子进程进行拷贝
{
pid = fork();
if(pid > 0) //父进程
{
pos += copySize;
}
else if(pid == 0) //子进程
{
copy(src, dest, pos, copySize);
printf("当前读取位置为:%d,每次所读文件大小:%d,当前进程为%d\n",pos,copySize,getpid());
break;
}
}
}
int main(int argc, char *argv[])
{
int n = atoi(argv[3]); //创建的进程个数
//获取每个进程将要copy的字节数
int copySize = cutting(argv[1], n);
//创建进程进行拷贝
create(argv[1], argv[2], copySize, n);
return 0;
}
【运行结果】
2.通过建立映射区来实现文件的拷贝
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mman.h>
//进程数
int n;
int len;
//文件拷贝
void copy(char *mem, char *dest, int pos, int blockSize)
{
int wfd = open(dest, O_RDWR|O_CREAT, 0644);
if(wfd < 0)
{
perror("open file error\n");
exit(1);
}
lseek(wfd, pos, SEEK_SET);
write(wfd, mem + pos, blockSize);
close(wfd);
}
//创建进程
void create(char *mem, char *dest, int blockSize)
{
pid_t pid;
int pos = 0;
for(int i = 0; i < n; i++)
{
pid = fork();
if(pid > 0) //父进程移动位置
{
wait(NULL);
pos += blockSize;
}
else
{
if(i == n - 1)
{
printf("pos = %d, blockSize = %d, id = %u\n", pos, blockSize + (len % n), getpid());
copy(mem, dest, pos, blockSize + (len % n));
break;
}
else
{
printf("pos = %d, blockSize = %d, id = %u\n", pos, blockSize, getpid());
copy(mem, dest, pos, blockSize);
break;
}
}
}
}
//./copy src dest n
int main(int argc, char *argv[])
{
n = atoi(argv[3]);
char *mem;
//打开文件,建立映射区
int rfd = open(argv[1], O_RDWR);
int blockSize;
if(rfd < 0)
{
perror("open error\n");
exit(1);
}
len = lseek(rfd, 0, SEEK_END);
blockSize = len / n;
mem = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, rfd, 0);
if(mem == MAP_FAILED)
{
perror("mmap error\n");
exit(1);
}
close(rfd);
//创建进程
create(mem, argv[2], blockSize);
munmap(mem, len);
return 0;
}
多线程拷贝
上面写过了多进程拷贝,那么多线程拷贝就很简单啦
【改变】这里我用了一个结构体来存储一些相关信息,为的就是在创建线程时把这些值传到线程的执行函数中。其他的与进程没有什么区别
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int pos = 0;
typedef struct{
char src[50]; //源文件
char dest[50]; //目的文件
int copySize; //每个线程要处理的字节数
int n; //要创建的线程数
}File;
int cutting(char *src, int n)
{
int fd = open(src, O_RDONLY);
int copySize;
if(fd < 0)
{
perror("open read file error\n");
return -1;
}
int len = lseek(fd, 0, SEEK_END); //获取文件字节数
if(len % n == 0)
{
copySize = len / n;
}
else
copySize = (len / n) + 1;
return copySize;
}
void copy(char *src, char *dest, int pos, int copySize)
{
int rfd = open(src, O_RDONLY);
int wfd = open(dest, O_WRONLY | O_CREAT, 0666);
char buf[copySize];
if(rfd < 0 || wfd < 0)
{
perror("open file error\n");
return;
}
lseek(rfd, pos, SEEK_SET);
lseek(wfd, pos, SEEK_SET);
int len = read(rfd, buf, sizeof(buf));
write(wfd, buf, len);
}
void *copyFile(void *arg)
{
File *file = (File *)arg;
printf("cpoySize: %d, n: %d, src: %s, dest: %s\n", file->copySize, file->n, file->src, file->dest);
copy(file->src, file->dest, pos, file->copySize);
printf("当前读取位置为:%d,每次所读文件大小:%d,当前线程为%l\n",pos,file->copySize,pthread_self());
pos += file->copySize;
//pthread_exit(NULL);
}
void create(File *file)
{
//printf("cpoySize: %d, n: %d, src: %s, dest: %s\n", file->copySize, file->n, file->src, file->dest);
int n = file->n;
pthread_t tid;
for(int i = 0; i < n; i++)
{
int ret = pthread_create(&tid, NULL, copyFile, (void *)file);
pthread_join(tid, NULL);
}
}
int main(int argc, char *argv[])
{
File *file = (File *)malloc(sizeof(File));
int num = atoi(argv[3]); //创建的进程个数
//获取每个进程将要copy的字节数
int copySize = cutting(argv[1], num);
strcpy(file->src, argv[1]);
strcpy(file->dest, argv[2]);
file->copySize = copySize;
file->n = num;
//printf("cpoySize: %d, n: %d, pos: %d\n", file->copySize, file->n, pos);
//创建进行拷贝
create(file);
free(file);
return 0;
}
【执行结果】
使用映射区实现多线程拷贝
如果文件大小不能整除线程数,那么最后一个线程要多拷贝 (文件大小 / 线程数) 的余数
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <pthread.h>
#include <string.h>
typedef struct
{
char *mem;
char dest[20]; //目的文件名
int pos;
int blockSize;
}File;
//进程数
int n;
int len;
//文件拷贝
void *copy(void *arg)
{
File *file = (File *)arg;
printf("当前读取位置为:%d,每次所读文件大小:%d\n",file->pos,file->blockSize);
int wfd = open(file->dest, O_RDWR|O_CREAT, 0644);
if(wfd < 0)
{
perror("open file error\n");
exit(1);
}
lseek(wfd, file->pos, SEEK_SET);
write(wfd, file->mem + file->pos, file->blockSize);
close(wfd);
file->pos += file->blockSize;
}
//创建进程
void create(File *file)
{
pthread_t tid;
file->pos = 0;
for(int i = 0; i < n; i++)
{
if(i == n - 1)
{
file->blockSize += len % n;
pthread_create(&tid, NULL, copy, (void *)file);
}
else
{
pthread_create(&tid, NULL, copy, (void *)file);
}
pthread_join(tid, NULL);
}
}
//./copy src dest n
int main(int argc, char *argv[])
{
n = atoi(argv[3]);
File *file = (File *)malloc(sizeof(File)); //创建结构体
strcpy(file->dest, argv[2]);
//打开文件,建立映射区
int rfd = open(argv[1], O_RDWR);
if(rfd < 0)
{
perror("open error\n");
exit(1);
}
len = lseek(rfd, 0, SEEK_END);
file->blockSize = len / n;
file->mem = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, rfd, 0);
if(file->mem == MAP_FAILED)
{
perror("mmap error\n");
exit(1);
}
close(rfd);
//创建线程
create(file);
munmap(file->mem, len);
return 0;
}