diff --git a/Makefile b/Makefile index f40f2b3b2110e848df90a0ae865b6b0fa33297a3..358568b955b09611d72e3cd0c9ffd89ae252135c 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,17 @@ all : fact fact : run.o test.o - gcc -g -std=c99 -o fact run.o test.o -lpthread -I${HOME}/local/include -lcunit -L${HOME}/local/lib + gcc -g -std=c99 -o fact run.o test.o -lpthread -I${HOME}/local/include -lcunit -L${HOME}/local/lib -lm run.o : run.c - gcc -g -std=c99 -o run.o -c run.c -W -Wall -lpthread + gcc -g -std=c99 -o run.o -c run.c -W -Wall -lpthread -lm test.o : test.c run.h - gcc -g -std=c99 -o test.o -c test.c -I${HOME}/local/include -W -Wall -lcunit -lpthread + gcc -g -std=c99 -o test.o -c test.c -I${HOME}/local/include -W -Wall -lcunit -lpthread -lm clean : diff --git a/Test_files/big_numb_input.txt b/Test_files/big_numb_input.txt new file mode 100644 index 0000000000000000000000000000000000000000..44ea552b43d405f983049ae739f006c1cd9c3a0a --- /dev/null +++ b/Test_files/big_numb_input.txt @@ -0,0 +1,4 @@ +4856742312 +213486416658 +16975134987934 +2378974143134643 diff --git a/Test_files/long_input.txt b/Test_files/long_input.txt new file mode 100644 index 0000000000000000000000000000000000000000..d4847c217eb1d62ff1691da9554bb54eefb2211a --- /dev/null +++ b/Test_files/long_input.txt @@ -0,0 +1,300 @@ +666343 +463698 +1021406 +506156 +913231 +268205 +982865 +917451 +499176 +529973 +746782 +276324 +435465 +352649 +104121 +981518 +413102 +880413 +676364 +804603 +1012028 +364381 +780260 +907169 +142193 +408465 +266175 +171413 +518652 +770297 +134273 +638327 +826830 +220605 +162237 +53202 +494421 +309475 +361113 +1033840 +319499 +981506 +404743 +897560 +1023027 +354480 +793492 +102802 +843491 +983760 +404152 +283682 +526652 +1018 +324508 +103308 +9422 +793590 +241316 +132914 +978150 +725819 +828636 +224489 +431210 +705190 +470751 +182145 +728190 +3574 +813793 +17035 +795384 +534134 +498463 +385529 +624262 +697671 +613191 +866262 +58631 +290417 +901545 +86993 +607222 +437288 +122029 +840703 +647460 +42875 +135229 +1033268 +361034 +916343 +739080 +975089 +82667 +670049 +640632 +950453 +666343 +463698 +1021406 +506156 +913231 +268205 +982865 +917451 +499176 +529973 +746782 +276324 +435465 +352649 +104121 +981518 +413102 +880413 +676364 +804603 +1012028 +364381 +780260 +907169 +142193 +408465 +266175 +171413 +518652 +770297 +134273 +638327 +826830 +220605 +162237 +53202 +494421 +309475 +361113 +1033840 +319499 +981506 +404743 +897560 +1023027 +354480 +793492 +102802 +843491 +983760 +404152 +283682 +526652 +1018 +324508 +103308 +9422 +793590 +241316 +132914 +978150 +725819 +828636 +224489 +431210 +705190 +470751 +182145 +728190 +3574 +813793 +17035 +795384 +534134 +498463 +385529 +624262 +697671 +613191 +866262 +58631 +290417 +901545 +86993 +607222 +437288 +122029 +840703 +647460 +42875 +135229 +1033268 +361034 +916343 +739080 +975089 +82667 +670049 +640632 +950453 +666343 +463698 +1021406 +506156 +913231 +268205 +982865 +917451 +499176 +529973 +746782 +276324 +435465 +352649 +104121 +981518 +413102 +880413 +676364 +804603 +1012028 +364381 +780260 +907169 +142193 +408465 +266175 +171413 +518652 +770297 +134273 +638327 +826830 +220605 +162237 +53202 +494421 +309475 +361113 +1033840 +319499 +981506 +404743 +897560 +1023027 +354480 +793492 +102802 +843491 +983760 +404152 +283682 +526652 +1018 +324508 +103308 +9422 +793590 +241316 +132914 +978150 +725819 +828636 +224489 +431210 +705190 +470751 +182145 +728190 +3574 +813793 +17035 +795384 +534134 +498463 +385529 +624262 +697671 +613191 +866262 +58631 +290417 +901545 +86993 +607222 +437288 +122029 +840703 +647460 +42875 +135229 +1033268 +361034 +916343 +739080 +975089 +82667 +670049 +640632 +950453 diff --git a/run.c b/run.c index 146ebb96c14c53928aeedfd2db1c2869dcbb2439..4a273392f31c7154dbfcb3e1a81ef72531d16f5e 100755 --- a/run.c +++ b/run.c @@ -4,6 +4,7 @@ #include <unistd.h> #include <pthread.h> #include <semaphore.h> +#include <math.h> int N; //travailler avec des linkendList @@ -60,7 +61,7 @@ sem_t full2; void put_in_buffer_1(char *c, struct buffer_rc *ptr){ //ajouter un element dans le buffer 1 - ptr->tab[ptr->head] = malloc(sizeof(c)); // on stoxke de espace + ptr->tab[ptr->head] = malloc(sizeof(char*)); // on stoxke de espace //ptr->tab[ptr->head] = c; strcpy(ptr->tab[ptr->head],c); //copie @@ -82,7 +83,7 @@ char *get_from_buffer_1(struct buffer_rc *ptr){ // chercher dans le buffer 1 } void put_in_buffer_2(struct queue *ptr, struct buffer_cw *buf){ //ajouter un element dans le buffer 2 - buf->tab[buf->head] = malloc(sizeof(*ptr)); + buf->tab[buf->head] = malloc(sizeof(queue_t*)); buf->tab[buf->head] = ptr; buf->len++; //augmente l'espace occupé dans le buffer buf->head = (buf->head + 1)%buf->size; @@ -92,6 +93,7 @@ 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]); buf->tab[buf->tail] = NULL; buf->len--; //diminue l'espace occupé par le buffer buf->tail = (buf->tail + 1)%buf->size; @@ -111,7 +113,7 @@ int is_prime(unsigned long long number) { // Vérifie si number est un nombre pr return 0 ; } } - for (unsigned long long i = 3 ; i <= (number/2); i = i + 2) { //regarde les differents nombres pouvant etre diviser number + for (unsigned long long i = 3 ; i*i <= number; i = i + 2) { //regarde les differents nombres pouvant etre diviser number if (is_div(number,i) != 0) { // si i peut diviser number return 0 ; //renvoi 0 => FAUX } @@ -119,8 +121,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){ //testcommentaire? +void enqueue(queue_t* q, unsigned long long val){ struct node *ptr; ptr = malloc(sizeof(node_t)); @@ -152,19 +153,14 @@ void enqueue(queue_t* q, unsigned long long val){ //testcommentaire? queue_t* prime_divs(unsigned long long number){ - struct queue new_queue; - new_queue.size = 0; - new_queue.final = 0; - new_queue.tail = malloc(sizeof(node_t)); - if(new_queue.tail==NULL){return NULL;} - - struct queue *ptr; + queue_t *ptr; ptr = malloc(sizeof(queue_t)); if (ptr == NULL){ - free(new_queue.tail); free(ptr); return NULL;} - *ptr = new_queue; + ptr->size = 0; + ptr->final = 0; + ptr->tail = malloc(sizeof(node_t)); for (unsigned long long i = 2; i <= number/2; i++){ if (is_div(number, i) && is_prime(i) == 1){ @@ -199,6 +195,7 @@ void *writing(void *param){ param1->tab[i] = NULL; } free(param1->tab); + free(pr_divs->tail); free(pr_divs); return NULL; } @@ -208,7 +205,7 @@ void *writing(void *param){ else{ FILE *file2 = param1->file2; - struct node *current; + node_t *current; current = malloc(sizeof(node_t)); if (current == NULL){return NULL;} *current = *pr_divs->tail; @@ -216,6 +213,7 @@ void *writing(void *param){ fprintf(file2,"%llu",current->value); current = current->next; + for (int i = 1; i < pr_divs->size; i++) { @@ -224,6 +222,8 @@ void *writing(void *param){ } fputc('\n',file2); + free(pr_divs->tail); + free(pr_divs); free(current); } @@ -262,14 +262,12 @@ void *calculating(void *param){ pthread_mutex_unlock(&mutex2); sem_post(&full2); + free(chaine); return NULL; } struct queue *pr_divs; - pr_divs = malloc(sizeof(queue_t)); - if (pr_divs == NULL){return NULL;} - pr_divs = prime_divs(strtoll(chaine,NULL,0)); sem_wait(&empty2); @@ -277,7 +275,7 @@ void *calculating(void *param){ put_in_buffer_2(pr_divs,buffer_2); pthread_mutex_unlock(&mutex2); sem_post(&full2); - + free(chaine); } return NULL; @@ -340,7 +338,7 @@ 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; @@ -359,6 +357,7 @@ struct buffer_cw * buff_init_2(FILE *file2){ return NULL;} ptr2->tab = malloc(2*N*sizeof(struct queue*)); + ptr2->size = 2*N; ptr2->len = 0; ptr2->head = 0; @@ -370,14 +369,13 @@ struct buffer_cw * buff_init_2(FILE *file2){ } struct buffer_rccw *buff_init_12(struct buffer_rc *ptr1,struct buffer_cw *ptr2){ - struct buffer_rccw buffer_12; - buffer_12.struct1 = ptr1; - buffer_12.struct2 = ptr2; struct buffer_rccw *ptr3; ptr3 = malloc(sizeof(struct buffer_rccw)); if (ptr3 == NULL){return NULL;} - *ptr3 = buffer_12; + ptr3->struct1 = ptr1; + ptr3->struct2 = ptr2; + return ptr3; } @@ -400,13 +398,21 @@ void thread_create_join(struct buffer_rc *ptr1,struct buffer_cw *ptr2,struct buf pthread_join(writer,NULL); } +void mut_sem_destroy(void){ + pthread_mutex_destroy(&mutex1); + pthread_mutex_destroy(&mutex2); + sem_destroy(&full1); + sem_destroy(&full2); + sem_destroy(&empty1); + sem_destroy(&empty2); +} + int run (char *input,char *output, int n_threads){ N = n_threads; - - FILE *file1 = NULL; - FILE *file2 = NULL; + FILE *file1; + FILE *file2; file1 = fopen(input,"r"); if (file1 == NULL){ @@ -428,9 +434,12 @@ int run (char *input,char *output, int n_threads){ sem_2_initializer(ptr2); thread_create_join(ptr1,ptr2,ptr3); + mut_sem_destroy(); + fclose(file1); fclose(file2); + free(ptr1->tab); free(ptr1); free(ptr2); free(ptr3); diff --git a/run.h b/run.h index 1abc27782d04ff1cb8e791a968d5edd7e4640167..fc96eee09665172f9413d9832e6b7b52257d7414 100644 --- a/run.h +++ b/run.h @@ -69,6 +69,7 @@ struct buffer_cw * buff_init_2(FILE *file2); struct buffer_rccw *buff_init_12(struct buffer_rc *ptr1,struct buffer_cw *ptr2); void thread_create_join(struct buffer_rc *ptr1,struct buffer_cw *ptr2,struct buffer_rccw *ptr3); +void mut_sem_destroy(void); int run (char *input,char *output,int n_threads); diff --git a/test.c b/test.c index 54df28794fd151afaf05396f03ec44b160583b4d..400695f394ee0d8e1397b081d23007f9dc47ccf3 100644 --- a/test.c +++ b/test.c @@ -217,7 +217,7 @@ int main(int argc, char *argv[]){ CU_basic_run_tests(); - CU_automated_run_tests(); + //CU_automated_run_tests(); CU_basic_show_failures(CU_get_failure_list()); } else