Professional Documents
Culture Documents
Yash Pendal - PP - Practical - 3
Yash Pendal - PP - Practical - 3
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
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;
// 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);
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: -
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