Skip to content
GitLab
Explorer
Connexion
S'inscrire
Navigation principale
Rechercher ou aller à…
Projet
L
lepl1503-2020-groupe-C1
Gestion
Activité
Membres
Code
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Déploiement
Releases
Registre de conteneur
Registre de modèles
Analyse
Analyse des contributeurs
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté
Contribuer à GitLab
Donner votre avis
Conditions générales et politique de confidentialité
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
Corentin Lengelé
lepl1503-2020-groupe-C1
Validations
33a7d40e
Valider
33a7d40e
rédigé
5 years ago
par
Hugo Clercq
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
deadlock solution ?
parent
3e8d07e7
Aucune branche associée trouvée
Branches contenant la validation
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
2
Masquer les modifications d'espaces
En ligne
Côte à côte
Affichage de
2 fichiers modifiés
functions.c
+22
-60
22 ajouts, 60 suppressions
functions.c
main.c
+18
-8
18 ajouts, 8 suppressions
main.c
avec
40 ajouts
et
68 suppressions
functions.c
+
22
−
60
Voir le fichier @
33a7d40e
...
...
@@ -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
);
}
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
main.c
+
18
−
8
Voir le fichier @
33a7d40e
...
...
@@ -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
);
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter