Multiplying 2D arrays using fork()


 
Thread Tools Search this Thread
Top Forums Programming Multiplying 2D arrays using fork()
# 8  
Old 03-18-2012
Quote:
Originally Posted by shamrock
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.
I'm trying to correct the code. i have this now:
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
  int n;	//common 3
  int pid;	//fork
  int pipefd[2];
  int w,r;
  char buff[100];
  char str[100+1]={'\0'};
  int data_len;
  int temp;
   
  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++)
      {
	for(n=0;n<3;n++){m[n]=0;}	//initialize m[4]
	for(n=0;n<3;n++)
	{
	  m[j]=m[j]+a[i][n]*b[n][j];
	}
      }
      for(j=0;j<4;j++)
      {
	sprintf(str,"%d",m[j]);
	w=write(pipefd[1],str,data_len);
      }
      exit(0);
    }
    if(pid>0)//parent
    {
      wait(0);
      for(i=0;i<4;i++)
      {
	m[i]=read(pipefd[0], buff, sizeof(buff));
      }
    }
    
  }
  printf("C:\n");
  for(i=0;i<2;i++)
  {
    for(j=0;j<4;j++)
    {
      printf("%d\t",c[i][j]);
    }
    printf("\n");
  }	       
}

i'm confused about how to put the m array in each column of c...
By the way i get no compilng errors, but the program dont run...
I cant understand the use of snprintf() Smilie
# 9  
Old 03-18-2012
I've made changes to your code, and have a working copy. I'll post my comments so if you want to work the changes yourself you can; I can post the code if you'd rather me do that.

In no real order:

Your initialisation and loop to compute m[] in the child runs from i=0; i < 3 which isn't initialising m[3], nor computing the value for m[3].

The initialisation of m[] must go before for(j=0;j<4;j++) otherwise you reset the values of m to zero before you are ready to write them to the parent.

you should close the reading side of the pipe in the child before you start (good ptractice), and you should close the writing end before the child exits.

You should close the writing end of the pipe in the parent before calling wait(); again good practice.

When writing your results to str in prep to send to parent, you need to add a space to the value, "%d ", so that the values are delimited when the parent reads them.

You should write a final end of string to the parent, and you should close the writing side of the pipe before the child exits.

In the parent:

Code:
            for(i=0;i<4;i++)
            {
                m[i]=read(pipefd[0], buff, sizeof(buff));
            }


This just won't work. The read will read everything that the child has written on the first read -- provided that the buffer is large enough, and for this small sample programme it is. Just because the child wrote 4 times (+ one end of string write), doesn't mean that the parent will need to read 5 times. Also, you're assigning the return code from the read() and not the value to m[i], You need to do one read, check for error, and then use sscanf() to parse the buffer and fill in c[i][j].

The parent should close the reading end of the pipe before looping.


I think those were all of the changes I needed to make to get your code to work. Post back if you want me to post the actual code -- Not sure if you want the experience of figuring it out, or want to see the example, and will error on the side of letting you work through it.

Last edited by agama; 03-18-2012 at 08:20 PM.. Reason: typo
This User Gave Thanks to agama For This Post:
# 10  
Old 03-19-2012
I look your comments and tried to adjust my program. I think i correct these :
Quote:
Originally Posted by agama
Your initialisation and loop to compute m[] in the child runs from i=0; i < 3 which isn't initialising m[3], nor computing the value for m[3].

The initialisation of m[] must go before for(j=0;j<4;j++) otherwise you reset the values of m to zero before you are ready to write them to the parent.

When writing your results to str in prep to send to parent, you need to add a space to the value, "%d ", so that the values are delimited when the parent reads them.

In the parent:

Code:
            for(i=0;i<4;i++)
            {
                m[i]=read(pipefd[0], buff, sizeof(buff));
            }


This just won't work. The read will read everything that the child has written on the first read -- provided that the buffer is large enough, and for this small sample programme it is. Just because the child wrote 4 times (+ one end of string write), doesn't mean that the parent will need to read 5 times. Also, you're assigning the return code from the read() and not the value to m[i], You need to do one read, check for error, and then use sscanf() to parse the buffer and fill in c[i][j].

