Multiplying 2D arrays using fork()


 
Thread Tools Search this Thread
Top Forums Programming Multiplying 2D arrays using fork()
# 1  
Old 03-13-2012
Multiplying 2D arrays using fork()

HI,
i am trying to multiply 2 2D arrays (a[2][3],b[3][4]) using fork.
The answer will be at c[2][4].
Each child have to calculate 1 row of c[2][4].
The code is right, as i think of it, with no errors but i dont get the correct c array...
I think there is maybe a mistake in i dimension ...
Anyway, here is the code:
Code:
//multiply a[2][3] * b[3][4] = c[2][4]
//with fork
//each child completes one c[2][4]'s row

#include<stdio.h>
#include<stdlib.h>

main()
{
  int a[2][3]={1,2,3, 
	       4,5,6};
  int b[3][4]={1,2,3,4,
	       5,6,7,8,
	       9,10,11,12};
  int c[2][4];
  int i,j;
  int m;	//for the calculation
  int n;	//common 3
  int pid;	//fork
  
  for(i=0;i<2;i++)
  {
    pid=fork();
    if(pid==-1)
    {
      printf("Can't fork\n");
    }
    if(pid==0)//child
    {
      for(j=0;j<4;j++)
      {
	for(n=0;n<3;n++)
	{
	  m=m+a[i][n]*b[n][j];
	}
	c[i][j]=m;
	m=0;
      }
      exit(EXIT_SUCCESS);
    }
    if(pid>0)//parent
    {
      wait(0);
    }
  }
  printf("C:\n");
  for(i=0;i<2;i++)
  {
    for(j=0;j<4;j++)
    {
      printf("%d\t",c[i][j]);
    }
    printf("\n");
  }	       
}

Any help, please ?
# 2  
Old 03-13-2012
First, move your initialisation of m to the start of the loop:


Code:
for(j=0;j<4;j++)
{    
    m = 0;
    for(n=0;n<3;n++)
    {
        m=m+a[i][n]*b[n][j];
    }   
    c[i][j]=m;
}

That would only affect your [0][0] value though.


The reason you're not seeing the result you expect is that you're not passing back the computed value from the child to the parent. With fork(), the child receives a copy of the process, but any manipulations to the data segment are local to the child. If you memset() your result array to -99 or something, you'll notice that it's still -99 at the end.


To pass back values from the child, have a look at creating a pipe that the child can write to and the parent can read from. See the man(2) page for pipe which will have all the details that you'll need.
This User Gave Thanks to agama For This Post:
# 3  
Old 03-13-2012
Are you expecting child processes storing numbers in their copy of array c to update array c in the parent?
# 4  
Old 03-13-2012
Quote:
Originally Posted by Skaperen
Are you expecting child processes storing numbers in their copy of array c to update array c in the parent?
I didnt knew that child processes use a copy of the variables i use. i should re-check the man fork(). Yes i want to update array c in the parent. Looking pipes ...

---------- Post updated at 07:01 PM ---------- Previous update was at 07:00 PM ----------

Quote:
Originally Posted by agama
The reason you're not seeing the result you expect is that you're not passing back the computed value from the child to the parent. With fork(), the child receives a copy of the process, but any manipulations to the data segment are local to the child. If you memset() your result array to -99 or something, you'll notice that it's still -99 at the end.


To pass back values from the child, have a look at creating a pipe that the child can write to and the parent can read from. See the man(2) page for pipe which will have all the details that you'll need.
Initiliazation corrected. Just looking for the use of pipes...

---------- Post updated at 08:40 PM ---------- Previous update was at 07:01 PM ----------

Ok, i read about pipe.
Trying to figure out how it works, i made an easy example.
The program makes a multiply of 2 integers (a and b) and saves the answer to c...
Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

main()
{
  int a,b,c;
  int pid;
  int pipefd[2];
  char buff[10];
  char str[100+1]={'\0'};
  int w,r;
  a=3;
  b=10;
  
  pipe(pipefd);
    if(pipefd<0)
      printf("pipe error\n");
    
  pid=fork();
  if(pid==-1)
    printf("Fork error\n");
  if(pid==0)
  {
    //child
    c=a*b;
    printf("child says answer %d\n",c);
    
    sprintf(str,"%d",c);
    w=write(pipefd[1],str,c);
    if(w!=c)
      printf("write error");
    exit(0);
  }
  if(pid>0)
  {
    //parent
    printf("father says answer %d\n",c);
    wait(0);
    
    r=read(pipefd[0], buff, sizeof(buff));
    if(r<=0)
      printf("read error\n");
    c=atoi(buff);
    printf("child told father answer is %d\n",c);
    
  } 
}

It works with no errors or notifications... Smilie
But when i tried with the arrays, troubles appeared again...
Any help?
Based on my first code and adding pipes i have this :
Code:
//multiply a[2][3] * b[3][4] = c[2][4]
//with fork
//each child completes one c[2][4]'s row

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

