Last active
March 1, 2018 20:51
-
-
Save rebeccajae/545f12cfe96e996d64a655877be73327 to your computer and use it in GitHub Desktop.
ECE426 Lab4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <pthread.h> | |
| #include <stdio.h> | |
| #define NTHREADS 10 | |
| void *sum_runner(void *param); | |
| int sum; | |
| int main(int arc, char *argv[]) | |
| { | |
| sum = 0; | |
| pthread_t threads[NTHREADS]; | |
| void *retvals[NTHREADS]; | |
| int count; | |
| for (count = 0; count < NTHREADS; ++count) | |
| { | |
| if (pthread_create(&threads[count], NULL, sum_runner, count+1) != 0) | |
| { | |
| fprintf(stderr, "error: Cannot create thread # %d\n", count); | |
| break; | |
| } | |
| } | |
| int i; | |
| for (i = 0; i < count; ++i) | |
| { | |
| if (pthread_join(threads[i], &retvals[i]) != 0) | |
| { | |
| fprintf(stderr, "error: Cannot join thread # %d\n", i); | |
| } | |
| } | |
| //sleep(1); | |
| printf("Sum: %d\n", sum); | |
| } | |
| void *sum_runner(void *param) | |
| { | |
| sum += (int)param; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <pthread.h> | |
| #include <stdio.h> | |
| #define N 3 | |
| #define NTHREADS N*N | |
| void *mult_runner(void *param); | |
| int m1[N][N] = { | |
| {1, 2, 3}, | |
| {1, 2, 3}, | |
| {1, 2, 3} | |
| }; | |
| int m2[N][N] = { | |
| {1, 2, 3}, | |
| {1, 2, 3}, | |
| {1, 2, 3} | |
| }; | |
| int mo[N][N]; | |
| int main(int arc, char *argv[]) | |
| { | |
| pthread_t threads[NTHREADS]; | |
| void *retvals[NTHREADS]; | |
| int count; | |
| for (count = 0; count < NTHREADS; ++count) | |
| { | |
| if (pthread_create(&threads[count], NULL, mult_runner, count) != 0) | |
| { | |
| fprintf(stderr, "error: Cannot create thread # %d\n", count); | |
| break; | |
| } | |
| } | |
| int i; | |
| for (i = 0; i < count; ++i) | |
| { | |
| if (pthread_join(threads[i], &retvals[i]) != 0) | |
| { | |
| fprintf(stderr, "error: Cannot join thread # %d\n", i); | |
| } | |
| } | |
| for(i = 0; i < 3; i++){ | |
| for(int j = 0; j < 3; j++){ | |
| printf("%d ", mo[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| } | |
| void *mult_runner(void *param) | |
| { | |
| int i = (int)param/3; | |
| int j = (int)param%3; | |
| mo[i][j] = 0; | |
| int k; | |
| for (k = 0; k < N; k++) | |
| mo[i][j] += m1[i][k]*m2[k][j]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment