Skip to content
Extraits de code Groupes Projets
Valider f974e601 rédigé par Giovanna Stefanelli's avatar Giovanna Stefanelli
Parcourir les fichiers

Update fact.c

parent 81fbb0fd
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #5641 en échec
// //
// Created on 22/04/2020. // Modified on 24/04/2020.
// //
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
//#include <time.h> check this part //#include <time.h>
#include <pthread.h> #include <pthread.h>
/* STRUCTURES AND VARIABLES */ /* STRUCTURES AND VARIABLES */
...@@ -66,7 +66,8 @@ bool is_prime(unsigned long nbr) { ...@@ -66,7 +66,8 @@ bool is_prime(unsigned long nbr) {
* (appelés tests de primalité) consistent à essayer de le diviser par tous les nombres * (appelés tests de primalité) consistent à essayer de le diviser par tous les nombres
* qui n'excèdent pas sa racine carrée" * 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 if ((nbr % j) == 0) { // verify if i is the divider of nbr
return (false); // i is a divider of nbr return (false); // i is a divider of nbr
} }
...@@ -84,8 +85,8 @@ factor* prime_divs (unsigned long numbr) { ...@@ -84,8 +85,8 @@ factor* prime_divs (unsigned long numbr) {
unsigned int n = 0; unsigned int n = 0;
dividers->number = numbr; dividers->number = numbr;
unsigned long tmp_number = dividers->number; unsigned long tmp_number = dividers->number;
unsigned long i;
for(unsigned long i = 2; i <= tmp_number ; i++) { for(i = 2; i <= tmp_number ; i++) {
if(is_div(dividers->number,i) == true){ if(is_div(dividers->number,i) == true){
if(is_prime(i) == true){ if(is_prime(i) == true){
dividers->prime_dividers[n] = i; dividers->prime_dividers[n] = i;
...@@ -110,30 +111,38 @@ void *read_write() { ...@@ -110,30 +111,38 @@ void *read_write() {
// Read input file line by line till the end of file (eof) // Read input file line by line till the end of file (eof)
while (!feof(f.in)) { while (!feof(f.in)) {
fscanf(f.in, "%lu\n", &number); // Read number to be factored fscanf(f.in, "%lu\n", &number); // Read number to be factored
/* Check if the number is > 2
/* Verify if the number is prime or not, in this case the dividers are calculated */ * in this case skip the number and
dividers = prime_divs(number); // Core function of the app // * leave a message in the file
*/
/* MUTEX LOCK - CONTROL OF CRITICAL ACCESS - START */ if(number > 2) {
if (pthread_mutex_lock(&mutex_readwrite) != 0) { /* Verify if the number is prime or not, in this case the dividers are calculated */
printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno)); dividers = prime_divs(number); // Core function of the app //
exit(EXIT_FAILURE);
} /* MUTEX LOCK - CONTROL OF CRITICAL ACCESS - START */
if (dividers->cnt != 0) { if (pthread_mutex_lock(&mutex_readwrite) != 0) {
fprintf(f.out, "%lu ", dividers->number); printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno));
for (int i = 0; i < dividers->cnt; i++) { exit(EXIT_FAILURE);
fprintf(f.out, " %lu", dividers->prime_dividers[i]);
} }
} else { // For internal command test if (dividers->cnt != 0) {
fprintf(f.out, "%lu ", dividers->number); fprintf(f.out, "%lu ", dividers->number);
} int i;
/* MUTEX UNLOCK - CONTROL OF CRITICAL ACCESS - END */ for (i = 0; i < dividers->cnt; i++) {
if (pthread_mutex_unlock(&mutex_readwrite) != 0) { fprintf(f.out, " %lu", dividers->prime_dividers[i]);
printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno)); }
exit(EXIT_FAILURE); } else { // For internal command test
} fprintf(f.out, "%lu ", dividers->number);
fprintf(f.out, "\n"); }
free(dividers); /* 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 pthread_exit((void *) 0); // thread exit: normal termination => 0
} }
...@@ -149,73 +158,12 @@ int main (int argc, char *argv[]) { ...@@ -149,73 +158,12 @@ int main (int argc, char *argv[]) {
char *thread_arg; // char *thread_arg; //
int NO_THREADS = DEFAULT_NO_THREADS; // Define default number of threads 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 if (argc == 3) { // Check if the number of argument is correct
inputfile = argv[1]; inputfile = argv[1];
outputfile = argv[2]; outputfile = argv[2];
printf("Thread number not mentioned\n"); printf("Thread number not mentioned\n");
printf("The default threads number is %d\n", DEFAULT_NO_THREADS); printf("The default threads number is %d\n", DEFAULT_NO_THREADS);
} else if(argc == 5) { \ No newline at end of file
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);
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter