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?


In reply to system() requires double ctrl-c by saintmike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.