Repository navigation
Expand file tree
/
Copy pathMatrixLogicalMultiplication.c
More file actions
162 lines (143 loc) · 4.11 KB
/
Copy pathMatrixLogicalMultiplication.c
File metadata and controls
162 lines (143 loc) · 4.11 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#define LC 8 // pois a matriz tem de ser quadrada
#define NP 4 // numero de processos
#define RND 100 // numeros randomicos da matriz
/*-----------------------------------------------*/
void gera_mat(int *mat)
{
int l,c;
for (l=0;l<LC;l++)
{
for (c=0;c<LC;c++)
{
mat[(l*LC)+c] = rand()%RND;
}
}
}
/*-----------------------------------------------*/
void multi_mat(int *mat1, int *mat2, int *nmat,int id_seq){
int lo,co,contmin,contmax,min, max,idp;
int vetmin[LC];
idp = id_seq;
for (lo=idp;lo<LC;lo=(lo+NP))
{
for(co=0;co<LC;co++)
{
//------------------separação interno externo---------------//
for (contmin=0;contmin<LC;contmin++)
{
if (mat1[lo*LC+contmin]> mat2[contmin*LC+co])
// 0 0 0 0
{
vetmin[contmin] = mat2[contmin*LC+co];
}
else
{
vetmin[contmin] = mat1[lo*LC+contmin];
}
}
max = vetmin[0];
for(contmax = 1;contmax<LC;contmax++)
{
if(vetmin[contmax]>max)
{
max = vetmin[contmax];
}
}
//------------------separação interno externo---------------//
nmat[(lo*LC)+co] = max;
/* Tentativa 1
for (li=0;li<LC;li++)
{
for (ci=0;ci<LC;ci++)
{
if (mat1[(li*LC)+ci] > mat2[(ci*LC)+li])
{
min = mat2[(ci*LC)+li];
}
else
{
min = mat1[(li*LC)+ci];
}
vetmin[ci] = min;
}
if (min>max)
{
max = min;
}
}
//------------------separação interno externo---------------//
nmat[(lo*LC)+co] = max;*/
}
}
}
/*-----------------------------------------------*/
void escreve_mat(int *mat)
{
int l,c;
srand(time(NULL));
for (l=0;l<LC;l++)
{
for (c=0;c<LC;c++)
{
if((mat[(l*LC)+c])>=10)
{
printf("%d ", mat[(l*LC)+c]);
}
else
{
printf(" %d ", mat[(l*LC)+c]);
}
}
printf("\n");
}
}
/*-----------------------------------------------*/
int main(){
int *mat1=NULL,*mat2=NULL, *nmat=NULL;
int pid, id_seq;
int i, shmid, chave = 3;
srand(time(NULL));
mat1 = (int *)malloc((LC*LC)*sizeof(int));
mat2 = (int *)malloc((LC*LC)*sizeof(int));
shmid = shmget(chave, (LC*LC)*sizeof(int), 0600 | IPC_CREAT);
nmat = (int *)shmat(shmid, 0, 0);
gera_mat(mat1);
gera_mat(mat2);
id_seq = 0;
for(i=1;i<NP;i++)
{
pid = fork();
if ( pid == 0)
{
id_seq = i;
break;
}
}
multi_mat(mat1,mat2,nmat,id_seq);
if (id_seq != 0){
shmdt(nmat);
}
else {
for(i=1;i<NP;i++){
wait(NULL);
}
printf("Matriz 1--------------------------------------------------------------------------------------------------------------------------------------\n");
escreve_mat(mat1);
printf("Matriz 2--------------------------------------------------------------------------------------------------------------------------------------\n");
escreve_mat(mat2);
printf("MAtriz R--------------------------------------------------------------------------------------------------------------------------------------\n");
escreve_mat(nmat);
shmdt(nmat);
shmctl(shmid, IPC_RMID, 0);
}
free(mat1);
free(mat2);
}
/*-----------------------------------------------*/