-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab71.c
86 lines (73 loc) · 1.29 KB
/
lab71.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
int a[30][30];
int b[30][30];
int c[30][30];
int rowm,colm;
void * matrix(void *rowq)
{
int row =(int)rowq;
int i;
for(i=0;i<colm;++i)
{
c[row][i]=a[row][i]+b[row][i];
}
printf("row %d added by %d\n",row,(unsigned)pthread_self());
}
void main(int argc,char *argv[])
{
printf("enter no.of rows and colms:");
scanf("%d %d",&rowm,&colm);
int l,m;
for(l=0;l<rowm;l++)
{
for(m=0;m<colm;m++)
{
a[l][m]=0;
b[l][m]=1;
}
}
printf("matrix A:\n");
for(l=0;l<rowm;l++)
{
for(m=0;m<colm;m++)
{
printf("%-5d",a[l][m]);
}
printf("\n");
}
printf("matrix B:\n");
for(l=0;l<rowm;l++)
{
for(m=0;m<colm;m++)
{
printf("%-5d",b[l][m]);
}
printf("\n");
}
pthread_t thread[rowm];
int t,re;
for(t=0;t<rowm;++t)
{
printf("creating->%d\n",t);
re=pthread_create(&thread[t],NULL,matrix,(void *)t);
if(re)
{
printf("error");
exit(-1);
}
}
for(l=0;l<rowm;++l)
pthread_join(thread[l],NULL);
printf("main executing: \nmatrix is:\n");
for(l=0;l<rowm;l++)
{
for(m=0;m<colm;m++)
{
printf("%-5d",c[l][m]);
}
printf("\n");
}
pthread_exit(0);
}