Use C programming to write a multi-thread program where the userinputs any number into the matrices and handle two of the matrices,A and B. The code must randomly generate the content of matrix Aand B with specified dimension provided as two command arguments.The user must also input how many rows and columns that they want.Then create three threads:
a. one thread will transpose the A and B
b. one thread will calculate addition of two matrices as C = A +B
c. one thread will calculate subtraction of two matrices as C =A - B
Also print out the results for each thread. Remember to allocatespace for C and the transposed A and B.
The following image shows a transposed matrix.
---------------------------------------------------------------------------------------
Sample Array Statistics Solution that we discussed in class:
//From OS concepts of John Wiley & Sons//please run this program as ./main 3 5 6 7 2 4 6// It is up to you as what integer array to supply as thearguments.
#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <sys/types.h>
/* the list of integers */int *list;
/* the threads will set these values */double average;int maximum;int minimum;
void *calculate_average(void *param);void *calculate_maximum(void *param);void *calculate_minimum(void *param);
int main(int argc, char *argv[]){int i;int num_of_args = argc-1;pthread_t tid_1;pthread_t tid_2;pthread_t tid_3;
/* allocate memory to hold array of integers */list = malloc(sizeof(int)*num_of_args);
for (i = 0; i < num_of_args; i++)list = atoi(argv[i+1]);
/* create the threads */pthread_create(&tid_1, 0, calculate_average,&num_of_args);pthread_create(&tid_2, 0, calculate_maximum,&num_of_args);pthread_create(&tid_3, 0, calculate_minimum,&num_of_args);
/* wait for the threads to exit */pthread_join(tid_1, NULL);pthread_join(tid_2, NULL);pthread_join(tid_3, NULL);
printf("The average is %f\n", average);printf("The maximum is %d\n", maximum);printf("The minimum is %d\n", minimum);
return 0;}
void *calculate_average(void *param){int count = *(int *)param;int i, total = 0;
printf("count = %d\n",count);for (i = 0; i < count; i++)printf("%d\n",list);
for (i = 0; i < count; i++)total += list;
average = total / count;
pthread_exit(0);}
void *calculate_maximum(void *param){int count = *(int *)param;int i;
maximum = list[0];
for (i = 1; i < count; i++)if (list > maximum)maximum = list;
pthread_exit(0);}
void *calculate_minimum(void *param){int count = *(int *)param;int i;
minimum = list[0];
for (i = 1; i < count; i++)if (list < minimum)minimum = list;
pthread_exit(0);}
a b C ~:1 A = def 2 x 3 a AT = b be f C 3x2
Use C programming to write a multi-thread program where the user inputs any number into the matrices and handle two of t
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am