0

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..

  • 1
    You will need to communicate between your processes using sockets. You might find it easier to use the p_thread library and make it threaded. – Fantastic Mr Fox May 30 '14 at 06:14
  • Modifying a variable in one process doesn't modify similarly named variables in other processes! – ikegami May 30 '14 at 06:16
  • You are changing `c` and `z` in the memory of the child processes. They don't affect the data in the parent process. – R Sahu May 30 '14 at 06:17
  • So what can I do now? as I have to use basic things here like fork(), wait(), exec(), any suggestions..? – Mobile Developer iOS Android May 30 '14 at 06:21
  • There is no reasonable way to do this with the functions that you've mentioned. Is this for a homework assignment? If so, is it possible that there are some other functions being discussed that you've forgotten about? –  May 30 '14 at 06:28

2 Answers2

5

You can't just simply use variables in between processes like that. Each process has different copies of these matrices you are creating and they are not sharing same memory space. You will have to use some IPC (Inter Process Communication) mechanisms to communicate between your processes.

I guess in your case shared memory will be the preferable choice — see Most efficient matrix multiplication in C using fork() and IPC

So if it's not a homework and you are trying to parallelize this multiplication for some application. I would recommend using threads instead of process as threads share same memory space. See What resources are shared between threads? and What is the difference between a process and a thread?

Community
  • 1
  • 1
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
0

When you do fork(), you create an exact copy of the parent process called the child, the only difference is the PID. The child process should be seen as a totally different program running on your computer with it's own memory space that is NOT shared with the parent process. So if you change something on the child, the parent (or brothers) won't be able to see those changes.

I don't know about arrays but you can always exit the child processes with the exit code being the result of the calculation and view the result on the parent with using something like:

wait(&status);
printf("The result is: %d\n",WEXITSTATUS(status));

But the best way to communicate the results with the parent is with a pipe since it's a simple message between a group of processes that originated from a single parent. Sockets are a bit too overkill for this.

And as Abhishek Bansal and Jonathan Leffler wrote, you can also use threads. Threads of the same process share their memory space but the way to use them is a bit different compared to processes.

goncalo luis
  • 193
  • 1
  • 6