#include #include #include int glob=10; // variable partage int Work_Stop = 1; void* Processus_P1 () { int x; x=glob; sleep(5); x=x+7; glob = x; printf("ici P1 [%d], glob = %d\n",(int)pthread_self(), glob); pthread_exit(NULL); } void* Processus_P2 () { int x; x=glob; sleep(5); x=x-2; glob = x; printf("ici P2 [%d], glob = %d\n",(int)pthread_self(),glob); pthread_exit(NULL); } int main( ) { pthread_t P1, P2; printf("valeur intiale de vraibale partage glob = %d\n",glob); //creation d'un thread pour P1 if ( pthread_create(&P1, NULL, Processus_P1 , NULL) != 0) return -1; printf("creation du Processus P1 : thread[%d] avec succes\n",(int)P1); // creation d'un thread pour P2 if ( pthread_create(&P2, NULL, Processus_P2 , NULL) != 0) return -1; printf("creation du Processus P2 :thread [%d] avec succes\n",(int)P2); // attendre la fin des Processus (threads) pthread_join(P1,NULL); pthread_join(P2,NULL); printf("Fin d'execution de Processus P1 et P2\n"); printf("Valeur finale variable partage de glob = %d \n",glob); printf("\n\n"); return 0; }