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