in reply to Re: Gracefull Death of a Child: How to Catch Mum's SIGNALS?
in thread Gracefull Death of a Child: How to Catch Mum's SIGNALS?

well,
using within Mum 'Mum' and Darling 'Dar'
for my $k ( keys %SIG ) { $SIG{$k} = sub { print "\n$$: 'Mum' Caught \$SIG{ $k }\n" } }
and killing mum.pl by her pid by USR1 - but any other SIG does the same:
kill -USR1 7405
I get mum's sigh
7405: 'Mum' Caught $SIG{ USR1 }
but Darling still dies silently ;(


If (me, playing around) Mum and Darling use 'local':

for my $k ( keys %SIG ) { local $SIG{$k} = sub { print "\n$$: Mum Caught \$SIG{ $k }\n" } }
Only Mum doesn't catch any SIG anymore, I just get
userdefined Signal 1
(in german) :(
Even the use of sigtrap within Darling makes no difference..
Thanks anayway ..
Carl

Replies are listed 'Best First'.
Re^3: Gracefull Death of a Child: How to Catch Mum's SIGNALS?
by gt8073a (Hermit) on Apr 04, 2007 at 16:00 UTC

    As others have mentioned, threads are in a single process. So killing mum is killing darling. They're the same process, different thread.

    In the code below, $SIG{$k} is local to the for block.

    for my $k ( keys %SIG ) { local $SIG{$k} = sub { print "\n$$: Mum Caught \$SIG{ $k }\n" } }

    For your own experience, try removing %SIG handlers from mum, and only put them in darling. Also, compare fork() to your threads.

    JJ
      Well,
      as I wrote, I was playing around with local, to perform a
      kind of nothing-left-open-test - and nothing doesn't work.
      What is interessting is, that there is now an example at
      http://www.cpanforum.com/dist/threads that works,
      it causes the threads to break their sleep, and the signals SIGUSR1
      were set or thrown by mum.


      As soon as I changed it a bit (code even there) that signals from
      'outside' should be caught - it fails again: the SIGUSR1 were sent
      to the threads, mum waits,but the thread's sleep is not broken.


      So it seems this is something that Perl can't do!

      Surprised and and bit disappointed,
      Carl

Re^3: Gracefull Death of a Child: How to Catch Mum's SIGNALS?
by Moron (Curate) on Apr 04, 2007 at 13:38 UTC
    oh ok! re "use sigtrap makes no difference" - well it wouldn't unless you define a handler for the signal being sent.

    Update: what about using Thread::Queue to send and receive homegrown "signals" - that way at least you have some guarantee of thread safety.

    -M

    Free your mind

      Moron,
      I did that! The problem is nothing arrives 'in' Darling!
      Even the example that comes with threads: pool.pl
      tries to 'fake' the signals by defining its own threaded variable $TERM
      which is set by Mums SIG-handler.
      BUT in the childs this $TERM has to be checked ALL THE TIME,
      otherwise the childs were allready killed before they had a chance
      to perform their gracefull death..
      sighing
      Carl