I think those were all of the changes I needed to make to get your code to work. Post back if you want me to post the actual code -- Not sure if you want the experience of figuring it out, or want to see the example, and will error on the side of letting you work through it.
The code is now :
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
  int n;	//common 3
  int pid;	//fork
  int pipefd[2];
  int w,r;
  char buff[100];
  char str[100+1]={'\0'};
  int temp;
   
  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(n=0;n<3;n++){m[n]=0;}	//initialize m[4]
      for(j=0;j<4;j++)
      {
	for(n=0;n<3;n++)
	{
	  temp=temp+a[i][n]*b[n][j];
	}
	m[j]=temp;
	sprintf(str,"%d ",m[j]);
	w=write(pipefd[1],str,sizeof(str));
	if(w!=sizeof(str))
	  printf("\nWrite error");
      }
      exit(0);
    }
    if(pid>0)//parent
    {
      wait(0);
      r=read(pipefd[0], buff, sizeof(buff));
      if(r<=0)
	printf("\nRead Error");
      for(n=0;n<3;n++)
      {
	sscanf(m[n],"%s",buff);	//or otherwise
	c[i][n]=m[n];	//give to c each time a row
      }
    }
    
  }
  printf("C:\n");
  for(i=0;i<2;i++)
  {
    for(j=0;j<4;j++)
    {
      printf("%d\t",c[i][j]);
    }
    printf("\n");
  }	       
}

I get a notice in sscanf in compiling :
Code:
example.c: In function ‘main’:
example.c:61:2: warning: passing argument 1 of ‘sscanf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:450:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’

and a Segmentation fault as an output.
So i'm working on pipes, to fix the other issues...
I want to learn how to use the pipes, thanks for your time and explanations. I will try more and if i give up, i'll tell you to send me the final code to study and work on it. Thanks!

Last edited by giampoul; 03-19-2012 at 04:15 PM..
# 11  
Old 03-20-2012
Quote:
Originally Posted by giampoul
I look your comments and tried to adjust my program. I think i correct these :

The code is now :
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] = {0};   // initialise c[2][4] items to zero and no need for m[4]
  int i,j;
  int n;	//common 3
  int pid;	//fork
  int pipefd[2];
  int w,r;
  char buff[100];
  char str[100+1]={'\0'};
  int temp;
   
  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(n=0;n<3;n++){m[n]=0;}	// this is wasteful...and not needed
      for(j=0;j<4;j++)
      {
	for(n=0;n<3;n++)
	{
	  temp=temp+a[i][n]*b[n][j];     // again temp is uninitialised...replace this statement with the ones below
      
         c[i][j] += a[i][n]*b[n][j];
         }
         sprintf(str,"%d ",c[i][j]);
         w=write(pipefd[1],str,sizeof(str));
         if(w!=sizeof(str))
	     printf("\nWrite error");

	}
	m[j]=temp;     // not needed
	
      }
      exit(0);
    }
    if(pid>0)//parent
    {
      wait(0);
      r=read(pipefd[0], buff, sizeof(buff));
      if(r<=0)
	printf("\nRead Error");
      for(n=0;n<3;n++)  // c is a 2x4 matrix so it should be n<4
      {
	sscanf(m[n],"%s",buff);	//or otherwise
	c[i][n]=m[n];	//give to c each time a row
      }
    }
    
  }
  printf("C:\n");
  for(i=0;i<2;i++)
  {
    for(j=0;j<4;j++)
    {
      printf("%d\t",c[i][j]);
    }
    printf("\n");
  }	       
}

See my comments in red above...it needs more fixes but that is left as an exercise for the op...
This User Gave Thanks to shamrock For This Post:
# 12  
Old 03-20-2012
Because your parent process waits for the child to exit, all data will be read into the buffer, and you'll need to pull all of the values out in a single sscanf() call.

