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 :
Here is what i got :
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.
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.