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
from the shell and you want to terminate it by using CTRL-C, you have to hit CTRL-C twice.#!/usr/local/bin/perl system("some_long_running_shell_command"); some_other_stuff();
It gets worse if you run a loop like
then you have to hit CTRL-C ten times in a row to terminate the program!#!/usr/local/bin/perl for (1..10) { system("some_long_running_shell_command"); } some_other_stuff();
You can easily reproduce this behavior by running
which requires two CTRL-C key strokes to terminate. On the other hand, if you use backquotes, as in#!/usr/local/bin/perl system("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.#!/usr/local/bin/perl `sleep 10`; sleep 10;
However, in the system() case, perl performs some magic to block the SIGINT signal before it execs the child (pp_system in util.c):
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.#ifndef PERL_MICRO rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand); rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand); #endif
Why is it doing that?
Back to
Seekers of Perl Wisdom