main()
{
  int a[2][3]={1,2,3, 
	       4,5,6};
  int b[3][4]={1,2,3,4,
	       5,6,7,8,
	       9,10,11,12};
  int c[2][4];
  int i,j;
  int m[4];	//for the calculation. Array is just an idea, dont know if works
  int n;	//common 3
  int pid;	//fork
  int pipefd[2];
  int w,r;    //for write() and read()
  char buff[100];
  char str[100+1]={'\0'};
   
  for(i=0;i<2;i++)
  {
    pipe(pipefd);
      if(pipefd<0)
	printf("Pipe error\n");
    pid=fork();
    if(pid==-1)
    {
      printf("Can't fork\n");
    }
    if(pid==0)//child
    {
      for(j=0;j<4;j++)
      {
	//m initialize
	for(n=0;n<3;n++)
	{
	  m[j]=m[j]+a[i][n]*b[n][j];
	}
      }
      //what !?
      sprintf(str,"%d",m[j]);
      w=write(pipefd[1],"%d",m);
      if(w!=m)
	printf("write error\n");
      //m=0;
      exit(0);
    }
    if(pid>0)//parent
    {
      wait(0);
      r=read(pipefd[0], buff,sizeof(buff));
      if(r<=0)
	printf("read error\n");
    }
  }
  printf("C:\n");
  for(i=0;i<2;i++)
  {
    for(j=0;j<4;j++)
    {
      printf("%d\t",c[i][j]);
    }
    printf("\n");
  }	       
}

Of course, it doesnt works. Its unfinished. But i want help...
I will update it when i change something...
# 5  
Old 03-14-2012
Only have time for a quick look, but this is what jumped out at me:

Write is not like printf(), it takes a buffer of data and a length and doesn't do any formatting. You had the right idea in your first test (without arrays), but you need to give write the length -- you gave it 'c' which probably caused write to grab too many bytes depending on the value of c.

Code:
data_len = snprintf( str, sizeof( str ), "%d\n", m[j]);
write( pipe[1], str, data_len );

I added a new line -- you'll need some form of space between the data that the parent process will be reading.

Hope this gets you a bit further.
This User Gave Thanks to agama For This Post:
# 6  
Old 03-14-2012
There are many things wrong with the pipe and array code...not initalizing m[4] to 0 before using it in the addition and in the parent you are not reading the value into c[i][j] but instead putting it into buff.
This User Gave Thanks to shamrock For This Post:
# 7  
Old 03-14-2012
Looking for it and will reply back!
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiplying lines of file that contain certain letter by value

I am trying to remove the last letter in a file and then multiply each line (which contained this letter) by 500. This is what I have: 1499998A 1222222A 1325804A 1254556 1235 9998 777 cat /tmp/listzz |gawk '{print $4}'|gawk '{gsub(//, ""); print } This removes the A... (1 Reply)
Discussion started by: newbie2010
1 Replies

2. UNIX for Dummies Questions & Answers

Group sums by matching and then multiplying by weights

Hi Experts, Please help with the following. I have 3 columns in File 1 , variables with values nested within groups. File 1 gr1 var1 a gr1 var2 b gr1 var3 a gr1 var4 c gr2 var1 a gr2 var2 a gr2 var4 c gr3 var1 b gr3 var3 b gr3 var4 a gr3 var5 a (3 Replies)
Discussion started by: ritakadm
3 Replies

3. Shell Programming and Scripting

Creating a loop for multiplying columns

I have 2 files, that look like this: ID SNP1 SNP2 SNP3 SNP4 A1 1 2 0 2 A2 2 0 1 1 A3 0 2 NA 1 A4 1 1 0 2 and this: SNP score SNP1 0.5 SNP2 0.7 SNP3 0.8 SNP4 0.2 Basically, all of the SNP-values are 0,1, 2 or NA, and they each have a score, listed in the second file. The total... (5 Replies)
Discussion started by: kayakj
5 Replies

4. Shell Programming and Scripting

Multiplying array element

I am trying to take all the elements of an array and multiply them by 2, and then copy them to a new array. Here is what I have i=0 for true in DMGLIST do let DMGSIZES2="${DMGSIZES}"*2 let i++ done unset i echo ${DMGSIZES2} It does the calculation correctly for the first element,... (7 Replies)
Discussion started by: nextyoyoma
7 Replies

5. Programming

Multiplying column in awk and PHP

hello, I'm writing a php script in fedora to run with a csv file. I want the script to read column 4 and multiply each single line in the column by 1000, how would that script look? I've written one script but it's obviously incorrect because it will not execute the command. here is my... (4 Replies)
Discussion started by: brandonadam
4 Replies

6. Programming

problem in multiplying arrays

Hi, this is my code.It's simple : there are 2 2D arrays and the multiplied to C. #include<stdio.h> #include<sys/shm.h> #include<sys/stat.h> #include<stdlib.h> main() { int *A; //A int *B; //B int *C; //C int i,j,x,k,d; int id; ... (17 Replies)
Discussion started by: giampoul
17 Replies

7. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

8. Shell Programming and Scripting

multiplying values from two text files

Im very new to programming. But I would like to write a script which extracts and multiply values from 2 txt and output as a new file. Can someone please teach me how to write it? Thank you so much for example File A File B 1 34 1 2 2 13 2 2 3 8 3 3 File C output 1 68 2... (2 Replies)
Discussion started by: crunchichichi
2 Replies

9. Shell Programming and Scripting

Multiplying Floats/Decimals

Is there a way that i can get something like this to work: Number=`expr 80 \* 10.69` i.e. To multiply an integer by a decimal or a decimal by a decimal etc...? thanks (10 Replies)
Discussion started by: rleebife
10 Replies

10. Shell Programming and Scripting

Error only when multiplying two numbers

Hi $ a=10 ; b=2 $ expr $a + $b 12 $ expr $a - $b 8 $ expr $a / $b 5 $ expr $a * $b expr: syntax error Any idean why I am getting this error only when multiplying two numbers. Whats the exact syntax? Thanks a lot to all in advance CSaha (5 Replies)
Discussion started by: csaha
5 Replies
Login or Register to Ask a Question