I have four matrices and I want to add them using fork. I have to add first two matrices using fork and then other two using other fork. Then I want to add the result of above two matrix addition to get the desired output.
I am using the following code but not getting the correct answer in res matrix,
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[2][2] = {1,2,
4,5};
int b[2][2] = {1,2,
3,4};
int x[2][2] = {2,4,
3,6};
int y[2][2] = {4,6,
2,1};
int c[2][2];
int z[2][2];
int res[2][2];
int i,j;
int pid,pid2; //fork
pid = fork();
if(pid==-1)
{
printf("Can't fork\n");
}
if(pid==0)//child
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j] = a[i][j]+b[i][j];
printf("cccc: %d\n", c[i][j]);
}
}
exit(EXIT_SUCCESS);
}
if(pid>0)//parent
{
wait(0);
}
pid2=fork();
if(pid2==-1)
{
printf("Can't fork\n");
}
if(pid2==0)//child
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
z[i][j] = x[i][j]+y[i][j];
printf("zzzz: %d\n", z[i][j]);
}
}
exit(EXIT_SUCCESS);
}
if(pid2>0)//parent
{
wait(0);
}
printf("Result:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
res[i][j] = c[i][j]+z[i][j];
printf("%d\t", res[i][j]);
}
printf("\n");
}
}
Any help plzz..