码迷,mamicode.com
首页 > 系统相关 > 详细

linux--多进程进行文件拷贝

时间:2017-12-08 01:22:22      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:技术分享   des   进程拷贝   ring   str   函数   http   实现   程序   

学习IO的时候,我们都曾经利用文件IO函数,标准IO函数都实现了对文件的拷贝,

对某一个文件进行拷贝时,我们可以考虑一下几种方式:

a.单进程拷贝:

假设某一文件需要拷贝100字节,每一个时间片可以完成拷贝20个字节工作量,则需要被分配5个时间片才可以完成任务,但问题是这些个时间片并不是被连续分配的,我们并不知道

到经过多少时间片才会有下一个能分配给该进程的时间片,为了解决这个问题,我们有了第二种方法。

b.多进程拷贝(单核单CPU):

 通过切换进程,随着进程数的增加,当前程序获得时间片所需要的时间也就更少。

c.多进程拷贝(多核并发处理)

我们要实现的是第二个方法,代码如下:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<unistd.h>
  4 #include<fcntl.h>
  5 #include<string.h>
  6 #include<sys/types.h>
  7 #include<sys/stat.h>
  8 #include<sys/wait.h>
  9 int cutting(char *src,int prono)
 10 {
 11     int fd,filesize;
 12     if((fd=open(src,O_RDONLY))==-1)
 13     {
 14         perror("cutting open failed");
 15     return -1;
 16     }
 17     if((filesize=lseek(fd,0,SEEK_END))==-1)
 18     {
 19         perror("filesize failed");
 20         close(fd);
 21         return -1;
 22     }
 23     int blocksize;
 24     if(filesize%prono==0)
 25     {
 26         blocksize=filesize/prono;
 27     }
 28     else
 29     {
 30         blocksize=filesize/prono+1;
 31     }
 32     close(fd);
 33     //printf("%d",blocksize);
 34     return blocksize;
 35 
 36 }
 37 int copy(char *src,char *des,int pos,int blocksize)
 38 {
 39     if(access(src,F_OK)==-1)
 40     {
 41         perror("acess failed");
 42     }
 43     int fd1,fd2;
 44     char buf[blocksize];
 45     fd1=open(src,O_RDONLY);
 46     fd2=open(des,O_WRONLY|O_CREAT,0664);
 47     lseek(fd1,pos,SEEK_SET);
 48     lseek(fd2,pos,SEEK_SET);
 49 
 50 
 51     int len=read(fd1,buf,sizeof(buf));
 52     write(fd2,buf,len);
 53     close(fd1);
 54     close(fd2);
 55     return 1;
 56 }
 57 int create(char *src,char *des,int blocksize,int prono)
 58 {
 59     int i;
 60     pid_t pid;
 61     int pos=0;
 62     for(i=0;i<prono;i++)
 63     {
 64         pid=fork();
 65         if(pid>0)
 66         {
 67             pos+=blocksize;
 68 
 69             //printf("当前读取位置为:%d,每次所读文件大小:%d,当前进程为%d\n",pos,blocksize,getpid());
 70            
 71     }
 72 
 73         else if(pid==0)
 74         {
 75             copy(src,des,pos,blocksize);
 76             
 77             printf("当前读取位置为:%d,每次所读文件大小:%d,当前进程为%d\n",pos,blocksize,getpid());
 78             break;
 79         }
 80         
 81     }
 82     return 1;
 83 }
 84 int main(int argc,char **argv)
 85 {
 86     int prono;
 87     int blocksize;
 88     if(argc<3)
 89     {
 90         printf("the canshu you have chuan are too less\n");
 91     }
 92     if(argv[3]!=0)
 93     {
 94         prono=atoi(argv[3]);
 95         if(prono<=0||prono>=100)
 96         {
 97             printf("the num of the process you give cant not less than 0 or more than 100\n");
 98         }
 99         
100     }
101     else prono=5;
102     blocksize=cutting (argv[1],prono);
103     create(argv[1],argv[2],blocksize,prono);
104 
105     return 0;
106 }

技术分享图片

 

linux--多进程进行文件拷贝

标签:技术分享   des   进程拷贝   ring   str   函数   http   实现   程序   

原文地址:http://www.cnblogs.com/curo0119/p/8001509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!