define _BACKFILE

include <sys/types.h>

include <stdlib.h>

include <string.h>

include <fcntl.h>

include <stdio.h>

include <unistd.h>

include <sys/stat.h>

include <pthread.h>

include <errno.h>

define BUFFERSIZE 1024

define MAXBACKUPFILE 50

define MAXNAMESIZE 80

void backup_file(void arg);

/***************************************************************/
int main(int argc,char *argv[]){

pthread_t thread_id[MAXBACKUPFILE];/*all thread_id*/
int filenum;
char filename[MAXNAMESIZE];
int fd[MAXBACKUPFILE][2];
int *bytes_copied_p;
int total_bytes_copied=0;

filenum=argc-1;
if(filenum>MAXBACKUPFILE){
    fprintf(stderr, "USUAGE:%s arg1 arg2 ... arg%d\n", *argv,MAXBACKUPFILE);exit(1);
}

//copy every file
//printf("begin to copy file\n\n");
int i;
for(i=0;i<filenum;i++){
    printf("begin to open src file %d\n",i);
    if( (fd[i][0]=open(argv[i+1],O_RDONLY) ) < 0 ){
        fprintf(stderr, "Unable to open source file %s: %s\n", argv[i+1],strerror(errno) );continue;
    }
    printf("begin to open des file %d\n",i);
    sprintf(filename, "%s.bak", argv[i+1]);/*des file neme*/

    if( (fd[i][1]=open(filename,O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR) ) < 0 ){
        fprintf(stderr, "Unable to create dest file %s: %s\n", filename,strerror(errno) );continue;
    }
    printf("begin to copy file %d\n\n",i);
    if( pthread_create(&thread_id[i],NULL,backup_file,(void *)fd[i] ) !=0 ){
        fprintf( stderr,"Could not create thread %d: %s\n",i,strerror(errno) );
    }
}

//wait thread to stop
if( ( bytes_copied_p = ( malloc( sizeof(int) ) ) )==NULL ) exit(1);
*bytes_copied_p=0;
for(i=0;i<filenum;i++){
    printf("wait Thread %d to stop\n",i);
    if(pthread_join(thread_id[i], (void **)&(bytes_copied_p) ) != 0){
        fprintf( stderr,"No thread %d to join: %s\n",i,strerror(errno) );
    }
    else{
        printf( "Thread %d backuped %d bytes for %s\n",i ,*bytes_copied_p,argv[i+1] );
        total_bytes_copied += *bytes_copied_p;
        free(bytes_copied_p);
    }

}

printf("Total bytes copied = %d\n",total_bytes_copied);
return 0;

}
/***************************************************************/

void backup_file(void arg){
int src_file,des_file;/srcfile and destfile/

int bytes_read=0;
int bytes_written=0;

int *bytes_copied_p;/*all types had copied*/

char buffer[BUFFERSIZE];
char *bufp;/*continue write by move pointer*/

src_file=*((int *)arg);
des_file=*((int *)arg+1);

if( ( bytes_copied_p = ( malloc( sizeof(int) ) ) )==NULL ) pthread_exit(NULL);
*bytes_copied_p=0;

while(1){
    bytes_read=read(src_file,buffer,BUFFERSIZE);
    
    if( bytes_read==0 || ( (bytes_read<0) && (errno != EINTR) ) ) break;/*read over or other*/
    else if( bytes_read<0 && errno==EINTR ) continue; /*stopped by signal*/

    bufp=buffer;
    while(bytes_read>0){
        bytes_written=write(des_file, bufp,bytes_read);
        if(bytes_written<0 && errno!=EINTR) break;
        else if(bytes_written<0) continue;
        
        *bytes_copied_p+=bytes_written;
        bytes_read-=bytes_written;
        bufp+= bytes_written;
    }
}
close(src_file);
close(des_file);
pthread_exit(bytes_copied_p);

}

版权声明:本文为Davidhwj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Davidhwj/p/10930575.html