/* primo esempio di utilizzo dei semafori */ #include #include #include sem_t mysem; void *body(void *arg) { int i,j; sem_wait(&mysem); for (j=0; j<40; j++) { for (i=0; i<1000000; i++); printf("%c",*(char *)arg); } sem_post(&mysem); return NULL; } int main() { pthread_t t1,t2,t3; pthread_attr_t myattr; int err; sem_init(&mysem,0,1); pthread_attr_init(&myattr); err = pthread_create(&t1, &myattr, body, (void *)"."); err = pthread_create(&t2, &myattr, body, (void *)"#"); err = pthread_create(&t3, &myattr, body, (void *)"o"); pthread_attr_destroy(&myattr); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); printf("\n"); return 0; }