/* 2crire un programme qui réalise, à l'aide de threads, l'exécution des actions P0 à P7 données
dans le graphe de précédence tel que
P0 : P1,P2,P3
P1 : P4,P5
P2 : P6
P3 : P7
P4 : P7
P5 : P7
P6 : P7
P7 : 
/* graphe.c */
/* Graphe de precedence */
/* A compiler avec l'option -threads ou -lpthread selon les systemes */

#include <pthread.h>

#define NbTh 8 
int compteur[NbTh]={0,0,0,0};
pthread_t tid[NbTh];

void * P0 (void *); void * P1 (void *); void * P2 (void *); void * P3 (void *); void * P4 (void *); void * P5 (void *); void * P6 (void *); void * P7 (void *);
void * Filles[]={P0,P1,P2,P3,P4,P5,P6,P7};
 

/* *************   P0 */
void *P0 (void *k){
printf("Debut de P0\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P0\n"); pthread_exit(NULL);
}

/* *************   P1 */
void *P1 (void *k) {
pthread_join(tid[0], NULL);
printf("Debut de P1\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P1\n"); pthread_exit(NULL);
}

/* *************   P2 */
void *P2 (void *k) {
pthread_join(tid[0], NULL);
printf("Debut de P2\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P2\n"); pthread_exit(NULL);
}

/* *************   P3 */
void *P3 (void *k) {
pthread_join(tid[0], NULL);
printf("Debut de P3\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P3\n"); pthread_exit(NULL);
}

/* *************   P4 */
void *P4 (void *k) {
pthread_join(tid[1], NULL);
printf("Debut de P4\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P4\n"); pthread_exit(NULL);
}

/* *************   P5 */
void *P5 (void *k) {
pthread_join(tid[1], NULL);
printf("Debut de P5\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P5\n"); pthread_exit(NULL);
}

/* *************   P6 */
void *P6 (void *k) {
pthread_join(tid[2], NULL);
printf("Debut de P6\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P6\n"); pthread_exit(NULL);
}

/* *************   P7 */
void *P7 (void *k) {
pthread_join(tid[4], NULL);
pthread_join(tid[5], NULL);
pthread_join(tid[6], NULL);
pthread_join(tid[3], NULL);
printf("Debut de P7\n");
srand(pthread_self()); usleep(rand()%200000);
printf("\tFin de P7\n"); pthread_exit(NULL);
}

main() {
int i,  num;
/* creation des threads */
for(num=0;num<NbTh;num++)
pthread_create(tid+num, 0, (void *(*)())Filles[num], (void *) num);

/* attente de la terminaison de P7 */
    pthread_join(tid[7], NULL); 
printf("Fin de toutes les threads\n");
exit(0); 
}
