标签:
<pre name="code" class="cpp">#include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> int copy_file_init(const char *src_file,const char *dest_file) { int fd_src; int fd_dest; int src_file_len; fd_src = open(src_file,O_RDONLY); if(fd_src < 0){ fprintf(stderr,"Fail to open %s : %s\n",src_file,strerror(errno)); exit(EXIT_FAILURE); } src_file_len = lseek(fd_src,0,SEEK_END); fd_dest = open(dest_file,O_WRONLY | O_TRUNC | O_CREAT,0666); if(fd_dest < 0){ fprintf(stderr,"Fail to open %s : %s\n",dest_file,strerror(errno)); exit(EXIT_FAILURE); } close(fd_src); close(fd_dest); return src_file_len; } int copy_file(const char *src_file,const char *dest_file,int postion,int len) { int n; int fd_src; int fd_dest; int count = 0; char buf[1024]; fd_src = open(src_file,O_RDONLY); if(fd_src < 0){ fprintf(stderr,"Fail to open %s : %s\n",src_file,strerror(errno)); exit(EXIT_FAILURE); } fd_dest = open(dest_file,O_WRONLY); if(fd_dest < 0){ fprintf(stderr,"Fail to open %s : %s\n",dest_file,strerror(errno)); exit(EXIT_FAILURE); } lseek(fd_src,postion,SEEK_SET); lseek(fd_dest,postion,SEEK_SET); while(1){ n = read(fd_src,buf,sizeof(buf)); if(n <= 0){ break; } n = write(fd_dest,buf,n); if(n <= 0){ break; } count += n; if(count >= len){ break; } } close(fd_src); close(fd_dest); return count; } void process_copy_file(const char *src_file,const char *dest_file,int src_file_len,int precess_num) { int n; int m=0; pid_t pid; pid = fork(); if(pid < 0){ perror("Fail to fork"); exit(EXIT_FAILURE); } if(pid == 0){ n = copy_file(src_file,dest_file,src_file_len/2,(src_file_len - src_file_len / 2)); printf("Child process copy %d bytes!\n",n); } if(pid > 0){ n = copy_file(src_file,dest_file,0,src_file_len / 2); printf("Parent process copy %d bytes!\n",n); } while(precess_num > 0){ pid = fork(); if(pid < 0){ perror("Fail to fork"); exit(EXIT_FAILURE); } else{ n--; } if } return ; } //./a.out src_file dest_file int main(int argc, const char *argv[]) { int src_file_len; if(argc < 3){ fprintf(stderr,"Usage : %s <src file> <dest file>!\n",argv[0]); exit(EXIT_FAILURE); } src_file_len = copy_file_init(argv[1],argv[2]); process_copy_file(argv[1],argv[2],src_file_len); exit(EXIT_SUCCESS); }
#include <stdio.h> #include <pthread.h> #include <string.h> #include <stdlib.h> #include <errno.h> //pthread_mutex_t lock; struct args{ char src_file[128]; char des_file[128]; int position; int len; }; int get_filelen(const char *src_file) { int len; FILE *fp; fp = fopen(src_file,"r"); fseek(fp,0,SEEK_END); len = ftell(fp); rewind(fp); return len; } void init_args(struct args *args ,const char *src_file,const char *des_file,int pos,int len) { strcpy(args->src_file, src_file); strcpy(args->des_file, des_file); args->position = pos; args->len = len; } void *copy_file(void *arg) { // pthread_mutex_lock(&lock); int n = 0; int ret; int count = 0; int read_len = 0; char buf[4096]; FILE *fp1,*fp2; struct args *p = (struct args*)arg; fp1 = fopen(p->src_file,"r"); if(fp1 == NULL){ fprintf(stderr,"fopen file %s fail:%s\n",p->src_file,strerror(errno)); exit(EXIT_FAILURE); } fp2 = fopen(p->des_file,"a"); if(fp2 == NULL){ fprintf(stderr,"fopen file %s fail:%s\n",p->des_file,strerror(errno)); exit(EXIT_FAILURE); } fseek(fp1,p->position,SEEK_SET); fseek(fp2,p->position,SEEK_SET); printf("p->position=%d,p->len=%d\n",p->position,p->len); read_len = p->len > 4096?4096:p->len; while(1){ if(count >= p->len){ break; } printf("read_len=%d\n",read_len); n = fread(buf,sizeof(buf[0]),read_len,fp1); if(n <= 0){ printf("fread over: %d\n",n); break; } printf("n=%d\n",n); ret = fwrite(buf,sizeof(buf[0]),n,fp2); if(ret <= 0){ printf("fwrite over\n"); break; } count += n; } printf("write %d bytes to des file\n",count); fclose(fp1); fclose(fp2); // pthread_mutex_unlock(&lock); } int main(int argc, const char *argv[]) { int ret; int len; int position; struct args args1,args2; pthread_t tid1,tid2; //pthread_mutex_init(&lock,NULL); if(argc < 3){ fprintf(stderr,"Usage: %s <filename> <filename>\n",argv[0]); exit(EXIT_FAILURE); } len = get_filelen(argv[1]); position = len / 2; init_args(&args1,argv[1],argv[2],0,position); init_args(&args2,argv[1],argv[2],position,len-position); fprintf(stdout,"src file length=%d\n",len); ret = pthread_create(&tid1,NULL,copy_file,&args1); if(ret != 0){ fprintf(stderr,"create pthread tid1 fail: %s\n",strerror(errno)); exit(EXIT_FAILURE); } ret = pthread_create(&tid2,NULL,copy_file,&args2); if(ret != 0){ fprintf(stderr,"create pthread tid2 fail: %s\n",strerror(errno)); exit(EXIT_FAILURE); } pthread_join(tid1,NULL); pthread_join(tid2,NULL); }
标签:
原文地址:http://blog.csdn.net/liubo2011/article/details/51362054