in reply to sending signal to forked process
I think using forks only complicates matters unnecessarily (as opposed to plain forking), because forks goes to great lengths to install signal handler wrappers as a tied %SIG hash etc. (see the source of the for-internal-use-only forks::signals in case you're interested in the details). In other words, unless you already have code implemented for the threads API, you might reconsider using plain forking instead.
Anyhow, there is an environment variable THREADS_DAEMON_MODEL which you can set to reverse the child-parent relationship between the main process and the secondary "thread state" control process of forks. This makes things somewhat easier, as you can then install an IGNORE signal handler that applies to both of these processes. Something like this:
#!/usr/local/bin/perl -l BEGIN { # reverses the parent-child relationship # between main and "thread state" process $ENV{THREADS_DAEMON_MODEL} = 1; setpgrp; # for sending signals to our process group } use forks; $SIG{USR1} = 'IGNORE'; my @procs; for (1..3) { # fork a few processes push @procs, threads->new( sub { $SIG{USR1} = sub { print "hi! from 'thread' #$_" }; print "running 'thread' #$_ (pid=$$) ..."; sleep 3; } ); } sleep 1; my $ps = "ps fT -o pid,ppid,pgrp,cmd"; print "\ninitial processes:"; system $ps; sleep 1; my $pgrp = getpgrp; print "\nsending process group $pgrp USR1 signal"; kill -10, $pgrp; $_->join() for @procs; sleep 1; print "\nprocesses after join()"; system $ps; __END__ $ ./833279.pl running 'thread' #1 (pid=24090) ... running 'thread' #2 (pid=24091) ... running 'thread' #3 (pid=24092) ... initial processes: PID PPID PGRP CMD 25932 25930 25932 bash -rcfile .bashrc 24088 25932 24088 \_ /usr/local/bin/perl -l ./833279.pl 24089 24088 24088 \_ /usr/local/bin/perl -l ./833279.pl 24090 24089 24088 \_ /usr/local/bin/perl -l ./833279.pl 24091 24089 24088 \_ /usr/local/bin/perl -l ./833279.pl 24092 24089 24088 \_ /usr/local/bin/perl -l ./833279.pl 24093 24089 24088 \_ ps fT -o pid,ppid,pgrp,cmd sending process group 24088 USR1 signal hi! from 'thread' #1 hi! from 'thread' #2 hi! from 'thread' #3 processes after join() PID PPID PGRP CMD 25932 25930 25932 bash -rcfile .bashrc 24088 25932 24088 \_ /usr/local/bin/perl -l ./833279.pl 24089 24088 24088 \_ /usr/local/bin/perl -l ./833279.pl 24094 24089 24088 \_ ps fT -o pid,ppid,pgrp,cmd
As you can see, the two processes in question (24088 = control process, 24089 = main process) "survived", despite the signal having been sent to the entire process group.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sending signal to forked process
by ajeet@perl (Acolyte) on Apr 08, 2010 at 05:10 UTC |