Rahul
2006-01-06 04:04:54 UTC
Hi
I have a script main.pl as follows
for ($i = 1; $i<=5000; $i++)
{
&foo();
}
sub foo {
if (fork() == 0){
# inside CHILD
# do something
exit 0;
else {
# inside PARENT
# do nothing
}
}
now, since the parent does not wait upon the child to finish (i don't want
it to explicitly wait, since i want to have multiple children run
simultaneously), so there are lot of zombie children left, thereby making
the system slow, and finally hang.
i researched a lot, there are 2 ways to avoid this.
1) $SIG{'CHLD'} = 'IGNORE';
2) $SIG{'CHLD'} = sub { while(waitpid(-1,WNOHANG)>0) {} } ;
also I have lots of calls such as
die if system("ls")
die if system("rm ..")
etc in my main parent routine as well as the child part of the foo()
routine...
So if I follow method 1) above to avoid zombie, the system() calls rreturn
error (as the inherent 'wait' has no child process to wait upon , as SIGCHLD
is ignored) (The ERRORS are seen only on perl 5.8.6 and not on perl 5.6.1)
If I follow method 2) above to avoid zombies, by writing my own handler, the
system() calls wait indefinitely as the child's SIGCHLD has already been
taken care of by my handler.
Could some1 help me avoid zombies without interfering with my system() calls
?
thanks,
Rahul
I have a script main.pl as follows
for ($i = 1; $i<=5000; $i++)
{
&foo();
}
sub foo {
if (fork() == 0){
# inside CHILD
# do something
exit 0;
else {
# inside PARENT
# do nothing
}
}
now, since the parent does not wait upon the child to finish (i don't want
it to explicitly wait, since i want to have multiple children run
simultaneously), so there are lot of zombie children left, thereby making
the system slow, and finally hang.
i researched a lot, there are 2 ways to avoid this.
1) $SIG{'CHLD'} = 'IGNORE';
2) $SIG{'CHLD'} = sub { while(waitpid(-1,WNOHANG)>0) {} } ;
also I have lots of calls such as
die if system("ls")
die if system("rm ..")
etc in my main parent routine as well as the child part of the foo()
routine...
So if I follow method 1) above to avoid zombie, the system() calls rreturn
error (as the inherent 'wait' has no child process to wait upon , as SIGCHLD
is ignored) (The ERRORS are seen only on perl 5.8.6 and not on perl 5.6.1)
If I follow method 2) above to avoid zombies, by writing my own handler, the
system() calls wait indefinitely as the child's SIGCHLD has already been
taken care of by my handler.
Could some1 help me avoid zombies without interfering with my system() calls
?
thanks,
Rahul