Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > signal HANDLER in perl

Reply
Thread Tools

signal HANDLER in perl

 
 
gaurava@lycos.com
Guest
Posts: n/a
 
      04-25-2007
Hi,
I am trying to write a program where the main process registers a
SIGNAL handler for SIGCHLD, forks a bunch of processes and then reaps
the dead children. I have some control over when to register the
signal handler (before or after forking the child processes but not a
whole lot). I am seeing some weird behaviour. Basically I see a valid
child exit, handle it, and then start seeing a slew of SIGCHLD signals
from a process with PID 0. I was wondering if someone could tell me
the right way to handle signals, or has seen this problem before and
knows how to fix it. I am copying pseudo code for my script below.

Thanks,
Gaurav


sub handler {
while(($kid = waitpid(-1, WNOHANG)) != -1) {
if ($kid == $imp_pid) {
exit();
}
}
}

$imp_pid = fork_important_child();
$SIG{CHLD} = 'handler;

$other_pid = fork_normal_process();
$yet_another_pid = fork_normal_process();
:
:
snip

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-25-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
> I am trying to write a program where the main process registers a
> SIGNAL handler for SIGCHLD, forks a bunch of processes and then reaps
> the dead children. I have some control over when to register the
> signal handler (before or after forking the child processes but not a
> whole lot). I am seeing some weird behaviour. Basically I see a valid
> child exit, handle it, and then start seeing a slew of SIGCHLD signals
> from a process with PID 0.


Perhaps you see something you interpret as that, but you are probably
wrong. Since you didn't show us what you actually do see, it is hard to
point out how you are interpreting it wrongly. We can make a guess, maybe
it will be right, maybe not.


> I was wondering if someone could tell me
> the right way to handle signals, or has seen this problem before and
> knows how to fix it. I am copying pseudo code for my script below.


Please post real code. Otherwise, it is hard to give you real answers.

>
> Thanks,
> Gaurav
>
> sub handler {
> while(($kid = waitpid(-1, WNOHANG)) != -1) {



from perldoc -f waitpid:

Waits for a particular child process to terminate
and returns the pid of the deceased process, or
"-1" if there is no such child process. On some
systems, a value of 0 indicates that there are
processes still running.

So if waitpid is returning 0, then you are probably on a system in which
waitpid returns 0 when there are still processes running.

-1 means "There are no processes for me to wait for", while 0 means
"There are processes to wait for, but you said WNOHANG so I am not going
to actually wait for them." So maybe you should test for $kid being
>0 rather than !=-1.



> if ($kid == $imp_pid) {
> exit();
> }


If you are only interested in the case in which $kid==$imp_pid, you should
probably specify $imp_pid rather than -1 as the first argument to waitpid.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-25-2007
(E-Mail Removed) wrote:
>
> > if ($kid == $imp_pid) {
> > exit();
> > }

>
> If you are only interested in the case in which $kid==$imp_pid, you
> should probably specify $imp_pid rather than -1 as the first argument to
> waitpid.


Scratch that. Perhaps you should use $imp_pid as the first argument to
waitpid, but perhaps not, depending on how long-running your job is and
thus the risk of accumulating zombies.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
gaurava@lycos.com
Guest
Posts: n/a
 
      04-27-2007
thanks that helps. I will try it out.

On Apr 25, 12:22 pm, (E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>
> > > if ($kid == $imp_pid) {
> > > exit();
> > > }

>
> > If you are only interested in the case in which $kid==$imp_pid, you
> > should probably specify $imp_pid rather than -1 as the first argument to
> > waitpid.

>
> Scratch that. Perhaps you should use $imp_pid as the first argument to
> waitpid, but perhaps not, depending on how long-running your job is and
> thus the risk of accumulating zombies.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> Usenet Newsgroup Service $9.95/Month 30GB



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does a handler render the request to another handler in the MVC of SPRING 2.0.1 framework? rayzyang@gmail.com Java 0 05-30-2007 11:28 AM
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
Re: JNI calls from inside UNIX signal handler Joseph Millar Java 5 07-10-2003 04:34 PM
Re: JNI calls from inside UNIX signal handler Marc Rochkind Java 0 07-09-2003 03:44 PM



Advertisments