Thread POSIX
Première année d’IUT Informatique
1 Création de thread
Exercice 1
– Programmez et compilez l’exemple de création de thread vu en cours (Il faut faire
attention aux options de compilation)
– Exécutez ce programme et interprétez son comportement
Exercice 2
//Cette fonction doit recevoir comme argument
//le nom donne au thread
void ma_nouvelle_sequence_control (void arg)
{
int i;
for (i = 0 ; i < 10 ; i++) {
printf ("Thread %s: %d\n", (char*)arg, i);
sleep (1);
}
pthread_exit (0);
}
– En utilisant les fonctions décrites en cours (pthread create, pthread exit, pthread join),
réalisez un programme C qui lance 3 threads. Ces trois threads exécutent la fonction
ma_nouvelle_sequence_control
– Exécutez votre programme et interprétez son comportement
2 Introduction à la synchronisation des threads
2.1 Introduction à la synchronisation des threads
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
Publicité
void thread1_process (void arg)
{
sleep (1);
1
printf ("[2]\n");
pthread_exit (0);
}
void thread2_process (void arg)
{
sleep (5);
printf ("[1]\n");
pthread_exit (0);
}
int main (int argc, char **argv)
{
pthread_t th1, th2;
void *ret;
if (pthread_create (&th1, NULL, thread1_process, NULL) < 0)
{
fprintf (stderr, "pthread_create error\n");
exit (1);
}
if (pthread_create (&th2, NULL, thread2_process, NULL) < 0) {
fprintf (stderr, "pthread_create error\n");
exit (1);
}
(void)pthread_join (th1, &ret);
(void)pthread_join (th2, &ret);
return 0;
Publicité
}
– Exécutez ce programme
– Quelle est la séquence affichée par les threads ?
2.2 contrôler l’ordre de l’exécution
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//Ajout de cette ligne
#include <semaphore.h>
//ajout de cette variable
static sem_t my_sem;
void thread1_process (void arg)
2
{
sleep (1);
//attente sur la barriere
sem_wait (&my_sem);
printf ("[2]\n");
pthread_exit (0);
}
void thread2_process (void arg)
{
sleep (5);
printf ("[1]\n");
//liberation de la barriere
sem_post (&my_sem);
pthread_exit (0);
}
Publicité
main (int ac, char **av)
{
pthread_t th1, th2;
void *ret;
//initialisation de la barrière
sem_init (&my_sem, 0, 0);
if (pthread_create (&th1, NULL, thread1_process, NULL) < 0) {
fprintf (stderr, "pthread_create error\n");
exit (1);
}
if (pthread_create (&th2, NULL, thread2_process, NULL) < 0) {
fprintf (stderr, "pthread_create error\n");
exit (1);
}
(void)pthread_join (th1, &ret);
(void)pthread_join (th2, &ret);
}
– Exécutez la nouvelle version du programme
– Quelle est la séquence affichée maintenant par les threads ?
– Quel est le rôle de la variable my sem ?
2.3 Cas pratique
– Réalisez un programme qui lance 10 threads numérotés de 0 à 9 ; chaque thread
devra afficher son numéro.
– Important : L’ordre de la création des threads sera croissant de 0 à 9.
– On souhaite que la séquence des numéros affichée par les threads soit dans l’ordre
décroissant de 9 à 0.
– Remarque : On n’utilisera pas de synchronisation temporelle avec ’sleep()’
3