fork() & pipe()

B

bgy

Hi,

I'm trying to understand fork() & pipe() mechanism.

Concerning fork() i understood but pipe() still presents some mysteries
for me.

I explain, i would like to create 5 childrens (5 + 1 parent).
To understand i try to send each own pid to the parent.

Here is how i do this :
#!/usr/bin/perl

use strict;
use warnings;
use IO::Handle;
use constant {
MAX_PROCESS => 2
};

my ($child, $pid, @childs, @process_list, $child_pid);

pipe(FROM_CHILD, TO_PARENT);

for (1...MAX_PROCESS) {
$pid = fork();
if ($pid) { # Parent code
push(@childs, $pid);
close TO_PARENT;
$child_pid = <FROM_CHILD>;
close FROM_CHILD;
print "My child's pid : $child_pid \n";
push(@process_list, $child_pid);
}
else { #child
close FROM_CHILD;
print TO_PARENT $$;
close TO_PARENT;
print "New process launched (",$_,"/",MAX_PROCESS,"): [$$]\n";
exit(0);
}
}

print "[$$] -> @process_list \n";

foreach (@childs) {
waitpid($_,0);
}


Here is what i got :
maanes@void:~/workspace$ ./fork.pl
New process launched (1/2): [32497]
My child's pid : 32497
print() on closed filehandle TO_PARENT at ./fork.pl line 26.
New process launched (2/2): [32498]
readline() on closed filehandle FROM_CHILD at ./fork.pl line 19.
Use of uninitialized value in concatenation (.) or string at ../fork.pl line 21.
My child's pid :
Use of uninitialized value in join or string at ./fork.pl line 33.
[32496] -> 32497


So for the first child it works properly.
But i have to admit that i don't understand this error.
So if someone could help me to fix this...

Thanks.
 
X

xhoster

You close the pipe the first time through the loop, so it is no longer
open the 2nd time through.

If you want all children to talk on the same pipe, then don't close it
in the loop. If you want different pipes for each child, then create the
pipe inside the loop, not before it.

....
print TO_PARENT "$$\n";

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
B

bgy

You close the pipe the first time through the loop, so it is no longer
open the 2nd time through.

If you want all children to talk on the same pipe, then don't close it
in the loop. If you want different pipes for each child, then create the
pipe inside the loop, not before it.

...

print TO_PARENT "$$\n";

Xho

Thanks for the help !
It works now.
I did what you told me, place the pipe inside the loop !

But still one mystery, what is the utily of \n here :
print TO_PARENT "$$\n";

bgy
 
X

xhoster

bgy said:
Thanks for the help !
It works now.
I did what you told me, place the pipe inside the loop !

But still one mystery, what is the utily of \n here :

You read what is written to TO_PARENT using the construct <FROM_CHILD>,
which is line-oriented. So you should write to it in lines. Perhaps
you used -l or set $\ on order to do this, but it didn't look like it.

If the child closes the pipe after writing to it, then you might not need
the "\n", as the eof causes the <FROM_CHILD> to return. If you have all
children write on one pipe, then there is no eof after the first child
and <FROM_CHILD> never returns, and you have a deadlock.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top