#include #include #include #include /* implementa il grafo di esempio1 con i mutex */ pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m1=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m2=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m3=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m4=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m5=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m6=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m7=PTHREAD_MUTEX_INITIALIZER; void *thread_function1(void *arg) { int i; for(i=0;i<5;i++){ printf("Sono il thread1!\n"); sleep(1); } pthread_mutex_unlock(&m); pthread_mutex_unlock(&m1); pthread_mutex_unlock(&m2); return NULL; } void *thread_function2(void *arg) { int i; pthread_mutex_lock(&m); for(i=0;i<5;i++){ printf("\t Sono il thread2!\n"); sleep(1); } pthread_mutex_unlock(&m3); return NULL; } void *thread_function3(void *arg) { int i; pthread_mutex_lock(&m1); for(i=0;i<5;i++){ printf("\t\t Sono il thread3!\n"); sleep(1); } pthread_mutex_unlock(&m4); return NULL; } void *thread_function4(void *arg) { int i; pthread_mutex_lock(&m2); for(i=0;i<10;i++){ printf("\t\t\t\t Sono il thread4!\n"); sleep(1); } pthread_mutex_unlock(&m5); return NULL; } void *thread_function5(void *arg) { int i; pthread_mutex_lock(&m3); pthread_mutex_lock(&m4); printf("\t\t\t\t\t Sono il thread5!\n"); sleep(1); pthread_mutex_unlock(&m6); return NULL; } void *thread_function6(void *arg) { int i; pthread_mutex_lock(&m5); pthread_mutex_lock(&m6); printf("Sono il thread6!\n"); sleep(1); return NULL; } int main(void) { pthread_t mythread1, mythread2, mythread3, mythread4, mythread5, mythread6; pthread_mutex_lock(&m); pthread_mutex_lock(&m1); pthread_mutex_lock(&m2); pthread_mutex_lock(&m3); pthread_mutex_lock(&m4); pthread_mutex_lock(&m5); pthread_mutex_lock(&m6); if ( pthread_create( &mythread1, NULL, thread_function1, NULL) ) { printf("errore di creazione thread."); abort(); } if ( pthread_create( &mythread2, NULL, thread_function2, NULL) ) { printf("errore di creazione thread."); abort(); } if ( pthread_create( &mythread3, NULL, thread_function3, NULL) ) { printf("errore di creazione thread."); abort(); } if ( pthread_create( &mythread4, NULL, thread_function4, NULL) ) { printf("errore di creazione thread."); abort(); } if ( pthread_create( &mythread5, NULL, thread_function5, NULL) ) { printf("errore di creazione thread."); abort(); } if ( pthread_create( &mythread6, NULL, thread_function6, NULL) ) { printf("errore di creazione thread."); abort(); } pthread_join(mythread1, NULL); pthread_join(mythread2, NULL); pthread_join(mythread3, NULL); pthread_join(mythread4, NULL); pthread_join(mythread5, NULL); pthread_join(mythread6, NULL); exit(0); }