Multiple children and single pipe


 
Thread Tools Search this Thread
Top Forums Programming Multiple children and single pipe
# 1  
Old 11-07-2011
Multiple children and single pipe

Greetings everyone, I need a bit of help in solving the following problem:
I'm given an array of numbers and I have to compute the sum of the array elements using n processes, and the inter process communication has to be done with pipes(one pipe, to be exact).

I managed to solve the problem by waiting for a random child to finish, reading the sum sent by the child through the pipe, and so on for the following children, and incrementing the final sum.

Now I have to solve it by reading the partial sums in one fell swoop, after all the children have finished, and I'm not sure as to how to save the entire information sent by the kids. I tried to use a for statement, but it failed, since all the information would be passed in one single element of the array, and offering only the address of the first element in the array failed as well.

Any input would be much appreciated.

Here is the code that's bugging me:

Code:
#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
#include <stdlib.h> 
 
 
 
void main () { 
 
        int size, fd [2]; 
        printf ("enter the size of the number array: "); 
        scanf ("%d", &size);
        int i, arr [size];
        for(i=0; i < size; i++)
           arr [i] = i + 1;
         
 
        int nr_proc; 
        printf ("enter the number of processes that you want to use: "); 
        scanf ("%d", &nr_proc); 
        pid_t pid [nr_proc];
         
        for (i = 0; i < nr_proc; i++) { 
           pipe (fd); 
           pid [i] = fork (); 
           if (pid [i] < 0) 
              printf ("error in fork.\n"); 
           if (pid [i] == 0) { 
              close (fd [0]); 
              int j = 0, sum = 0; 
              for (j = i * size/nr_proc; j < (i+1) * size/nr_proc; j++) { 
                sum += arr[j]; 
              } 
              printf ("test %d\n", sum); 
              write(fd [1], &sum, sizeof (int)); 
              exit (1); 
           }
        } 
        close (fd [1]);
        for (i = 0; i < nr_proc; i++)
           wait ();
        int aux[nr_proc], test = 0;
        read (fd [0], aux, sizeof(aux));  // this is where i need help
        printf ("test aux %d\n", aux [1]);
        int sum = 0;
        for (i = 0; i < nr_proc; i++) 
           sum += aux [i];
        printf ("result = %d\n", sum);
              
}

# 2  
Old 11-07-2011
Your description of what needs to be done isnt very clear so start off by explaing your problem clearly...
# 3  
Old 11-07-2011
Sorry about that, I'm not a native english speaker. I'll try to give an example:

suppose we have this array:
arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
I want to compute the sum of it's elements using 2 child processes, to do so we'll split the array in two equal(or almost equal, if the number of elements is an odd number), and the first child will compute:
(1+2+3+4+5 = 15)
while the second will do:
(6+7+8+9+10 = 40).

Now, each child will send their computed sum to the parent through the pipe fd(2). The problem I'm having is that I don't know how to save both sums at the same time, that is, if I try to read the sums after all kids have finished, for example
int buffer[2];
read (fd (0), buffer, sizeof (buffer));
will first assign 15 to buffer [0], and then he'll assign 40 to buffer [0] , and i want it to assign 15 to buffer [0] and 40 to buffer [1]
# 4  
Old 11-07-2011
Quote:
Originally Posted by ephesos
Sorry about that, I'm not a native english speaker. I'll try to give an example:

suppose we have this array:
arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
I want to compute the sum of it's elements using 2 child processes, to do so we'll split the array in two equal(or almost equal, if the number of elements is an odd number), and the first child will compute:
(1+2+3+4+5 = 15)
while the second will do:
(6+7+8+9+10 = 40).

