diff --git a/fact.c b/fact.c index a953c4937cb1a8af44953370c305197a1efc7951..f191cbf2b94e3be33a3afb745fe2d58775208b73 100644 --- a/fact.c +++ b/fact.c @@ -1,5 +1,5 @@ // -// Created on 23/04/2020. +// Modified on 24/04/2020. // #include <errno.h> #include <sys/types.h> @@ -8,7 +8,7 @@ #include <string.h> #include <math.h> #include <stdbool.h> -//#include <time.h> check this part +//#include <time.h> #include <pthread.h> /* STRUCTURES AND VARIABLES */ @@ -66,7 +66,8 @@ bool is_prime(unsigned long nbr) { * (appelés tests de primalité) consistent à essayer de le diviser par tous les nombres * qui n'excèdent pas sa racine carrée" */ - for (unsigned long j = 2; j <= sqrt(nbr); j++) { + unsigned long j; + for (j = 2; j <= sqrt(nbr); j++) { if ((nbr % j) == 0) { // verify if i is the divider of nbr return (false); // i is a divider of nbr } @@ -84,8 +85,8 @@ factor* prime_divs (unsigned long numbr) { unsigned int n = 0; dividers->number = numbr; unsigned long tmp_number = dividers->number; - - for(unsigned long i = 2; i <= tmp_number ; i++) { + unsigned long i; + for(i = 2; i <= tmp_number ; i++) { if(is_div(dividers->number,i) == true){ if(is_prime(i) == true){ dividers->prime_dividers[n] = i; @@ -110,30 +111,38 @@ void *read_write() { // Read input file line by line till the end of file (eof) while (!feof(f.in)) { fscanf(f.in, "%lu\n", &number); // Read number to be factored - - /* Verify if the number is prime or not, in this case the dividers are calculated */ - dividers = prime_divs(number); // Core function of the app // - - /* MUTEX LOCK - CONTROL OF CRITICAL ACCESS - START */ - if (pthread_mutex_lock(&mutex_readwrite) != 0) { - printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - if (dividers->cnt != 0) { - fprintf(f.out, "%lu ", dividers->number); - for (int i = 0; i < dividers->cnt; i++) { - fprintf(f.out, " %lu", dividers->prime_dividers[i]); + /* Check if the number is > 2 + * in this case skip the number and + * leave a message in the file + */ + if(number > 2) { + /* Verify if the number is prime or not, in this case the dividers are calculated */ + dividers = prime_divs(number); // Core function of the app // + + /* MUTEX LOCK - CONTROL OF CRITICAL ACCESS - START */ + if (pthread_mutex_lock(&mutex_readwrite) != 0) { + printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno)); + exit(EXIT_FAILURE); } - } else { // For internal command test + if (dividers->cnt != 0) { fprintf(f.out, "%lu ", dividers->number); - } - /* MUTEX UNLOCK - CONTROL OF CRITICAL ACCESS - END */ - if (pthread_mutex_unlock(&mutex_readwrite) != 0) { - printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - fprintf(f.out, "\n"); - free(dividers); + int i; + for (i = 0; i < dividers->cnt; i++) { + fprintf(f.out, " %lu", dividers->prime_dividers[i]); + } + } else { // For internal command test + fprintf(f.out, "%lu ", dividers->number); + } + /* MUTEX UNLOCK - CONTROL OF CRITICAL ACCESS - END */ + if (pthread_mutex_unlock(&mutex_readwrite) != 0) { + printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + fprintf(f.out, "\n"); + free(dividers); + } else { + fprintf(f.out, "The number %lu is not > 2, discarded \n", number); + } } pthread_exit((void *) 0); // thread exit: normal termination => 0 } @@ -149,73 +158,12 @@ int main (int argc, char *argv[]) { char *thread_arg; // int NO_THREADS = DEFAULT_NO_THREADS; // Define default number of threads + /* WARNING: ONLY NUMBER POSITIVE AND >2 ARE ACCEPTED */ + printf("!!! WARNING: ONLY POSITIVE NUMBER AND >2 ARE ACCEPTED !!!\n"); + printf(" OTHERWISE THE PROGRAMME CANNOT RUN CORRECTLY\n\n"); + if (argc == 3) { // Check if the number of argument is correct inputfile = argv[1]; outputfile = argv[2]; printf("Thread number not mentioned\n"); printf("The default threads number is %d\n", DEFAULT_NO_THREADS); - } else if(argc == 5) { - thread_arg = argv[1]; - if(strcmp(thread_arg, "-N") != 0) { - printf("%s\n", "Wrong input of the option\n Use option \"-N\"\n"); - exit(EXIT_FAILURE); - } - NO_THREADS = atoi(argv[2]); - if(NO_THREADS > MAX_NO_THREADS) { - printf("Threads requested > %d\n", MAX_NO_THREADS); - exit(EXIT_FAILURE); - } - inputfile = argv[3]; - outputfile = argv[4]; - } else { - printf("%s\n", "Many or few arguments\n"); - printf("Correct command syntax:\n"); - printf("./fact [-N number_of_threads] input_file.txt output_file.txt\n\n"); - exit(EXIT_FAILURE); - } - /* - * START CLOCK to measure CPU time - * calculate programme performance - */ - /* - clock_t start, end; - double cpu_time_used; - start = clock(); - */ - // Open input and output files - f.in = fopen(inputfile, "r"); - f.out = fopen(outputfile, "a+"); - if ((f.in == NULL) || (f.out == NULL)) { - printf("Incorrect input_file or output_file name\n"); - exit(EXIT_FAILURE); - } - - pthread_t thread[NO_THREADS]; // thread IDs - // INITIALIZE MUTEX "mutex_readwrite" - if (pthread_mutex_init(&mutex_readwrite , NULL) != 0){ - printf("\n mutex init failed-mutex_readwrite\n"); - exit(EXIT_FAILURE); - } - for (int i = 0; i < NO_THREADS; i++) { - pthread_create(&thread[i], NULL, (void*)read_write, NULL); - } - for (int i = 0; i < NO_THREADS; i++) { - pthread_join(thread[i], NULL); - } - /* END CLOCK to measure CPU time */ - /* - end = clock(); - cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; - fprintf(f.out, "\nThe programme execution needed %.3f seconds\n\n", cpu_time_used); - printf("\nThe programme execution needed %.3f seconds", cpu_time_used); - printf("\n -- Programme end --\n"); - */ - // Eliminate mutex "mutex_readwrite" - if (pthread_mutex_destroy(&mutex_readwrite) != 0){ - printf("\nmutex_destroy-mutex_readwrite %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - fclose(f.in); - fclose(f.out); - return (EXIT_SUCCESS); -}