diff --git a/run.c b/run.c index 46a3bf7afcb82fac0f6139a10c7455f80dc86097..05c7155ddb1f68932cac9395191c8f0a46dfeeee 100755 --- a/run.c +++ b/run.c @@ -1,15 +1,16 @@ #include "run.h" void put_in_buffer_1(char *c, struct buffer_rc *ptr){ //ajouter un element dans le buffer 1 - - ptr->tab[ptr->head] = malloc(sizeof(char*)); // on stoxke de espace + + ptr->tab[ptr->head] = malloc(sizeof(char*)); // on stoxke de espace + if(ptr->tab[ptr->head] ==NULL){return;} //ptr->tab[ptr->head] = c; strcpy(ptr->tab[ptr->head],c); //copie - - ptr->len++; - ptr->head = (ptr->head + 1)%ptr->size; //chaque fois qu'un élément est ajouté, + + ptr->len++; + ptr->head = (ptr->head + 1)%ptr->size; //chaque fois qu'un élément est ajouté, //on augmente le pointeur de 1 mais l'espace du buffer est de size donc faire un modulo - + } char *get_from_buffer_1(struct buffer_rc *ptr){ // chercher dans le buffer 1 @@ -30,7 +31,7 @@ void put_in_buffer_2(struct queue *ptr, struct buffer_cw *buf){ //ajouter un ele } queue_t *get_from_buffer_2(struct buffer_cw *buf){ // trouver un element - + struct queue *result; result = buf->tab[buf->tail]; //donne une valeur a result //free(buf->tab[buf->tail]); @@ -61,7 +62,7 @@ int is_prime(unsigned long long number) { // Vérifie si number est un nombre pr return 1 ; //sinon => VRAI } -void enqueue(queue_t* q, unsigned long long val){ +void enqueue(queue_t* q, unsigned long long val){ struct node *ptr; ptr = malloc(sizeof(node_t)); @@ -109,18 +110,18 @@ void *writing(void *param){ struct buffer_cw *param1 = (struct buffer_cw *) param; int stop = 0; - - while(param1->stop == 0){ - + + while(param1->stop == 0){ + sem_wait(&full2); pthread_mutex_lock(&mutex2); queue_t *pr_divs = get_from_buffer_2(param); pthread_mutex_unlock(&mutex2); - sem_post(&empty2); + sem_post(&empty2); if (pr_divs->final == 1) - { - + { + if (stop == N-1){ free(pr_divs); free(param1->tab); @@ -130,7 +131,7 @@ void *writing(void *param){ stop++; } else{ - FILE *file2 = param1->file2; + FILE *file2 = param1->file2; node_t *current; current = pr_divs->tail; @@ -139,7 +140,7 @@ void *writing(void *param){ fprintf(file2,"%llu",current->value); node_t *toFree = current; current = current->next; - + for (int i = 1; i < pr_divs->size; i++) { @@ -149,14 +150,14 @@ void *writing(void *param){ current = current->next; } fputc('\n',file2); - + free(pr_divs->tail); free(pr_divs); } - + } - + return NULL; } @@ -166,8 +167,8 @@ void *calculating(void *param){ struct buffer_rc *buffer_1 = param1->struct1; struct buffer_cw *buffer_2 = param1->struct2; - - while(buffer_1->stop == 0){ + + while(buffer_1->stop == 0){ sem_wait(&full1); pthread_mutex_lock(&mutex1); @@ -176,7 +177,7 @@ void *calculating(void *param){ sem_post(&empty1); if (strcmp("stop",chaine) == 0) - { + { struct queue *final; final = malloc(sizeof(queue_t)); if (final == NULL){return NULL;} @@ -185,45 +186,45 @@ void *calculating(void *param){ sem_wait(&empty2); pthread_mutex_lock(&mutex2); - put_in_buffer_2(final,buffer_2); + put_in_buffer_2(final,buffer_2); pthread_mutex_unlock(&mutex2); sem_post(&full2); - + free(chaine); return NULL; } - + struct queue *pr_divs; pr_divs = prime_divs(strtoll(chaine,NULL,0)); - + sem_wait(&empty2); pthread_mutex_lock(&mutex2); - put_in_buffer_2(pr_divs,buffer_2); + put_in_buffer_2(pr_divs,buffer_2); pthread_mutex_unlock(&mutex2); sem_post(&full2); free(chaine); } - + return NULL; } void *reading (void *param){ struct buffer_rc *param1= (struct buffer_rc *) param; - + char chaine[30]; FILE *input; input = param1->file1; - + while (fgets(chaine,30,input) != NULL){ - + sem_wait(&empty1); pthread_mutex_lock(&mutex1); - + put_in_buffer_1(chaine,param1); //put each line in the buffer #1 - + pthread_mutex_unlock(&mutex1); sem_post(&full1); } @@ -234,11 +235,11 @@ void *reading (void *param){ pthread_mutex_lock(&mutex1); put_in_buffer_1("stop\0",param1); //put each stop line in the buffer #1 - + pthread_mutex_unlock(&mutex1); sem_post(&full1); } - + return NULL; } @@ -265,14 +266,14 @@ struct buffer_rc * buff_init_1(FILE *file1){ if (ptr1 == NULL){ free(ptr1); return NULL;} - ptr1->tab = malloc(2*N*sizeof(char*)); + ptr1->tab = malloc(2*N*sizeof(char*)); ptr1->size = 2*N; ptr1->len = 0; ptr1->head = 0; ptr1->tail = 0; ptr1->stop = 0; ptr1->file1 = file1; - + return ptr1; } @@ -282,7 +283,7 @@ struct buffer_cw * buff_init_2(FILE *file2){ ptr2 = malloc(sizeof(struct buffer_cw)); if (ptr2 == NULL){ return NULL;} - + ptr2->tab = malloc(2*N*sizeof(struct queue*)); ptr2->size = 2*N; @@ -291,7 +292,7 @@ struct buffer_cw * buff_init_2(FILE *file2){ ptr2->tail = 0; ptr2->stop = 0; ptr2->file2 = file2; - + return ptr2; } @@ -302,7 +303,7 @@ struct buffer_rccw *buff_init_12(struct buffer_rc *ptr1,struct buffer_cw *ptr2){ if (ptr3 == NULL){return NULL;} ptr3->struct1 = ptr1; ptr3->struct2 = ptr2; - + return ptr3; } @@ -337,7 +338,7 @@ void mut_sem_destroy(void){ int run (char *input,char *output, int n_threads){ N = n_threads; - + FILE *file1; FILE *file2; @@ -355,7 +356,7 @@ int run (char *input,char *output, int n_threads){ struct buffer_rc *ptr1 = buff_init_1(file1); struct buffer_cw *ptr2 = buff_init_2(file2); struct buffer_rccw *ptr3 = buff_init_12(ptr1,ptr2); - + sem_1_initializer(ptr1); sem_2_initializer(ptr2); diff --git a/run.o b/run.o new file mode 100644 index 0000000000000000000000000000000000000000..b0081eb4a3921755482a912e35e3db220d1093b7 Binary files /dev/null and b/run.o differ diff --git a/test.o b/test.o new file mode 100644 index 0000000000000000000000000000000000000000..8292e4a379af2d6e89d47408f40a49e15fc98db3 Binary files /dev/null and b/test.o differ