/*************************************** * Practice 7 - Winter 2009 ***************************************/ /*************************************** Question 2 ***************************************/ #include #include #include #define N 9 int n_threads; int mat[N][N]; void *init_mat (void *arg); int main (int argc, char *argv[]) { int i, j; pthread_t *thr; if (argc != 2) return; n_threads = atoi (argv[1]); if ((thr = (pthread_t *)malloc (n_threads * sizeof (pthread_t))) == NULL) { printf ("error in the malloc\n"); return 1; } for (i = 0; i < n_threads; i++) pthread_create (&thr[i], NULL, init_mat, (void *)i); for (i = 0; i < n_threads; i++) pthread_join (thr[i], NULL); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) printf ("%d\t", mat[i][j]); printf ("\n"); } } void *init_mat (void *arg) { int i, j, c; int size, start_at; int id = (int)arg; size = N / n_threads; start_at = id * size; for (i = 0, c = start_at; i < size; i++, c++) { for (j = 0; j < N; j++) { mat[c][j] = c + j; } } return NULL; } /*************************************** Question 3 ***************************************/ #include #include #include /* * global variables */ int n_threads; pthread_mutex_t mp; pthread_cond_t *cp; pthread_t *thr; /* * functions */ void *n_sync (void *arg); /* * main */ int main(int argc, char *argv[]) { int i; if (argc < 2) { printf ("Running with 2 threads\n"); n_threads = 2; } else { n_threads = atoi (argv[1]); if (n_threads < 2) { printf ("Need at least 2 threads\n"); return 1; } } if ((thr = (pthread_t *)malloc (n_threads * sizeof (pthread_t))) == NULL) { printf ("error in the malloc\n"); return 1; } if ((cp = (pthread_cond_t *)malloc (n_threads * sizeof (pthread_cond_t))) == NULL) { printf ("error in the malloc\n"); return 1; } pthread_mutex_init (&mp, NULL); for (i = 0; i < n_threads; i++) pthread_cond_init (&cp[i], NULL); for (i = 0; i < n_threads; i++) pthread_create (&thr[i], NULL, n_sync, (void *)i); for (i = 0; i < n_threads; i++) pthread_join (thr[i], NULL); return 0; } /* * n_sync */ void *n_sync (void *arg) { int this_id; int next_id; char dummy; this_id = (int)arg; next_id = (this_id + 1) % n_threads; if (this_id == 0) { printf ("Start!\n"); scanf ("%c", &dummy); } while (1) { if (this_id == 0) { /* * thread 0 - executes first */ printf ("Thread %d\n", this_id); sleep (3); pthread_cond_signal (&cp[next_id]); pthread_mutex_lock (&mp); pthread_cond_wait (&cp[this_id], &mp); pthread_mutex_unlock (&mp); } else { /* * thread 2 - executes second */ pthread_mutex_lock (&mp); pthread_cond_wait (&cp[this_id], &mp); pthread_mutex_unlock (&mp); printf ("Thread %d\n", this_id); sleep (3); pthread_cond_signal (&cp[next_id]); } } return; } /*************************************** Question 4 ***************************************/ #include #include #include #include void *thr_worker (void *arg); sem_t sem_share; int array[5]; int in = 0; int out = 0; int count = 0; int main(int argc, char *argv[]) { int i; int n_threads; int n_resources; pthread_t *thr; if (argc < 3) { n_threads = 4; n_resources = 2; } else { n_threads = atoi (argv[1]); n_resources = atoi (argv[2]); } sem_init (&sem_share, 0, n_resources); if ((thr = (pthread_t *)malloc (n_threads * sizeof (pthread_t))) == NULL) { printf ("error in the malloc\n"); return 1; } for (i = 0; i < n_threads; i++) pthread_create (&thr[i], NULL, thr_worker, (void *)i); for (i = 0; i < n_threads; i++) pthread_join (thr[i], NULL); } void *thr_worker (void *arg) { int this_id = (int)arg; while (1) { sleep (rand () % 10) + 1; sem_wait (&sem_share); printf ("%d is in\n", this_id); sleep (rand () % 10) + 1; sem_post (&sem_share); printf ("%d is out\n", this_id); } } /*************************************** Question 5 ***************************************/ #include #include #include #include void *thr_worker (void *arg); pthread_mutex_t mutex; pthread_t thr; sem_t sem_share; int main(int argc, char *argv[]) { int number; int n_resources; if (argc < 2) { n_resources = 2; } else { n_resources = atoi (argv[1]); } sem_init (&sem_share, 0, n_resources); while (1) { scanf ("%d", &number); sem_wait (&sem_share); pthread_create (&thr, NULL, thr_worker, (void *)number); } return 1; } void *thr_worker (void *arg) { int this_id = (int)arg; printf ("%d is in\n", this_id); sleep (rand () % 10) + 1; printf ("%d is out\n", this_id); sem_post (&sem_share); }