You are on page 1of 9

18162101026 YASH PENDAL

ICT GANPAT UNIVERSITY


COMPUTER SCIENCE DEPARTMENT
Parallel Programming (2CSE60E5)
PRACTICAL – 3

AIM: - To Demonstrate thread management using POSIX Thread API.


1. For any complex problem / task, before task gets divided into number of chunks,
create the multithreaded environment that can create ‘n’ no of threads, terminate
all threads and execute any function of your choice.
Code: -
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{

long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0;t<NUM_THREADS;t++)
{
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void
*)t);
}
/* Last thing that main() should do */
pthread_exit(NULL);
}

1
18162101026 YASH PENDAL
Output: -

2. Parallel Program to find sum of array using each thread computes sum of 1/4th
of array.
Code: -
#include <stdio.h>
#include <pthread.h>

// size of array
#define MAX 16

// maximum number of threads


#define MAX_THREAD 4

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

void* sum_array(void* arg)


{

// Each thread computes sum of 1/4th of array


int thread_part = part++;
int i;

for (int i = thread_part * (MAX / 4); i < (thread_part + 1) * (MAX


/ 4); i++)
sum[thread_part] += a[i];
}

// Driver Code
2
18162101026 YASH PENDAL
int main()
{

pthread_t threads[MAX_THREAD];
int i;

// Creating 4 threads
for (int i = 0; i < MAX_THREAD; i++)
pthread_create(&threads[i], NULL, sum_array,
(void*)NULL);

// joining 4 threads i.e. waiting for all 4 threads to complete


for (int i = 0; i < MAX_THREAD; i++)
pthread_join(threads[i], NULL);

// adding sum of all 4 parts


int total_sum = 0;
for (int i = 0; i < MAX_THREAD; i++)
total_sum += sum[i];

printf("\n Sum is %d ", total_sum);

return 0;
}
Output: -

3
18162101026 YASH PENDAL
Task – 3
Code: -
#include<stdio.h>
#include<stdlib.h>
#include <time.h> // for clock_t, clock()
#include <unistd.h> // for sleep()
int main()
{
// to store execution time of code
double time_spent = 0.0;
clock_t begin = clock();
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
4
18162101026 YASH PENDAL
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
//sleep(3);
clock_t end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time taken is %f seconds", time_spent);
return 0;
}

Output: -

Time Taken: - 0.000548 Seconds

5
18162101026 YASH PENDAL
Task – 4
Code: -
# include <stdio.h>
# include <pthread.h>
#include <time.h> // for clock_t, clock()
#include <unistd.h> // for sleep()
int MAT1[10][10];
int MAT2[10][10];
int MAT3[10][10];
int r1,c1,r2,c2;
void *thread_Multiply_Matrix(void *);
int main()
{
// to store execution time of code
double time_spent = 0.0;
clock_t begin = clock();
pthread_t tid;
int iCount,jCount,kCount;
printf("Enter Number of Rows For Matrix 1 :");
scanf("%d",&r1);
printf("Enter Number of Columns For Matrix 1 :");
scanf("%d",&c1);
for(iCount=0;iCount<r1;iCount++)
{
for(jCount=0;jCount<c1;jCount++)
{
printf("Enter Mat1[%d][%d] :",iCount,jCount);
scanf("%d",&MAT1[iCount][jCount]);
}
}
printf("\n");
printf("Enter Numer of Rows For Matrix 2 :");
scanf("%d",&r2);
printf("Enter Number of Columns For Matrix 2 :");
scanf("%d",&c2);
for(iCount=0;iCount<r2;iCount++)
{
for(jCount=0;jCount<c2;jCount++)
{
printf("Enter Mat2[%d][%d] :",iCount,jCount);
scanf("%d",&MAT2[iCount][jCount]);
}
6
18162101026 YASH PENDAL
}
if(c1!=r2)
{
printf("Multipication of Matrix not Possible !!!");
}
else
{
for(iCount=0;iCount<r1;iCount=iCount+2)
{
for(jCount=0;jCount<c2;jCount=jCount+2)
{
MAT3[iCount][jCount]=0;
}
}
pthread_create(&tid,NULL,thread_Multiply_Matrix,NULL);

for(iCount=0;iCount<r1;iCount=iCount+2)
{
for(jCount=0;jCount<c2;jCount++)
{
for(kCount=0;kCount<c1;kCount++)
{
MAT3[iCount][jCount]+=MAT1[iCount][kCount] *
MAT2[kCount][jCount];
}
}
}
pthread_join(tid,NULL);
}
printf("\n Matrix 1 \n");
for(iCount=0;iCount<r1;iCount++)
{
for(jCount=0;jCount<c1;jCount++)
{
printf("%d \t",MAT1[iCount][jCount]);
}
printf("\n");
}
printf("\n Matrix 2 \n");
for(iCount=0;iCount<r2;iCount++)
{
for(jCount=0;jCount<c2;jCount++)
7
18162101026 YASH PENDAL
{
printf("%d \t",MAT2[iCount][jCount]);
}
printf("\n");
}
printf("\n Multipication of Matrix ...\n");
for(iCount=0;iCount<r1;iCount++)
{
for(jCount=0;jCount<c2;jCount++)
{
printf("%d \t",MAT3[iCount][jCount]);
}
printf("\n");
}
//sleep(3);
clock_t end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time taken is %f seconds", time_spent);
return 0;
}
void *thread_Multiply_Matrix(void *para)
{
int iCount,jCount,kCount;
for(iCount=1;iCount<r1;iCount=iCount+2)
{
for(jCount=0;jCount<c2;jCount++)
{
for(kCount=0;kCount<c1;kCount++)
{
MAT3[iCount][jCount]+=MAT1[iCount][kCount] *
MAT2[kCount][jCount];
}
}
}
printf("thread finished ...");
pthread_exit(NULL);
}

Output: -
Time Taken: - 0.000965 Seconds

8
18162101026 YASH PENDAL

Program Name Normal – Matmul Matmul


Time 0.000548 Seconds 0.000956 seconds

You might also like