http://www.perlmonks.org?node_id=652103

LH2007 has asked for the wisdom of the Perl Monks concerning the following question:

i have a tiny code concern with signal handler.Forking 2 process,with parent process,there is a count from 0 to 100,with child process,a count down 100 to 0.From parent,if press Ctrl+c,child process stop while parent continue....My problem is:I don't know how to do to stop parent by press Ctrl+C again.Help me My Code
#!/usr/bin/perl my $pid = fork; die "Can't fork: $!" if ! defined $pid; if ( $pid ) { # parent print "Parent process: PID=$$,child=$pid\n"; my $i=0; print "Press Ctrl-C to terminate child\n"; print "Parent: Child:\n"; while($i<100) { $i++; print "$i\n"; sleep 1; $SIG{INT} = sub { warn "Sending sig INT to child PID $pid"; kill 'INT' => $pid; }; } } else { # child my $j=100; sleep 1; while($j>0){ $j--; print " $j\n"; sleep 1; }; $SIG{INT} = sub { die "Caught sig INT" }; sleep 1 while 1; exit; },

Replies are listed 'Best First'.
Re: Problem with signal handler
by jbert (Priest) on Nov 21, 2007 at 11:44 UTC
    You're nearly there.

    You're not setting your child signal handler until after the loop has finished. Set it before the loop (just as soon as you know you're the child) and you'll see the message you're expecting.

    Some additional notes:

    • You're setting the parent signal handler every time around the loop. You only need to set it once, when you know you are the parent.
    • You probably don't need to propogate the signal from the parent to the child with a 'kill', since a ctrl-C is normally sent to all processes in a process group. The shell creates a process group for each command, which will be inherited by all forked processes (unless you call setpgrp to change it). Note that for other signals in other environments you may need the code you have to 're-kill'.
    • You don't have strict and warnings on. It's not hiding a problem in this case, but you should always have them on, particularly to check your code before asking someone else about it.
    Edit: I missed the actual question. D'oh. Thanks for answering below.

    Of course, if the poster wants to kill both parent and child, it's possible that no signal handling code is necessary at all. a ctrl-C from the shell should send a SIGINT to the process group, giving that behaviour by default.

Re: Problem with signal handler
by ChOas (Curate) on Nov 21, 2007 at 11:46 UTC
    Hi! Just keep track in your parent to see if/how many times ctrl-c was pressed: like :
    [...] my $pid = fork; my $dontDie = 2; [...]
    And then in your Interrupt routine:
    $SIG{INT} = sub { die "This is the end" unless --$dontDie; warn "Sending sig INT to child PID $pid"; kill 'INT' => $pid; };
    (This is just a quick fix, do please pay close attention to jbert's comments)

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;