0

Im trying to copy files in c. I have no problems copying .txt, but with files like .jpeg i get errors... help pls

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#define BUFFER_SIZE 20

pthread_mutex_t mutex;
pthread_cond_t condP;
pthread_cond_t condC;

int varProductor=0;
int varConsumidor=0;
int cantVar=0;

FILE *fileP;
FILE *fileC;

char bytes[BUFFER_SIZE];
char var;



void* funcProductor(){
    while(!feof(fileP)){        
        pthread_mutex_lock(&mutex);

        while(cantVar==BUFFER_SIZE){                    
            pthread_cond_wait(&condP,&mutex);
        }


        if(!feof(fileP)){       
            bytes[varProductor]=getc(fileP);
            varProductor=(varProductor+1)%BUFFER_SIZE;          
            cantVar++;
        }
        pthread_mutex_unlock(&mutex);   
        pthread_cond_signal(&condC);

    }   
}
void* funcConsumidor(){
    while((varProductor-1)!=varConsumidor){
        pthread_mutex_lock(&mutex);         

        while(cantVar==0){
            pthread_cond_wait(&condC,&mutex);
        }
        if((varProductor-1)!=varConsumidor){    
            putc(bytes[varConsumidor],fileC);
            varConsumidor=(varConsumidor+1)%BUFFER_SIZE;
            cantVar--;
        }

        pthread_mutex_unlock(&mutex);   
        pthread_cond_signal(&condP);    

    }
}

int main(int args,char** argv){ 
    int hilosOrigen=atoi(argv[2]);
    int hilosDestino=atoi(argv[4]);



    fileP=fopen(argv[1],"rb");
    fileC=fopen(argv[3],"wb");

    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&condP,NULL);
    pthread_cond_init(&condC,NULL);

    pthread_t threadP[hilosOrigen];
    pthread_t threadC[hilosDestino];

    int i;

    for(i=0;i<hilosOrigen;i++){
        pthread_create(&threadP[i],NULL,funcProductor,NULL);
    }
    for(i=0;i<hilosDestino;i++){
        pthread_create(&threadC[i],NULL,funcConsumidor,NULL);
    }
    for(i=0;i<hilosOrigen;i++){
        pthread_join(threadP[i],NULL);
    }
    for(i=0;i<hilosDestino;i++){
        pthread_join(threadC[i],NULL);
    }


    return 0;
}

*Im using pthreads to read bytes and saving them into and array, then more pthreads write them in a new file. The reading and writing are controlled Semaphores.....


  • First, please go read this: http://stackoverflow.com/questions/how-to-ask . Next, what exactly is the error your are getting, and on what line of code? You also don't show your code for the threads, so how can we possibly help w/o seeing what those functions are doing? – OldProgrammer Apr 06 '13 at 22:39
  • 1
    That's a really bad way to copy files. Is this code for real or just to learn? – David Heffernan Apr 06 '13 at 22:41
  • While you're reading, [consider this.](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – WhozCraig Apr 06 '13 at 22:41
  • This is just to learn... Also all the thread code is in there. Hem.. the error im getting is.. Segmentation fault... that all the console shows – LinTw83 Apr 06 '13 at 23:53

0 Answers0