0% found this document useful (0 votes)
28 views

Matrix Operations (+,-, ,/) in C/CPP Using Pthreads

The document describes a lab assignment to implement multithreading for matrix operations using pthreads. It defines functions to add, subtract, multiply and divide two 2x2 matrices in separate threads. The main function gets the matrix elements as input, creates threads to perform the operations, and displays the results.

Uploaded by

Vedang Bhange
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Matrix Operations (+,-, ,/) in C/CPP Using Pthreads

The document describes a lab assignment to implement multithreading for matrix operations using pthreads. It defines functions to add, subtract, multiply and divide two 2x2 matrices in separate threads. The main function gets the matrix elements as input, creates threads to perform the operations, and displays the results.

Uploaded by

Vedang Bhange
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Assignment No: 4

Problem statement Topic: Implement multithreading for Matrix Operations using pthreads.

NAME: Vedang U Bhange ROLLNO: 8 Subject: OS

CLASS: FYMCA BRANCH: MCA BATCH: B1

Problem Statements:
Implement multithreading for Matrix Operations using pthreads.

Code and Outputs :-

//Matrix operations using pthreads


#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
#define MAX 2
int matrix1[MAX][MAX];
int matrix2[MAX][MAX];
int add[MAX][MAX];
int sub[MAX][MAX];
int mul[MAX][MAX];
int divv[MAX][MAX];
int step_i = 0;
pthread_t threads[MAX];
/***************/
void* addt(void* arg)
{
int i = step_i++; //i denotes row number of resultant matC

for (int j = 0; j < MAX; j++)


for (int k = 0; k < MAX; k++)
add[j][k] = matrix1[j][k] + matrix2[j][k];
}
void* subt(void* arg)
{
for (int j = 0; j < MAX; j++)
for (int k = 0; k < MAX; k++)
sub[j][k] = matrix1[j][k] - matrix2[j][k];
}
void* multi(void* arg)
{
for (int j = 0; j < MAX; j++)
for (int k = 0; k < MAX; k++)
mul[j][k] = matrix1[j][k] * matrix2[j][k];

}
void* divide(void* arg)
{
for (int j = 0; j < MAX; j++)
for (int k = 0; k < MAX; k++)
divv[j][k] = (matrix2[j][k]) / (matrix1[j][k]);

void disp() {
int i,j;
printf("Matrix A\n");
for (i = 0; i < MAX; i++){
for(j = 0; j < MAX; j++)
printf("%d ",matrix1[i][j]);
printf("\n");
}
printf("Matrix B\n");
for (i = 0; i < MAX; i++){
for(j = 0; j < MAX; j++)
printf("%d ",matrix2[i][j]);
printf("\n");
}
}

void display_result(){

printf("addtion of matrix1 and matrix2\n");


for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++){
printf("%d ",add[i][j]);}
printf("\n");

printf("Subtraction of matrix1 and matrix2\n");


for(int i = 0; i < MAX; i++) {
for(int j = 0; j < MAX; j++){
printf("%d ",sub[i][j]);
}
printf("\n");
}
printf("Multiplication of matrix1 and matrix2\n");
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++){

printf("%d ",mul[i][j]);
}
printf("\n");
}

printf("Division of matrix1 and matrix2\n");


for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++){

printf("%d ",divv[i][j]);
}
printf("\n");
}
}

int main()
{
int i,j,k;
printf("enter the first matrix element=\n");
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
{
scanf("%d",&matrix1[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
{
scanf("%d",&matrix2[i][j]);
}
}
disp();

for (int i = 0; i < MAX; i++) {


int* p;
pthread_create(&threads[i], NULL, addt, (void*)(p));
pthread_create(&threads[i], NULL, subt, (void*)(p));
pthread_create(&threads[i], NULL, multi, (void*)(p));
pthread_create(&threads[i], NULL, divide, (void*)(p));
}
for (int i = 0; i < MAX; i++){
pthread_join(threads[i], NULL);}
display_result();

return 0;
}

OUTPUT:

You might also like