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

Update fact.c

parent 0de5c673
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #8913 en échec
// //
// Modified on 26/04/2020. // Modified on 09/05/2020.
// //
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -106,17 +106,24 @@ factor* prime_divs (unsigned long numbr) { ...@@ -106,17 +106,24 @@ factor* prime_divs (unsigned long numbr) {
void *read_write() { void *read_write() {
factor *dividers; factor *dividers;
long in_number;
unsigned long number; unsigned long number;
long neg_number = 0;
// 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, "%ld\n", &in_number); // Read number to be factored
/* Check if the number is > 2 /* Check if the number is > 2
* in this case skip the number and * in this case skip the number and
* leave a message in the file * leave a message in the file
*/ */
if(number > 2) { if((in_number > 2) || (in_number < -2)) {
/* Verify if the number is prime or not, in this case the dividers are calculated */ /* Verify if the number is prime or not, in this case the dividers are calculated */
/* Handle negative number to calculate prime dividers *********NEW***********/
if(in_number < -2) {
neg_number = in_number;
}
number = abs(in_number);
dividers = prime_divs(number); // Core function of the app // dividers = prime_divs(number); // Core function of the app //
/* MUTEX LOCK - CONTROL OF CRITICAL ACCESS - START */ /* MUTEX LOCK - CONTROL OF CRITICAL ACCESS - START */
...@@ -124,14 +131,21 @@ void *read_write() { ...@@ -124,14 +131,21 @@ void *read_write() {
printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno)); printf("\nmutex_lock-mutex_readwrite %s\n", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Handle negative number to calculate prime dividers *********NEW********** */
if (neg_number != 0) {
in_number = neg_number;
neg_number = 0;
}
if (dividers->cnt != 0) { if (dividers->cnt != 0) {
fprintf(f.out, "%lu ", dividers->number); /* fprintf(f.out, "%lu ", dividers->number); ********** OLD *********** */
fprintf(f.out, "%ld", in_number); /* ********NEW********** */
int i; int i;
for (i = 0; i < dividers->cnt; i++) { for (i = 0; i < dividers->cnt; i++) {
fprintf(f.out, " %lu", dividers->prime_dividers[i]); fprintf(f.out, " %lu", dividers->prime_dividers[i]);
} }
} else { // For internal command test } else { // For internal command test
fprintf(f.out, "%lu ", dividers->number); fprintf(f.out, "%ld", in_number); /* ********NEW********** */
/* fprintf(f.out, "%lu ", dividers->number); *********** OLD ********** */
} }
/* MUTEX UNLOCK - CONTROL OF CRITICAL ACCESS - END */ /* MUTEX UNLOCK - CONTROL OF CRITICAL ACCESS - END */
if (pthread_mutex_unlock(&mutex_readwrite) != 0) { if (pthread_mutex_unlock(&mutex_readwrite) != 0) {
...@@ -141,7 +155,7 @@ void *read_write() { ...@@ -141,7 +155,7 @@ void *read_write() {
fprintf(f.out, "\n"); fprintf(f.out, "\n");
free(dividers); free(dividers);
} else { } else {
fprintf(f.out, "The number %lu is not > 2, discarded\n", number); fprintf(f.out, "%ld\n", in_number);
} }
} }
pthread_exit((void *) 0); // thread exit: normal termination => 0 pthread_exit((void *) 0); // thread exit: normal termination => 0
...@@ -158,9 +172,10 @@ int main (int argc, char *argv[]) { ...@@ -158,9 +172,10 @@ 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 */ /* WARNING: ONLY NUMBER POSITIVE AND >2 ARE ACCEPTED
printf("!!! WARNING: ONLY POSITIVE NUMBER AND >2 ARE ACCEPTED !!!\n"); *printf("!!! WARNING: ONLY POSITIVE NUMBER AND >2 ARE ACCEPTED !!!\n");
printf(" OTHERWISE THE PROGRAMME CANNOT RUN CORRECTLY\n\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];
...@@ -174,6 +189,13 @@ int main (int argc, char *argv[]) { ...@@ -174,6 +189,13 @@ int main (int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
NO_THREADS = atoi(argv[2]); NO_THREADS = atoi(argv[2]);
/* If the number of threads is <= 0 ************* VERSION UPDATE ***************
* NO_THREADS is set to DEFAULT_NO_THREADS
*/
if(NO_THREADS <= 0) {
NO_THREADS = 1;
printf("The threads number cannot be <= 0 - Threads number set to 1\n");
}
if(NO_THREADS > MAX_NO_THREADS) { if(NO_THREADS > MAX_NO_THREADS) {
printf("Threads requested > %d\n", MAX_NO_THREADS); printf("Threads requested > %d\n", MAX_NO_THREADS);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -232,4 +254,4 @@ int main (int argc, char *argv[]) { ...@@ -232,4 +254,4 @@ int main (int argc, char *argv[]) {
fclose(f.in); fclose(f.in);
fclose(f.out); fclose(f.out);
return (EXIT_SUCCESS); 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