Now, each child will send their computed sum to the parent through the pipe fd(2). The problem I'm having is that I don't know how to save both sums at the same time, that is, if I try to read the sums after all kids have finished, for example
int buffer[2];
read (fd (0), buffer, sizeof (buffer));
will first assign 15 to buffer [0], and then he'll assign 40 to buffer [0] , and i want it to assign 15 to buffer [0] and 40 to buffer [1]
Then how about creating 2 pipes...this way one child's output is read into buffer[0] and the other child's output is read into buffer[1].
This User Gave Thanks to shamrock For This Post:
# 5  
Old 11-07-2011
Quote:
Originally Posted by ephesos
Now, each child will send their computed sum to the parent through the pipe fd(2). The problem I'm having is that I don't know how to save both sums at the same time, that is, if I try to read the sums after all kids have finished, for example
int buffer[2];
read (fd (0), buffer, sizeof (buffer));
will first assign 15 to buffer [0], and then he'll assign 40 to buffer [0] , and i want it to assign 15 to buffer [0] and 40 to buffer [1]
That doesn't happen, the child doesn't magically cause read() to rewind and overwrite array index 0. They don't control that.

Check how many bytes you actually read. You may have to read more than once.

You're not guaranteed to get them in order, either.

You're not even guaranteed to not get one number written inside another, unless you do advisory locking on the pipe in your children.
# 6  
Old 11-07-2011
Corona, that's my exact problem, I'm not allowed to make multiple reads this time, I already solved it that way by waiting for a child, reading the pipe, repeat Smilie . I have to figure out a mechanism that allows me to read everything in the pipe at once, and store it, been googling for pipe I/O for a few hours now to no avail Smilie.
# 7  
Old 11-07-2011
Quote:
Originally Posted by ephesos
Corona, that's my exact problem, I'm not allowed to make multiple reads this time
How do you know you didn't read enough data? You don't even check what value read() returns.

You're closing all the write-ends and waiting for all the processes to return first, so conditions should be ideal.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

One parent, multiple children pipe with fork()

The task I have to do is something along the lines "I receive some input and based on the first character I send it through pipe to one of the children to print". The scheme it is based on is 1->2; 1->3; 1->4; 2 will print all the input that starts with a letter, 3 will print all the input that... (2 Replies)
Discussion started by: Ildiko
2 Replies

2. Shell Programming and Scripting

Single command pipe

Single command to ls all the files inside a particular directory hierachy and output this to a file and open this in a vim file so that i can use gf command in vim to browse through all the files inside this hierachy. eg : dir1/dir2 and dir1/dir3 dir2 and dir3 contain the files i need... (7 Replies)
Discussion started by: dll_fpga
7 Replies

3. UNIX for Dummies Questions & Answers

Using multiple pipe output

I have a script that finds all sffs and extracts them into .fastq file types. What I need to do is change the .fastq to .fasta using the below script. How can I change the input.fastq and output.fasta to mirror the file's name? Would I use an array and use the default iterator? #!/bin/bash ... (3 Replies)
Discussion started by: jrymer
3 Replies

4. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. UNIX Desktop Questions & Answers

How to squeeze multiple pipe character '|' into single '|' using sed?

Hi, I am trying to convert multiple Unix pipe symbol or bar into single |. I have tried with the following sed statements, but, no success :(. I need it using sed only echo "sed 's/\|\+/\|/g' sed 's/*/\|/' sed 's/\|*/|/' sed -r 's/\|+/\|/' However, the below awk code is working fine.... (4 Replies)
Discussion started by: royalibrahim
4 Replies

6. Programming

Communicate with multiple process using named pipe

how to read and write on pipes to communicate with each other? (5 Replies)
Discussion started by: nimesh
5 Replies

7. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

8. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

9. Programming

pipe read and write with many forked children

I know how to read and write if i have a forked process with only one child. However what is involved with reading and writing with many forked processes. Say one parent that forks 5 children, and needs to communicate with all 5 in half duplex. int temp, counter=0; do{ pipe(temp); ... (5 Replies)
Discussion started by: steveneliuk
5 Replies

10. Programming

How to use pipe() in multiple threads?

Hi, I have a program that runs two threads in stead of two processes. I want to use pipe to redirect the output of the first thread to the input of the second thread. One thread is continuously writing to a pipe, and the other thread will read from the pipe. How do I do that? Is there... (2 Replies)
Discussion started by: wminghao
2 Replies
Login or Register to Ask a Question