1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static int tab[10];
struct borne {
int debut;
int fin;
};
//fonction excecuter par les deux thread
void *multiplication (void * param) {
struct borne *b;
bb=(struct borne*) param;
int i;
int n=0;
n=tab[i];
for (i=b->debut;i<=b->fin;i++){
tab[i]=2*tab[i];
printf ("le resultat de la multiplication est %d,tab[i]");
}
}
main (int ac, char **av)
{
struct borne *bb;
struct borne *bb1;
bb = malloc(sizeof (struct borne));
bb1 = malloc(sizeof(struct borne));
bb->debut = 0;
bb->fin = 4;
bb1->debut = 5;
bb1->fin = 9;
//declaration des threads
pthread_t t1, t2;
void *attente;
//initilaisation du verrou
//pthread_mutex_init (&verrou, NULL);
//creation du thread 1
if (pthread_create (&t1, NULL, multiplication,(void *) bb) < 0) {
fprintf (stderr, "erreur lors de la creation du thread 1\n");
exit (1);
}
//creation du thread 2
if (pthread_create (&t2, NULL, multiplication, (void *) bb1) < 0) {
fprintf (stderr, "erreur lors de la creation du thread 2\n");
exit (1);
}
//attente de la fin du thread 1
(void)pthread_join (t1, &attente);
//attente de la fin du thread 2
(void)pthread_join (t2, &attente);
} |
Partager