I have an assignment to do a matrix multiplication with forks, using shared memory, then compare time results with the multiplication without forks, so here is the multiplication without them:
int matrizA[Am][An];
int matrizB[An][Bp];
//here i fill the matrix from a .txt file
int matrizR[Am][Bp];
int a,b,c;
for (a=0; a < Am; a++){
for (b = 0; b < Bp; b++)
{
matrizR[a][b] = 0;
for (c=0; c<An; c++){
matrizR[a][b] += matrizA[a][c] * matrizB[c][b];
}
}
}
Then i try to implement forks but the results are wrong, im not sure if I have to implement shared memory and where, are matrizA, matrizB, and matrizR2 should be shared? how i do this?
int matrizR2[Am][Bp];
pid_t pids[Am][Bp];
int h,j;
/* Start children. */TY
for (h = 0; h < Am; ++h) {
for (j=0; j<Bp ; ++j){
if ((pids[h][j] = fork()) < 0) {
perror("fork");
abort();
} else if (pids[h][j] == 0) {
matrizR2[h][j] = 0;
for (c=0; c<An; c++){
matrizR2[h][j] += matrizA[h][c] * matrizB[c][j];
}
printf("im fork %d,%d\n",h,j);
exit(0);
}
}
}
/* Wait for children to exit. */
int status;
pid_t pid;
while (n > 0) {
pid = wait(&status);
--n;
}