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.


In reply to Re: sending signal to forked process by almut
in thread sending signal to forked process by ajeet@perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.