in reply to Catching signals

use strict; my $pid = fork(); if ($pid) { print "I am parent $$\n"; kill INT => $pid; print "I am parent $$\n"; } elsif (defined($pid)) { print "I am child $$\n"; local $SIG{INT} = sub { die "\nOutta here!\n" }; print "I am child $$\n"; exit 1; } else { print "child was not created\n"; }
The child isn't hanging around much. I wouldn't be surprised if you never got the "Outta here" message.

You'll want the child to block. Add sleep 30 or something, after setting up the $SIG{INT}.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: •Re: Catching signals
by Anonymous Monk on Sep 04, 2002 at 18:13 UTC
    As I have replied to RMGir, i have tried making those corrections already and they don't seem to work. To clarify, here's the adapted code. I still don't see the handler message.
    use strict; use sigtrap 'handler' => \&myhand, 'INT'; sub myhand{ print "\nOutta here $$!\n" }; my $pid = fork(); if ($pid) { print "I am parent $$\n"; for (1 .. 1000000){} kill INT => $pid; print "I am parent $$\n"; } elsif (defined($pid)) { print "I am child $$\n"; while (1){ sleep 1; } } else { print "child was not created\n"; } wait(); print "hi\n";
      Ahh. That code works as expected for me. The signal handler is called in both the parent and the child. I had to use a ^Z to finally get out of the code.

      So, what's your perl -V look like?

      -- Randal L. Schwartz, Perl hacker

        Please see 195144 for my answer and RMGir's reply. Is there any other work around to communicate between child and parent under win32? (this all started when a child was doing a blocking read and the parent was used to break out of that read cycle) So far, I have tried pipe $parent,$child or die, which still seemed to be blocked by that read statement. I also tried open(TO,"$-") || die but that's not supported by win32 either. I also tried threads, but activestate, in a stroke of genius, did not compile the perl.exe binary with 5005Threads, which makes the Thread.pm module useless. Instead, they compiled it with iThreads, which, even after getting threads.pm and threads::shared.pm into perl 5.6.1, do not seem to work for me (I get an error message saying that perl version is less than 5.8).

        Any other suggestions?