In: Computer Science
The purpose of this question is to practice the pthread built in functions.
The following c program is a simple program to make a matrix of integers and print it.
//File name: a.c
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int** a;
int main(){
time_t t;
int m, n, i, j; //m is the numbers of rows and n is the number of columns.
printf("Enter the number of rows, and columns: ");
scanf("%d%d", &m, &n);
printf("%d, %d\n", m, n);
srand((unsigned) time(&t));
a=(int**) malloc(m*sizeof(int*));
for(j = 0; j < n; j++)
a[j] = (int*) malloc(n * sizeof(int*));
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
a[i][j] = rand() % 1000;
for(i = 0; i < m; i++){
for(j = 0; j < n; j++)
printf("%d,", a[i][j]);
printf("\n");
}
return 0;
}
Your project uses pthread built-in functions based on the following conditions:
1. The program reads from the console the number of rows and the number of columns (like the above program). Therefore, the matrix has m rows and n columns.
2. The program creates m threads.
3. Each thread assigns random numbers to one row of the matrix.
4. The function main, sorts each row.
5. Each thread displays its sorted row.
6. The function: main displays the entire matrix.
Answer: ?
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
#define MAX 4
void
*mult(void* arg)
{
int
*data =
(int
*)arg;
int
k = 0, i =
0;
int
x =
data[0];
for
(i = 1; i <=
x; i++)
{
k += data[i]*data[i+x];
}
int
*p =
(int*)malloc(sizeof(int));
*p = k;
pthread_exit(p);
}
int
main()
{
int
matA[MAX][MAX];
int
matB[MAX][MAX];
int
r1=MAX,c1=MAX,r2=MAX,c2=MAX,i,j,k;
printf("Enter the number of rows, and columns: ");
scanf("%d%d", &m, &n);
printf("%d, %d\n", m, n);
for
(i = 0; i <
r1; i++)
for
(j = 0; j < c1; j++)
matA[i][j]
= rand() % 10;
for
(i = 0; i <
r1; i++)
for
(j = 0; j < c1; j++)
matB[i][j]
= rand() % 10;
for
(i = 0; i <
r1; i++){
for(j = 0;
j < c1; j++)
printf("%d
",matA[i][j]);
printf("\n");
}
for
(i = 0; i <
r2; i++){
for(j = 0;
j < c2; j++)
printf("%d
",matB[i][j]);
printf("\n");
}
int
max =
r1*c2;
pthread_t *threads;
threads =
(pthread_t*)malloc(max*sizeof(pthread_t));
int
count =
0;
int* data = NULL;
for
(i = 0; i <
r1; i++)
for
(j = 0; j < c2; j++)
{
data
= (int
*)malloc((20)*sizeof(int));
data[0]
= c1;
for
(k = 0; k < c1; k++)
data[k+1]
= matA[i][k];
for
(k = 0; k < r2; k++)
data[k+c1+1]
= matB[k][j];
pthread_create(&threads[count++],
NULL,
mult,
(void*)(data));
}
printf ("After arranging rows in ascending order\n");
for (i=0;i<m;++i) {
for (j=0;j<n;++j) {
for (k=(j+1);k<n;++k) {
if (matA[i][j] > matA[i][k]) {
a = matA[i][j];
matA[i][j] = matA[i][k];
matA[i][k] = a;
}
}
}
}
for (i=0;i<m;++i) {
for (j=0;j<n;++j) {
printf (" %d",matT[i][j]);
}
printf ("\n");
}
printf ("After arranging the columns in descending order \n");
for (j=0;j<n;++j) {
for (i=0;i<m;++i) {
for (k=i+1;k<m;++k) {
if (matB[i][j] < matB[k][j]) {
a = matB[i][j];
matB[i][j] = matB[k][j];
matB[k][j] = a;
}
}
}
}
for (i=0;i<m;++i) {
for (j=0;j<n;++j) {
printf (" %d",matB[i][j]);
}
printf ("\n");
}
}
printf("RESULTANT MATRIX IS :-
\n");
for
(i = 0; i <
max; i++)
{
void
*k;
pthread_join(threads[i],
&k);
int
*p =
(int
*)k;
printf("%d
",*p);
if
((i +
1) % c2 == 0)
printf("\n");
}
return
0;
}
}