Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <pthread.h>
- #include <stdnoreturn.h>
- #define DELAI_MAX 1000 // en ms
- #define CHK(prim) do { if ((prim) == -1) { raler (#prim); } } while (0)
- #define TCHK(exp) do { if ((errno=(exp)) > 0) { raler (#exp); } } while (0)
- struct arg {
- int thrnum; //numero thread
- pthread_barrier_t *pbarN; // pointer sur barrierre unique
- pthread_mutex_t *pmtx; // mutex commun
- pthread_cond_t cond; // lacondition est propre à chaque thread
- int vasy;
- };
- noreturn void raler(const char *msg) {
- perror(msg);
- exit(1);
- }
- void attente_aleatoire(unsigned int *seed, int max_en_ms) {
- int delai_en_ms;
- delai_en_ms = max_en_ms * (rand_r(seed) / (double) RAND_MAX);
- usleep(delai_en_ms * 1000); // convertir en micro-secondes
- }
- void *un_thread(void *arg) {
- unsigned int seed; // pour rand_r
- int thrnum; // mon numéro de thread
- char nombre[100]; // numéro de thread converti en ASCII
- (void) arg; // pour -Wall -Wextra -Werror
- thrnum = 0; // TODO remplacer 0 par numéro de thread
- // on initialise seed avec notre numéro de thread pour avoir des
- // résultats reproductibles
- seed = thrnum;
- attente_aleatoire(&seed, DELAI_MAX);
- // TODO attendre l'ordre d'afficher le '+'
- struct arg *a = arg;
- thrnum = a->thrnum;
- TCHK(pthread_barrier_wait(a->pbarN));
- //todo: END
- CHK (write(1, "+", 1));
- // TODO attendre l'ordre d'afficher mon numéro
- TCHK(pthread_barrier_wait(a->pbarN));
- TCHK(pthread_mutex_lock(a->pmtx));
- while (!a->vasy) {
- TCHK(pthread_cond_wait(&a->cond, a->pmtx));
- }
- a->vasy = 0;
- TCHK(pthread_mutex_unlock(a->pmtx));
- // TODO END attendre l'ordre d'afficher mon numéro
- (void) snprintf(nombre, sizeof nombre, "%d ", thrnum);
- CHK (write(1, nombre, strlen(nombre)));
- return NULL;
- }
- int main(int argc, char *argv[]) {
- int nthreads;
- unsigned int seed = 0; // pour rand_r
- if (argc != 2 || (nthreads = atoi(argv[1])) <= 0) {
- fprintf(stderr, "usage: %s nb-threads\n", argv[0]);
- fprintf(stderr, "\tavec nb-threads > 0\n");
- exit(1);
- }
- pthread_t tid[nthreads];
- /////
- struct arg arg[nthreads];
- pthread_barrier_t barN;
- TCHK(pthread_barrier_init(&barN, NULL, nthreads + 1));
- /////
- for (int i = 0; i < nthreads; i++) {
- ////
- arg[i].thrnum = i;
- arg[i].pbarN = &barN;
- arg[i].vasy = 0;
- /////
- TCHK (pthread_create(&tid[i], NULL, un_thread, NULL));
- }
- attente_aleatoire(&seed, DELAI_MAX);
- CHK (write(1, "debut\n", 6));
- /////
- TCHK(pthread_barrier_wait(&barN));
- ///// ==> threads ecrivent '+'
- TCHK(pthread_barrier_wait(&barN));
- // TODO donner l'ordre aux threads d'écrire leur '+'
- CHK (write(1, "\n", 1)); // doit être affiché après tous les '+'
- // TODO prévenir chaque thread à son tour d'afficher son numéro
- for (int i = 0; i < nthreads; i++) {
- /////
- TCHK(pthread_mutex_lock(&mtx));
- arg[i].vasy = 1;
- TCHK(pthread_mutex_unlock(&mtx));
- // attendre la terminaison de chaque thread
- TCHK (pthread_join(tid[i], NULL));
- TCHK(pthread_cond_destroy(&arg[i].cond));
- }
- // TCHK(pthread_cond_destroy(&arg[i].cond));
- CHK (write(1, "\n", 1));
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement