http://qs321.pair.com?node_id=722571

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

I've always found it annoying that if you start a Perl program like
#!/usr/local/bin/perl system("some_long_running_shell_command"); some_other_stuff();
from the shell and you want to terminate it by using CTRL-C, you have to hit CTRL-C twice.

It gets worse if you run a loop like

#!/usr/local/bin/perl for (1..10) { system("some_long_running_shell_command"); } some_other_stuff();
then you have to hit CTRL-C ten times in a row to terminate the program!

You can easily reproduce this behavior by running

#!/usr/local/bin/perl system("sleep 10"); sleep 10;
which requires two CTRL-C key strokes to terminate. On the other hand, if you use backquotes, as in
#!/usr/local/bin/perl `sleep 10`; sleep 10;
then one CTRL-C suffices. In both cases, both the perl program and the executed shell script are running in the same process group, so the shell will send SIGINT to both processes if you hit CTRL-C once.

However, in the system() case, perl performs some magic to block the SIGINT signal before it execs the child (pp_system in util.c):

#ifndef PERL_MICRO rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand); rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand); #endif
So what happens is that the shell sends SIGINT to both parent and child, but perl choses to only terminate the child, while the parent keeps running.

Why is it doing that?