- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h> // pour sleep
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
-
- // mutex => MUtuelle EXclusion (permet d'éviter un accès simultané à une variable)
- static pthread_mutex_t mutex;
- static int tab[5];
-
-
- void* threadLectureTableau(void* arg)
- {
- for (int i = 0 ; i != 5 ; i++)
- {
- // blocage du mutex
- // (attente éventuelle de sa libération s'il est déjà bloqué)
- pthread_mutex_lock (&mutex);
-
- printf ("Lecture du tableau 1 : tab[%d] vaut %d\\n", i, tab[i]);
-
- // déblocage du mutex
- pthread_mutex_unlock (&mutex);
-
- sleep(1);
- }
-
- pthread_exit (0);
- }
-
-
- void* threadLectureTableau2(void* arg)
- {
- for (int i = 0 ; i != 5 ; i++)
- {
- // blocage du mutex
- // (attente éventuelle de sa libération s'il est déjà bloqué)
- pthread_mutex_lock (&mutex);
-
- printf ("Lecture du tableau 2 : tab[%d] vaut %d\\n", i, tab[i]);
-
- // déblocage du mutex
- pthread_mutex_unlock (&mutex);
-
- sleep(2);
- }
-
- pthread_exit (0);
- }
-
-
- void* threadEcritureTableau(void* arg)
- {
- // blocage du mutex
- // (attente éventuelle de sa libération s'il est déjà bloqué)
- pthread_mutex_lock (&mutex);
-
- for (int i = 0 ; i != 5 ; i++)
- {
- tab[i] = 2 * i;
- printf ("Ecriture du tableau : tab[%d] vaut %d\\n", i, tab[i]);
- sleep(1);
- }
-
- // déblocage du mutex
- pthread_mutex_unlock (&mutex);
-
- pthread_exit (0);
- }
-
-
- int main(int argc, char *argv[])
- {
- pthread_t thread1, thread2, thread3;
- void *retour;
-
- pthread_mutex_init (&mutex, NULL);
-
- if (pthread_create (&thread1, NULL, threadEcritureTableau, NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 1\\n");
- exit (1);
- }
-
- if (pthread_create (&thread2, NULL, threadLectureTableau, NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 2\\n");
- exit (1);
- }
-
- if (pthread_create (&thread3, NULL, threadLectureTableau2, NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 2\\n");
- exit (1);
- }
-
- // le processus principale est ainsi contraint d'attendre la fin d'exécution
- // des threads fils
- (void)pthread_join (thread1, &retour);
- (void)pthread_join (thread2, &retour);
- (void)pthread_join (thread3, &retour);
- }
-
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h> // pour sleep
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// mutex => MUtuelle EXclusion (permet d'éviter un accès simultané à une variable)
static pthread_mutex_t mutex;
static int tab[5];
void* threadLectureTableau(void* arg)
{
for (int i = 0 ; i != 5 ; i++)
{
// blocage du mutex
// (attente éventuelle de sa libération s'il est déjà bloqué)
pthread_mutex_lock (&mutex);
printf ("Lecture du tableau 1 : tab[%d] vaut %d\\n", i, tab[i]);
// déblocage du mutex
pthread_mutex_unlock (&mutex);
sleep(1);
}
pthread_exit (0);
}
void* threadLectureTableau2(void* arg)
{
for (int i = 0 ; i != 5 ; i++)
{
// blocage du mutex
// (attente éventuelle de sa libération s'il est déjà bloqué)
pthread_mutex_lock (&mutex);
printf ("Lecture du tableau 2 : tab[%d] vaut %d\\n", i, tab[i]);
// déblocage du mutex
pthread_mutex_unlock (&mutex);
sleep(2);
}
pthread_exit (0);
}
void* threadEcritureTableau(void* arg)
{
// blocage du mutex
// (attente éventuelle de sa libération s'il est déjà bloqué)
pthread_mutex_lock (&mutex);
for (int i = 0 ; i != 5 ; i++)
{
tab[i] = 2 * i;
printf ("Ecriture du tableau : tab[%d] vaut %d\\n", i, tab[i]);
sleep(1);
}
// déblocage du mutex
pthread_mutex_unlock (&mutex);
pthread_exit (0);
}
int main(int argc, char *argv[])
{
pthread_t thread1, thread2, thread3;
void *retour;
pthread_mutex_init (&mutex, NULL);
if (pthread_create (&thread1, NULL, threadEcritureTableau, NULL) < 0)
{
fprintf (stderr, "Impossible de créer le thread 1\\n");
exit (1);
}
if (pthread_create (&thread2, NULL, threadLectureTableau, NULL) < 0)
{
fprintf (stderr, "Impossible de créer le thread 2\\n");
exit (1);
}
if (pthread_create (&thread3, NULL, threadLectureTableau2, NULL) < 0)
{
fprintf (stderr, "Impossible de créer le thread 2\\n");
exit (1);
}
// le processus principale est ainsi contraint d'attendre la fin d'exécution
// des threads fils
(void)pthread_join (thread1, &retour);
(void)pthread_join (thread2, &retour);
(void)pthread_join (thread3, &retour);
}