0
I need help to perform a sum of matrices using shared memory.
#define LINHAS 3
#define COLUNAS 3
#define PULAR_LINHA printf("\n")
int main(int argc, char *argv[])
{
int linha, coluna; //indices
int pid, id;
int matriz1[][COLUNAS] = { {1,2,3}, {4,5,6}, {7,8,9} };
int matriz2[][COLUNAS] = { {3,5,3}, {4,5,6}, {7,8,9} };
int segmento, status;
segmento = shmget(IPC_PRIVATE, sizeof(int)*18, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
int *matrizSolucao = (int*)shmat(segmento, NULL, 0);
id = fork();
if (id == 0) // Processo filho
{
for(linha = 0; linha < LINHAS; linha++)
{
for(coluna = 0; coluna < COLUNAS; coluna++)
printf("%d\t", *matrizSolucao);
printf("\n");
}
}
else // Processo pai
{
pid = wait(&status);
for(linha = 0; linha < LINHAS; linha++)
{
for(coluna = 0; coluna < COLUNAS; coluna++)
*matrizSolucao = matriz1[linha][coluna] + matriz2[linha][coluna];
}
}
//Libera a mmoria compartilhada do processo
shmdt(matrizSolucao);
//Libera a memoria compartilhada
shmctl(segmento, IPC_RMID, 0);
return EXIT_SUCCESS;
}
With this code the matrix actually appears, but with all its values zeroed, can someone help? Thank you.
Thanks for the information, I reversed the code of the parent and child process, but now instead of zeros is printing only 18, I checked that it is only doing the sum of the last positions and putting this result in the whole matrix, which can be?
– Vitor Ghiraldelli
the instruction
*matrizSolucao = matriz1[linha][coluna] + matriz2[linha][coluna];
always write in the same space.– pmg