Something like this:

Code:
          if( (data_len = read( pipefd[0], buff, sizeof( buff )  )) <= 0 )            /* mimimal error checking, get data from child */
          {
                fprintf( stderr, "read failed: %s\n", strerror( errno ) );
                exit( 1 );
          }

            sscanf( buff, "%d %d %d %d", &c[i][0], &c[i][1], &c[i][2], &c[i][3] );      /* scan the buffer, convert each to int and place in m[i] */



You'll also notice that the buffer that sscanf() is to read is the first parameter, followed by the format string, and then pointer(s) to the locations that are to be populated. In your case, you can populate all 4 with one scanf() call.
This User Gave Thanks to agama For This Post:
# 13  
Old 03-22-2012
First of all thanks for the help you giving to me.
I am new in programming. Actually i am studying in informatics. That's also the reason my code is a mess, i mean lining and everything ...
I tried to fix it...
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>
#include <errno.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] = { 0 };
  	int i, j;
  	int n;	//common 3
  	int pid;
  	int pipefd[2];
  	int w, data_len;
  	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++ )
      			{
				for( n=0;n<3;n++ )
				{
	  				c[i][j] += a[i][n]*b[n][j];
        			}
        			sprintf( str, "%d ", c[i][j] );
        			w = write( pipefd[1], str, sizeof (str) );
        			if( w != sizeof (str) )
	    				printf("\nWrite error");
			}
      			exit(0);
    		}
    		if( pid > 0 )//parent
    		{
      			wait(0);
      			if( ( data_len = read( pipefd[0], buff, sizeof ( buff ) ) ) <=0 )
      			{
				fprintf( stderr,"Read failed: %d\n", strerror( errno ) );	//%s gives me a warning so i checked man errno and 
				exit(1);							//change it to %d
      			}
			sscanf( buff, "%d %d %d %d", &c[i][0], &c[i][1], &c[i][2], &c[i][3]);
    		}
  	}
  	printf("C:\n");
  	for( i=0;i<2;i++ )
  	{
    		for( j=0;j<4;j++ )
    		{
      			printf("%d\t",c[i][j]);
    		}
    		printf("\n");
  	}	       
}

Almost done!
I get no Segmentation fault, no errors or warnings in compiling!
But C array has the right numbers only in c[0][0] and c[1][0], at others i get 0s.
So its like this:
Code:
C:
38	0	0	0	
83	0	0	0

Tryin' to fix that ...
# 14  
Old 03-23-2012
Great progress!

You need to correct a few things if you haven't.

First, the warning about %s in conjunction with strerror() is because you haven't included string.h. The strerror() function returns a pointer to a string, and if you don't include string.h the compiler assumes that it returns int, and thus issues the warning about the mismatch with %s. Include string.h and change the %d to a %s. It has nothing to do with the type of errno.

You also should include sys/types.h and sys/wait.h.

Your initialisation of a, b and c is missing curly braces. To be correct, you need something like this for a and b:
Code:
int a[2][3] = { {1,2,3},
                   {4,5,6} };

For c you need
Code:
int c[2][4] = { {0}, {0} };

Those are all to correct compiler warnings. The real problem is in your writing from the child. You are converting each value to string and placing it in str with a terminating NULL character. That is correct. However, you are writing the entire buffer, including the characters past the NULL to the parent. You need to write just the values, and the associated trailing blank, to the parent. To do this you need to capture the length of the string created by sprintf() and use that as the number of characters to write to the pipe. For completeness, I would also write a final NULL to the pipe after all of the values. Something like the code segment below:

Code:
                    len = sprintf( str, "%d ", c[i][j] );   // length of value+blank into len
                    w = write( pipefd[1], str, len  );     // write only the value and blank
                    if( w != len )                         // check the number written, not the size of str
                        printf("\nWrite error");        // maybe exit too?
            }
            *str = 0;                     // write a final NULL to terminate the string
            write( pipefd[1], str, 1 );
            exit(0);

This User Gave Thanks to agama For This Post:
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