signal HANDLER in perl

G

gaurava

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
 
X

xhoster

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
 
X

xhoster

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
 
G

gaurava

thanks that helps. I will try it out.

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top