Multiple children and single pipe


 
Thread Tools Search this Thread
Top Forums Programming Multiple children and single pipe
# 8  
Old 11-07-2011
I followed your advice on checking the returned value of read () and write (), read returns 4 bytes, which is the same as what write() returns, I believe that means I only get the chance to read the input from the child that finishes last, as he overwrites the pipe ?

At this point I only see 2 solutions for the problem, the one I already solved, and having a pipe for every child like shamrock suggested(an array of pipes maybe ?), will try the sham's proposal.
# 9  
Old 11-07-2011
Quote:
Originally Posted by ephesos
I followed your advice on checking the returned value of read () and write (), read returns 4 bytes, which is the same as what write() returns, I believe that means I only get the chance to read the input from the child that finishes last, as he overwrites the pipe ?

At this point I only see 2 solutions for the problem, the one I already solved, and having a pipe for every child like shamrock suggested(an array of pipes maybe ?), will try the sham's proposal.
But if you are allowed to call read only once in the parent then it wont work as my solution requires 2 reads. Going over your posted code it looks like you start with one parent then fork creating a child which then forks itself creating another child...so you end up having a parent a child and a grandchild. Is that how you are supposed to approach it as that simplifies things and makes it completely doable with a single read in the parent.
# 10  
Old 11-07-2011
If you follow the code more closely you'll notice that it's only the parent that calls fork(), so what i have is a parent with n children processes.

Easy way to check this, do:

int pid = getppid(); // pid = parent's pid
printf ("pid = %d\n", pid);

inside of if (pid [i] == 0) { } , you'll get the same parent pid for each of the n children.
# 11  
Old 11-07-2011
Code:
int aux[nr_proc]

you should give this a fixed number not a variable. sizeof() is set at compile-time and knows nothing about variable-size arrays. I think sizeof() is only giving you 4.
# 12  
Old 11-08-2011
I tried to do that, it still returns 4, which is also the size of an int in my debian.
# 13  
Old 11-08-2011
In your code, you are creating pipe inside loop
Code:
for (i = 0; i < nr_proc; i++) {
     pipe (fd);

So you are losing data of previous child, try to keep it above for loop and try...
These 2 Users Gave Thanks to siva shankar For This Post:
# 14  
Old 11-08-2011
I can't believe how simple that was, it works now. You have my sincere gratitude !
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