0% found this document useful (0 votes)
9 views4 pages

Lab6 Assignment Os Lab-1

The document contains an OS lab assignment that involves creating two programs for matrix addition using threads in C. The first program creates 'm' threads, each computing a row of the result matrix, while the second program modifies it to create 'm*n' threads, with each thread computing a single element of the result matrix. Both programs include code snippets and instructions for inputting matrix dimensions and displaying the results.

Uploaded by

kali2023sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Lab6 Assignment Os Lab-1

The document contains an OS lab assignment that involves creating two programs for matrix addition using threads in C. The first program creates 'm' threads, each computing a row of the result matrix, while the second program modifies it to create 'm*n' threads, with each thread computing a single element of the result matrix. Both programs include code snippets and instructions for inputting matrix dimensions and displaying the results.

Uploaded by

kali2023sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

OS lab Assignment 6

Name -Kalicharan Chinmay Sahoo


Roll No -122CS0103

Q1)Input and add two (mxn) matrices. Main thread creates m child threads. Thread1
computes the 1st row, Thread2 computes the 2nd row, …, Threadm computes the mth row
elements of the result matrix.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_ROWS 100


#define MAX_COLS 100

typedef struct {
int row;
int m;
int n;
int (*arr1)[MAX_COLS];
int (*arr2)[MAX_COLS];
int (*ans)[MAX_COLS];
} ThreadData;

void *thread_function(void *args) {


ThreadData *data = (ThreadData *)args;
int row = data->row;
for (int j = 0; j < data->n; j++) {
data->ans[row][j] = data->arr1[row][j] + data->arr2[row][j];
}
return NULL;
}

int main() {
int m, n;

printf("Enter the number of rows: ");


scanf("%d", &m);
printf("Enter the number of cols: ");
scanf("%d", &n);

int arr1[m][n];
int arr2[m][n];
int ans[m][n];

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


for (int j = 0; j < n; j++) {
arr1[i][j] = i;
arr2[i][j] = j;
}
}

ThreadData threadData[m];

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


threadData[i].row = i;
threadData[i].m = m;
threadData[i].n = n;
threadData[i].arr1 = arr1;
threadData[i].arr2 = arr2;
threadData[i].ans = ans;
pthread_t threads;
pthread_create(&threads[i], NULL, thread_function, &threadData[i]);
}

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


pthread_join(threads[i], NULL);
}

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


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

return 0;
}

Output
Q2) Modify the program to create m*n threads.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_ROWS 100


#define MAX_COLS 100

typedef struct {
int row;
int col;
int m;
int n;
int (*arr1)[MAX_COLS];
int (*arr2)[MAX_COLS];
int (*ans)[MAX_COLS];
} ThreadData;

void *thread_function(void *args) {


ThreadData *data = (ThreadData *)args;
int row = data->row;
int col=data->col;
data->ans[row][col] = data->arr1[row][col] + data->arr2[row][col];
return ;

int main() {
int m, n;

printf("Enter the number of rows: ");


scanf("%d", &m);
printf("Enter the number of cols: ");
scanf("%d", &n);

int arr1[m][n];
int arr2[m][n];
int ans[m][n];

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


for (int j = 0; j < n; j++) {
arr1[i][j] = i;
arr2[i][j] = j;
}
}
ThreadData threadData[m];

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


threadData[i].row = i;
threadData[i].m = m;
threadData[i].n = n;
threadData[i].arr1 = arr1;
threadData[i].arr2 = arr2;
threadData[i].ans = ans;
for(int j=0;j<n;j++){
threadData[i].col=j;
// Creating the threads m * n times
pthread_t threads;
pthread_create(&threads, NULL, thread_function, &threadData[i]);
pthread_join(threads, NULL);
}
}

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


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

return 0;
}

Output

You might also like