Skip to content
Extraits de code Groupes Projets
Valider 33a7d40e rédigé par Hugo Clercq's avatar Hugo Clercq
Parcourir les fichiers

deadlock solution ?

parent 3e8d07e7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -9,6 +9,8 @@
//for is_prime (need -lm for compilation)
#include <math.h>
int finished;
pthread_t *threads;
//Struct for getopt args
typedef struct {
......@@ -321,17 +323,8 @@ void* compute(){
printf("waiting compute\n");
sem_wait(&full);
pthread_mutex_lock(&mutex);
if(counter <=0){
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sem_post(&full);
return NULL;
}
else{
number = pop(buffer);
printf("COMPUTING : %lld\n",number );
}
number = pop(buffer);
printf("COMPUTING : %lld\n",number );
pthread_mutex_unlock(&mutex);
sem_post(&empty);
//end of critical area
......@@ -342,24 +335,20 @@ void* compute(){
sem_wait(&empty2);
pthread_mutex_lock(&mutex2);
div_push(buffer2, result);
if(counter <= 0){
printf("HIIIIII\n" );
pthread_mutex_unlock(&mutex2);
sem_post(&empty2);
sem_post(&full);
return NULL;
}
/*
if(!done && buffer->size <= Args.nThreads)
done2--;
*/
counter--;
printf("counter %d\n",counter );
pthread_mutex_unlock(&mutex2);
sem_post(&full2);
if (counter == 0){
for (int i = Args.nThreads; i >= 0 + finished;i--){
pthread_cancel(threads[i]);
}
}
//printf("done2 = %d\n",done2 );
//end of critical area
}
/*
pthread_mutex_lock(&mutex2);
......@@ -370,6 +359,7 @@ void* compute(){
}
void* consume(){
FILE* fdOut = fopen(Args.output_file, "w");
if(fdOut == NULL){
printf("fopen Error in main for fdOut\n");
......@@ -386,45 +376,17 @@ void* consume(){
sem_wait(&full2);
pthread_mutex_lock(&mutex2);
result= div_pop(buffer2);
if(counter <= 0){
printf("HEEEEEEE\n" );
printf("consumed %lld\n",result.value );
if(result.value == 0){
free(result.dividers);
pthread_mutex_unlock(&mutex2);
sem_post(&empty2);
printf("consumed : %lld\n",result.value);
fprintf(fdOut, "%lld", result.value);
if (result.length != 0){
for(int i = 0; i<result.length; i++){
fprintf(fdOut, " %lld", result.dividers[i]);
}
else{
fprintf(fdOut, "%lld", result.value);
if (result.length != 0){
for(int i = 0; i<result.length; i++){
fprintf(fdOut, " %lld", result.dividers[i]);
}
}
fprintf(fdOut, "\n");
free(result.dividers);
pthread_mutex_unlock(&mutex2);
sem_post(&empty2);
}
}
else{
printf("consumed %lld\n",result.value );
pthread_mutex_unlock(&mutex2);
sem_post(&empty2);
//end of critical area
fprintf(fdOut, "%lld", result.value);
if (result.length != 0){
for(int i = 0; i<result.length; i++){
fprintf(fdOut, " %lld", result.dividers[i]);
}
}
fprintf(fdOut, "\n");
free(result.dividers);
}
fprintf(fdOut, "\n");
free(result.dividers);
pthread_mutex_unlock(&mutex2);
sem_post(&empty2);
}
......
......@@ -2,6 +2,8 @@
#include "functions.c"
int main(int argc, char** argv){
Args.nThreads = 8; //default number of threads
......@@ -72,27 +74,29 @@ int main(int argc, char** argv){
int* error;
pthread_t reader;
pthread_t writer;
pthread_t threads[Args.nThreads];
//pthread_t threads[Args.nThreads];
threads = malloc(sizeof(pthread_t) * Args.nThreads);
finished = 0;
if (pthread_create(&reader, NULL, &produce, NULL) != 0){
printf("Error while creating the reader thread");
printf("Error while creating the reader thread\n");
return EXIT_FAILURE;
}
for(int i =0; i < Args.nThreads; i++){
for(int i =0; i<Args.nThreads; i++){
if(pthread_create(&threads[i], NULL, &compute, NULL) != 0){ //TODO: compute function
printf("Error while creating one of the computing threads");
printf("Error while creating one of the computing threads\n");
return EXIT_FAILURE;
}
}
if (pthread_create(&writer, NULL, &consume, NULL) != 0){
printf("Error while creating the writer thread");
printf("Error while creating the writer thread\n");
return EXIT_FAILURE;
}
if(pthread_join(reader, (void**) &error) != 0){
printf("Error while joining the reader thread");
printf("Error while joining the reader thread\n");
return EXIT_FAILURE;
}
//done = 0 for sure
......@@ -108,14 +112,17 @@ int main(int argc, char** argv){
sem_post(&full);
sem_post(&full);
*/
printf("------------ joined reader ---------------\n");
for(int i =0; i < Args.nThreads; i++){
if(pthread_join(threads[i], (void**) &error) != 0){
printf("Error while joining one of the computing threads");
printf("Error while joining one of the computing threads\n");
return EXIT_FAILURE;
}
printf("---------------------------------one thread finished\n");
finished++;
}
/*
sem_post(&full2);
......@@ -137,11 +144,14 @@ int main(int argc, char** argv){
printf("---------------- join consume ---------------------\n");
sem_destroy(&full);
sem_destroy(&empty);
pthread_mutex_destroy(&mutex);
free_queue(buffer);
free(threads);
sem_destroy(&full2);
sem_destroy(&empty2);
pthread_mutex_destroy(&mutex2);
......
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