0% found this document useful (0 votes)
15 views14 pages

Khushbu Os

The document contains 4 questions related to performing operations on arrays using multithreading in C programming. Q1 summarizes calculating the sum of elements in a 2D array by splitting the array across 4 threads. Q2 searches for a key element in a 1D array split across 4 threads. Q3 finds the maximum element in a 1D array by having each thread find the local maximum of a portion of the array. Q4 performs addition and subtraction of 2 matrices by splitting the operations across multiple threads.

Uploaded by

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

Khushbu Os

The document contains 4 questions related to performing operations on arrays using multithreading in C programming. Q1 summarizes calculating the sum of elements in a 2D array by splitting the array across 4 threads. Q2 searches for a key element in a 1D array split across 4 threads. Q3 finds the maximum element in a 1D array by having each thread find the local maximum of a portion of the array. Q4 performs addition and subtraction of 2 matrices by splitting the operations across multiple threads.

Uploaded by

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

Khushboo

20214294

Q1- To fint the sum an array using multithreading.

#include<pthread.h>
#include<stdio.h> #define N 16
int sum = 0;
void *sum_array(void * args)
{
int *val_p = (int * )args; for(int i = 0; i<4;i++)
{ sum += val_p[i];
} pthread_exit(0);
} int main()
{ pthread_t thread[4]; int arr[4][4]
= {{1,2,5,3},{4,5,6,7},{7,6,7,9},{1,2,3,4}};

for(int i = 0;i<4;i++)
{ pthread_create(&thread[i],NULL,sum_array,arr[i]
);
} for(int i = 0;i<4;i++)
{ pthread_join(thread[i],NULL);
}
printf("the sum of array using multithreadin g = %d \n",sum); return
0;
}

Output-1

Q2 . To search for an element in an array using multithreading.

#include <stdio.h>
#include <pthread.h>
#define max 16
#define thread_max 4

int a[max] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30,
64, 110, 220 }; int key = 220;

int f = 0;
int current_thread = 0;
void* ThreadSearch(void* args)
{ int num = current_thread++;
for (int i = num * (max / 4); i < ((num + 1) * (max / 4));
i++) { if (a[i] == key) printf("The key is %d \
n",a[i]); f = 1;
}
}

// Driver Code int main()


{ pthread_t thread[thread_max];
for (int i = 0; i < thread_max; i++) {
pthread_create(&thread[i], NULL,
ThreadSearch,
(void*)NULL);
}
for (int i = 0; i < thread_max; i++) { pthread_join(thread[i], NULL);
} if (f == 1) printf("Key element found \n");
else printf("Key not present\n"); return 0;
}

Output-2-

Q3- To find maximum element of an array using multithreading.

// C code to find maximum of an array using


Multithreading
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define max 16

#define Th_max 4
// Array int a[max] = {1, 5, 7, 10, 12, 14, 15, 18, 20,
22, 25, 27, 300, 64, 110, 220};

int max_num[Th_max] = {0}; int


thread_no = 0;
void maximum(void *arg)
{ int i, num = thread_no++; int maxs
= 0;
for (i = num * (max / 4); i < (num + 1) * (max
/ 4); i++) { if (a[i] > maxs)
maxs = a[i];
} max_num[num] = maxs;
} int main()
{ int maxs = 0; int i; pthread_t
threads[Th_max];
for (i = 0; i < Th_max; i++) pthread_create(&threads[i],
NULL, maximum, (void *)NULL);
for (i = 0; i < Th_max; i++)
pthread_join(threads[i], NULL);

for (i = 0; i < Th_max; i++)


{ if (max_num[i] > maxs)
maxs = max_num[i];
} printf("Maximum Element is : %d", maxs);
return 0;
}

Output-3-

Q-4

To perform addition and subtraction of matrices using multithreading.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define max 4 #define th 4
pthread_t thread[th * 2]; int mat_A[max][max],
mat_B[max][max], sum[max][max], sub[max][max];
void *addtion(void *args)
{ int core = (int)args; for (int i = core * max / 4; i < (core + 1) * max
/ 4; i++)
{ for (int j = 0; j < max; j++)
{ sum[i][j] = mat_A[i][j] + mat_B[i][j];
}
} } void *subtraction(void *args)
{ int core = (int)args; for (int i = core * max / 4; i < (core + 1) * max
/ 4; i++)
{ for (int j = 0; j < max; j++)
{ sub[i][j] = mat_A[i][j] - mat_B[i][j];
}
} } int
main()
{ int step = 0; printf("Enter the matrix A \n");
for (int i = 0; i < max; i++)
{ for (int j = 0; j < max; j++)
{
scanf("%d", &mat_A[i][j]);
}
} printf("Enter the martix B \n"); for (int i = 0;
i < max; i++)
{ for (int j = 0; j < max; j++)
{ scanf("%d", &mat_B[i][j]);
} } printf("Matrix A is \n"); for (int
i = 0; i < max; i++)
{ for (int j = 0; j < max; j++)
{ printf("%d ", mat_A[i][j]);
} printf("\n");
} printf("Matrix B is \n"); for (int i = 0; i
< max; i++)
{ for (int j = 0; j < max; j++)
{ printf("%d ", mat_B[i][j]);
} printf("\n");
}
// creating the thread
for (int i = 0; i < th; i++)
{ pthread_create(&thread[i], NULL, &addtion,
(void *)step); pthread_create(&thread[i], NULL,
&subtraction, (void *)step); step++;
} for (int i = 0; i < th * 2; i++)
{ pthread_join(thread[i], NULL);
} printf("Addtion of the matrix A and B is
\n"); for (int i = 0; i < max; i++)
{ for (int j = 0; j < max; j++)
{ printf("%d ", sum[i][j]);
} printf("\n");
} printf("\n");
printf("Subtracted matrix A and B is \n"); for (int i = 0; i < max;
i++)
{
for (int j = 0; j < max; j++)

{ printf("%d", sub[i][j]);
} printf("\n");
} return 0;
}

Output- 4 –

You might also like