Skip to content
Extraits de code Groupes Projets
Valider 736a7eb6 rédigé par Arnaud Lefebvre's avatar Arnaud Lefebvre
Parcourir les fichiers

Replace main.c

parent 96b4a95d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #10007 réussi
......@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdint.h>
#include <pthread.h>
#include <semaphore.h>
......@@ -16,6 +17,8 @@ char *f_out;
int nthreads = 1;
buffer1 *buffer_1;
buffer2 *buffer_2;
int nlines1;
int nlines2;
void *reading() {
......@@ -35,8 +38,8 @@ void *reading() {
printf("file_out opened with succes.\n");
}
int number;
while (fscanf(file_in, "%d", &number) != EOF) {
uint64_t number;
while (fscanf(file_in, "%lu", &number) != EOF) {
sem_wait(&(buffer_1->free));
pthread_mutex_lock(&(buffer_1->mutex));
put_node_t(buffer_1->numbers, number);
......@@ -53,8 +56,10 @@ void *reading() {
void *calculating() {
while (buffer_1->can_stop == false || buffer_1->numbers->length != 0) {
int number = 0;
int nlines1 = count_lines(f_in);
while ((buffer_1->can_stop == false || buffer_1->numbers->length != 0) && nlines1 != 0) {
nlines1 --;
uint64_t number;
sem_wait(&(buffer_1->full));
pthread_mutex_lock(&(buffer_1->mutex));
......@@ -79,7 +84,9 @@ void *calculating() {
}
void *writing() {
while(buffer_2->can_stop == false || buffer_2->prime_numbers->length > 0) {
int nlines2 = count_lines(f_in);
while((buffer_2->can_stop == false || buffer_2->prime_numbers->length > 0) && nlines2 != 0) {
nlines2 --;
sem_wait(&(buffer_2->full));
pthread_mutex_lock(&(buffer_2->mutex));
......@@ -89,7 +96,7 @@ void *writing() {
int length = removed->length;
for (int i = 0; i < length; i++) {
fprintf(file_out, "%d ", get_node_t(removed));
fprintf(file_out, "%lu ", get_node_t(removed));
}
clear_list_t(removed);
free(removed);
......@@ -158,7 +165,7 @@ int main(int argc, char *argv[]) {
printf("Read_thread_join OK.\n");
}
/*
for (int i = 0; i < nthreads; i++) {
if (pthread_cancel(calc[i]) != 0) {
printf("Error with cancel calculating thread[%d].\n", i);
......@@ -175,22 +182,6 @@ int main(int argc, char *argv[]) {
printf("Calc_thread_join[%d] OK.\n", i);
}
}
*/
for (int i = 0; i < nthreads; i++) {
if (pthread_cancel(calc[i]) != 0) {
printf("Error with cancel calculating thread[%d].\n", i);
if (pthread_join(calc[i], NULL) != 0) {
printf("Error with join calculating thread.\n");
return -1;
} else {
printf("Calc_thread_join[%d] OK.\n", i);
}
} else {
printf("Calc_thread_cancel[%d].\n", i);
}
}
if (pthread_join(write, NULL) != 0) {
printf("Error with join writing thread.\n");
......
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