Why can't we just change the signal disposition on system()?

Discussion in 'Perl Misc' started by grocery_stocker, Mar 24, 2007.

  1. This is taken from documentation:

    Since "SIGINT" and "SIGQUIT" are ignored during the execution of
    "system", if you expect your program to
    terminate on receipt of these signals you will need to
    arrange to do so yourself based on the return
    value.

    @args = ("command", "arg1", "arg2");
    system(@args) == 0
    or die "system @args failed: $?"

    You can check all the failure possibilities by
    inspecting $? like this:

    if ($? == -1) {
    print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
    printf "child died with signal %d, %s coredump
    \n",
    ($? & 127), ($? & 128) ? 'with' :
    'without';
    }
    else {
    printf "child exited with value %d\n", $? >> 8;
    }

    Why can't we just trap the signal and have it exit? Ie like
    sub sig {
    exit;
    }

    $SIG{INT} = \&sig;


    Chad
     
    grocery_stocker, Mar 24, 2007
    #1
    1. Advertising

  2. On Mar 23, 5:50 pm, "grocery_stocker" <> wrote:
    > This is taken from documentation:
    >
    > Since "SIGINT" and "SIGQUIT" are ignored during the execution of
    > "system", if you expect your program to
    > terminate on receipt of these signals you will need to
    > arrange to do so yourself based on the return
    > value.
    >
    > @args = ("command", "arg1", "arg2");
    > system(@args) == 0
    > or die "system @args failed: $?"
    >
    > You can check all the failure possibilities by
    > inspecting $? like this:
    >
    > if ($? == -1) {
    > print "failed to execute: $!\n";
    > }
    > elsif ($? & 127) {
    > printf "child died with signal %d, %s coredump
    > \n",
    > ($? & 127), ($? & 128) ? 'with' :
    > 'without';
    > }
    > else {
    > printf "child exited with value %d\n", $? >> 8;
    > }
    >
    > Why can't we just trap the signal and have it exit? Ie like
    > sub sig {
    > exit;
    > }
    >
    > $SIG{INT} = \&sig;
    >
    > Chad



    Wait, I sat down and thought about this. system() has it's own set of
    signal handlers. Since each function can like only have one signal
    handler, the ones used by system() was supercede the ones I would
    write.
     
    grocery_stocker, Mar 24, 2007
    #2
    1. Advertising

  3. On Mar 23, 5:50 pm, "grocery_stocker" <> wrote:
    > This is taken from documentation:
    >
    > Since "SIGINT" and "SIGQUIT" are ignored during the execution of
    > "system", if you expect your program to
    > terminate on receipt of these signals you will need to
    > arrange to do so yourself based on the return
    > value.
    >
    > @args = ("command", "arg1", "arg2");
    > system(@args) == 0
    > or die "system @args failed: $?"
    >
    > You can check all the failure possibilities by
    > inspecting $? like this:
    >
    > if ($? == -1) {
    > print "failed to execute: $!\n";
    > }
    > elsif ($? & 127) {
    > printf "child died with signal %d, %s coredump
    > \n",
    > ($? & 127), ($? & 128) ? 'with' :
    > 'without';
    > }
    > else {
    > printf "child exited with value %d\n", $? >> 8;
    > }
    >
    > Why can't we just trap the signal and have it exit? Ie like
    > sub sig {
    > exit;
    > }
    >
    > $SIG{INT} = \&sig;
    >
    > Chad



    Wait, I sat down and thought about this. system() has it's own set of
    signal handlers. Since each function can like only have one signal
    handler per signal, the ones used by system() was supercede the ones I
    would
    write.
     
    grocery_stocker, Mar 24, 2007
    #3
  4. >>>>> "grocery" == grocery stocker <> writes:

    grocery> Why can't we just trap the signal and have it exit? Ie like
    grocery> sub sig {
    grocery> exit;
    grocery> }

    grocery> $SIG{INT} = \&sig;

    system is really just a convenience method for the following steps:

    sub my_system {
    my @args = @_;
    defined (my $kidpid = fork) or die "Cannot fork: $!";
    unless ($kidpid) { # kid does:
    exec @args;
    die "Cannot find $args[0]: $!";
    }
    # parent does:
    {
    local $SIG{INT} = $SIG{QUIT} = 'IGNORE';
    waitpid($kidpid);
    }
    return $?;
    }

    If you want slightly different behavior, just copy this, and amend as
    necessary. For example, just before the exec, you can do things like change
    directory, close or open filehandles, set the process priority, and so on. In
    your case, you can set up different signal handlers around the waitpid.

    print "Just another Perl hacker,";

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Randal L. Schwartz, Mar 24, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. siarsky
    Replies:
    1
    Views:
    816
    Jol Padgett
    Feb 10, 2004
  2. Michael Pronath
    Replies:
    1
    Views:
    1,183
    Diez B. Roggisch
    Jan 3, 2005
  3. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    472
    Jack Orenstein
    Sep 18, 2005
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    917
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,033
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page