T
theater
Hello,
When you fork() you'd like your child to be able to communicate with the
father, and vice versa. This has usually been handled with the help of
pipe() and this is also usually prone to troubles...
Such as this one:
I only want to kid to talk to daddy, not the other way round.
I create the pipe.
Then I have a loop , each iteration will create a new child.
Here's a snippet: (lots of error checking omitted here for the sake of
simplicity)
pipe(READHANDLE, WRITEHANDLE);
foreach $myKid ( @childrenList ) {
$pid = fork();
if($pid == 0) {
# I am the kid
# I close the reading end of the pipe
close(READHANDLE);
# and then I do y stuff and exit
exit;
}
else {
# I am the father, yes i know i should check for error, # but this
is just an example
# i do my stuff here
}
}
--> the problem is:
1/ if i leave it like above, some of the kids will be stuck... They just
don't exit(). It's not a resource problem because in reality i control
the number of simultaneous children.
And I have a print() statement just before exit()ing and I can see it
when debugging.
First of all I'm clueless about why this happens, but...
2/ if I close() the WRITEHANDLE end of the pipe in the father's section,
then I have absolutely no deadlock problem anymore BUT I won't be able
to get the other children to write anymore as they inherit from the
father, and after the 1st child this handle will be closed...
This seems to be quite common but in the case of a loop I have not seen
any solutions / fix.
Thanks for sharing your thoughts on this matter.
When you fork() you'd like your child to be able to communicate with the
father, and vice versa. This has usually been handled with the help of
pipe() and this is also usually prone to troubles...
Such as this one:
I only want to kid to talk to daddy, not the other way round.
I create the pipe.
Then I have a loop , each iteration will create a new child.
Here's a snippet: (lots of error checking omitted here for the sake of
simplicity)
pipe(READHANDLE, WRITEHANDLE);
foreach $myKid ( @childrenList ) {
$pid = fork();
if($pid == 0) {
# I am the kid
# I close the reading end of the pipe
close(READHANDLE);
# and then I do y stuff and exit
exit;
}
else {
# I am the father, yes i know i should check for error, # but this
is just an example
# i do my stuff here
}
}
--> the problem is:
1/ if i leave it like above, some of the kids will be stuck... They just
don't exit(). It's not a resource problem because in reality i control
the number of simultaneous children.
And I have a print() statement just before exit()ing and I can see it
when debugging.
First of all I'm clueless about why this happens, but...
2/ if I close() the WRITEHANDLE end of the pipe in the father's section,
then I have absolutely no deadlock problem anymore BUT I won't be able
to get the other children to write anymore as they inherit from the
father, and after the 1st child this handle will be closed...
This seems to be quite common but in the case of a loop I have not seen
any solutions / fix.
Thanks for sharing your thoughts on this matter.