#include #include #include #include #include #include typedef struct th{ int rang; int nbAffich; }th; void *affichageThread (void *arg){ th *str_th = (th *) arg; int iter = 0; while (iter < str_th->nbAffich){ printf("Je suis le thread de rang %d , mon identificateur est %lu\n", str_th->rang, (long unsigned) pthread_self()); iter++; } pthread_exit(NULL); } int main (int argc, char *argv[]){ if (argc != 3){ fprintf(stderr, "Erreur! Usage: %s nombre_de_threads nombre_d'affichages\n", argv[0]); exit(99); } if (atoi(argv[1]) < 1 || atoi(argv[2]) < 1){ fprintf(stderr, "Erreur! Les arguments doivent êtres entiers et supérieurs à 0!\n"); exit(98); } th new_th; new_th.rang = 0; new_th.nbAffich = atoi(argv[2]); th* pnew_th; //pnew_th = malloc(sizeof(th) * atoi(argv[1])); //new_th->nbAffich = atoi(argv[2]); pthread_t* ptid = NULL; ptid = malloc(sizeof(pthread_t) * atoi(argv[1])); if (ptid == NULL){ perror("Erreur: malloc ptid"); exit(97); } for (int i = 0; i < atoi(argv[1]); i++){ //new_th[i].rang = i; //pnew_th->rang = i; //new_th.rang = i; pnew_th = malloc(sizeof(th)); (*pnew_th).rang = i; (*pnew_th).nbAffich = atoi(argv[2]); if (pthread_create(&ptid[i], NULL, affichageThread, &pnew_th/*[i]*/) != 0){ perror("\nProblème lors de la création du thread fils\n"); exit(95); } } for (int i = 0; i < atoi(argv[1]); i++) pthread_join(ptid[i], NULL); exit(0); }