Linux操作系统:多进程拷贝

多个进程读同一文件,拷贝到目标文件。(是为了提高效率)

子进程 copy 文件进程:

参数:源文件、目标文件、文件起始位置、文件块大小

1.打开源文件----read

2.打开目标文件---write

3.移动源文件指针到目标位置

4.移动目标文件指针到目标位置

5.从源文件读目标大小

6.从目标文件写入目标大小的内容

7.关闭源文件,目标文件

父进程:

参数:源文件、目标文件、进程数

1.校验参数(源文件是否存在)

2.将文件切片(获取文件大小/进程数)

3.创建子进程(子进程复制拷贝文件)

4.回收子进程(僵尸进程)

系统io和文件io的区别

open、read和fopen、fread:

标准文件io:自带缓冲区。需要从自己的缓冲区拷贝到内核,在从内核拷贝到磁盘,实时性不好,但是可以跨系统

系统io:直接从内核缓冲区拷贝到内核中,实时性好,只属于linux系统

子进程:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <sys/types.h>
  5 #include <sys/stat.h>
  6 #include <fcntl.h>
  7 #include <sys/wait.h>
  8 
  9 int arg_check(const char* srcfile, int pronumber)
 10 {
 11         if(access(srcfile,F_OK) == -1)
 12         {
 13                 perror("源文件不存在");
 14                 exit(0);
 15         }
 16         if(pronumber <= 0 || pronumber > 100)
 17         {
 18                 perror("进程的数量请在[0-100]范围内");
 19                 exit(0);
 20         }
 21         return 0;
 22 }
 23 int block_cur(const char* srcfile,int pronumber)
 24 {
 25         //获取文件大小
 26         int srcfd = open(srcfile,O_RDONLY);
 27         int filesize = lseek(srcfd,0,SEEK_END);
 28         if(filesize % pronumber == 0)
 29                 return filesize/pronumber;
 30         else
 31                 return filesize/pronumber + 1;
 32 }
33 int process_create(const char* srcfile, const char* desfile,int pronumber, int blocksize)
 34 {
 35         pid_t pid;
 36         int flags;
 37         for(flags = 0; flags < pronumber; flags++)
 38         {
 39                 pid = fork();
 40                 if(pid == 0)
 41                         break;
 42         }
 43         if(pid > 0)
 44         {
 45                 printf("process_create success...\n");
 46         }
 47         else if(pid == 0)
 48         {
 49                 int npos = flags * blocksize;
 50                 char szblocksize[10];
 51                 char szpos[10];
 52                 sprintf(szpos,"%d",npos);
 53                 sprintf(szblocksize,"%d",blocksize);
 54                 printf("child process id[%d],file start pos [%d],blocksize[%d]\n",getpid(),npos,blocksize);
 55                 execl("/home/buka/0365file/processcopy/copy","copy",srcfile,desfile,szpos,szblocksize,NULL);
 56         }
 57         return 0;
 58 }
 59 
 60 int main(int argc, char** argv)
 61 {
 62         if(argc < 3)
 63         {
 64                 perror("参数不足,请重新填写");
64                 perror("参数不足,请重新填写");
 65                 exit(0);
 66         }
 67         int pronumber;
 68         if(argv[3] == 0)
 69                 pronumber = 5;
 70         else
 71                 pronumber = atoi(argv[3]);
 72         //1.校验参数(源文件、目标文件、进程数)
 73         arg_check(argv[1],pronumber);
 74         //if(pronumber == 0)
 75         //      pronumber = 5;
 76         //2.数据切片(将文件切为指定大小)
 77         int nblocksize = block_cur(argv[1],pronumber);
 78         //3.子进程的创建与拷贝
 79         process_create(argv[1],argv[2],pronumber,nblocksize);
 80         //4.回收子进程(僵尸进程)
 81         pid_t zid;
 82         while((zid = waitpid(-1,0,WNOHANG)) != -1)
 83         {
 84                 if(zid > 0)
 85                 {
 86                         printf("zombie process id [%d]\n",zid);
 87                 }
 88         }
 89 
 90 
 91 
 92         return 0;
 93 }

父进程:

1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <sys/types.h>
  5 #include <sys/stat.h>
  6 #include <fcntl.h>
  7 
  8 int main(int arg, char** argv)
  9 {
 10         //1.校验参数 源文件、目标文件、起始位置、文件块大小
 11         if(arg < 4){
 12                 perror("参数不足");
 13                 exit(0);
 14         }
 15         //2.打开源文件--read
 16         int srcfd = open(argv[1],O_RDONLY);
 17         if(srcfd == -1)
 18         {
 19                 perror("open read file failed");
 20                 exit(0);
 21         }
 22         //3.打开目标文件--write
 23         int desfd = open(argv[2],O_WRONLY|O_CREAT,0664);
 24         if(desfd == -1)
 25         {
 26                 perror("open write file failed");
 27                 exit(0);
 28         }
 29         //4.移动源文件指针
 30         int npos = atoi(argv[3]);
 31         lseek(srcfd,npos,SEEK_SET);
 32         //5.移动目标文件指针
 33         lseek(desfd,npos,SEEK_SET);
 34         //6.读源文件内容
 35         int nbuffersize = atoi(argv[4]);
 36         char szbuf[nbuffersize];
 37         int readlen = read(srcfd,szbuf,nbuffersize);
 38         if(readlen == -1)
 39         {
 40                 perror("read file erro");
 41                 exit(0);
 42         }
 43         //7.项目表文件写入文件内容
 44         write(desfd,szbuf,readlen);
 45         //8.关闭源文件、目标文件
 46         close(srcfd);
 47         close(desfd);
 48 
 49 
 50         return 0;
 51 }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值