Thread POSIX

Programming · lab

Voir tous les documents en programmation

Thread POSIX

Premi`ere ann´ee d’IUT Informatique

1 Cr´eation de thread

Exercice 1

– Programmez et compilez l’exemple de cr´eation de thread vu en cours (Il faut faire

attention aux options de compilation)

– Ex´ecutez ce programme et interpr´etez 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´ecrites en cours (pthread create, pthread exit, pthread join),

r´ealisez un programme C qui lance 3 threads. Ces trois threads ex´ecutent la fonction

ma_nouvelle_sequence_control

– Ex´ecutez votre programme et interpr´etez son comportement

2 Introduction `a la synchronisation des threads

Publicité

2.1 Introduction `a la synchronisation des threads

#include <stdio.h>

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

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)

{

Publicité

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;

}

– Ex´ecutez ce programme

– Quelle est la s´equence affich´ee par les threads ?

2.2 contrˆoler l’ordre de l’ex´ecution

#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

Publicité

{

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);

}

main (int ac, char **av)

{

pthread_t th1, th2;

void *ret;

//initialisation de la barri`ere

sem_init (&my_sem, 0, 0);

if (pthread_create (&th1, NULL, thread1_process, NULL) < 0) {

fprintf (stderr, "pthread_create error\n");

exit (1);

Publicité

}

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´ecutez la nouvelle version du programme

– Quelle est la s´equence affich´ee maintenant par les threads ?

– Quel est le rˆole de la variable my sem ?

2.3 Cas pratique

– R´ealisez un programme qui lance 10 threads num´erot´es de 0 `a 9 ; chaque thread

devra afficher son num´ero.

– Important : L’ordre de la cr´eation des threads sera croissant de 0 `a 9.

– On souhaite que la s´equence des num´eros affich´ee par les threads soit dans l’ordre

d´ecroissant de 9 `a 0.

– Remarque : On n’utilisera pas de synchronisation temporelle avec ’sleep()’

3