$SIG{CHLD} and system

Discussion in 'Perl Misc' started by Heinrich Mislik, Sep 30, 2004.

  1. perl -e '$SIG{CHLD} = "IGNORE";system("date") and die'

    gives me

    Thu Sep 30 14:34:22 MSZ 2004
    Died at -e line 1.

    I can understand, why this happens. But is it documented? Shouldn't system
    set up it's own signalhandlers? The function system in C does so. Does this mean, that perl does not call this function?

    Any thoughts


    Cheers

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Sep 30, 2004
    #1
    1. Advertising

  2. Heinrich Mislik

    Guest

    (Heinrich Mislik) wrote in message news:<415bfee0$0$12646$>...
    > perl -e '$SIG{CHLD} = "IGNORE";system("date") and die'
    > gives me
    >
    > Thu Sep 30 14:34:22 MSZ 2004
    > Died at -e line 1.


    That's exactly what it's supposed to give you...

    > I can understand, why this happens.


    Why what happens? Perl run's "date", then returns when date exits,
    after which it proceeds to die....perl is reporting that it die()ed.
    I'm not sure what you expected to happen. You can check return values
    etc of the system():

    $exit_value = $? >> 8;
    $signal_num = $? & 127
    $dumped_core = $? & 128

    --K-sPecial
    , Sep 30, 2004
    #2
    1. Advertising

  3. In article <415c5847$0$8106$-online.net>, says...
    >
    >
    >Heinrich Mislik wrote:
    >> perl -e '$SIG{CHLD} = "IGNORE";system("date") and die'
    >>
    >> gives me
    >>
    >> Thu Sep 30 14:34:22 MSZ 2004
    >> Died at -e line 1.
    >>
    >> I can understand, why this happens. But is it documented? Shouldn't system
    >> set up it's own signalhandlers? The function system in C does so. Does this
    > > mean, that perl does not call this function?
    >>
    >> Any thoughts

    >
    >Mostly one thought: read "perldoc -f system". It forks a child
    >and, in cases like yours when there's no meta chars in
    >the argument string, calls execvp. No system() call invoked.


    Right. But even with this its the same:

    perl -e '$SIG{CHLD} = "IGNORE";system("date 2>/dev/null") and die'

    Fri Oct 1 11:43:48 MSZ 2004
    Died at -e line 1.

    >Apart from that, I can't reproduce this behaviour
    >(just tested 5.6.1 and 5.8.[03] on Linux and Win32).
    >%SIG isn't changed by the call of system("date").


    Just to clarify: my point is, that the returncode of system is changed by setting $SIG{CHLD} = "IGNORE".

    >What version and OS are you on?


    I tried

    This is perl, v5.8.4 built for aix

    This is perl, v5.6.1 built for i386-freebsd

    This is perl, v5.8.2 built for cygwin-thread-multi-64int

    cygwin is a bit different with the last examle (with the 2>/dev/null):
    sh gets into an enless loop. This seems to be a problem with cygwin's
    C-function system.

    Cheers

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Oct 1, 2004
    #3
  4. In article <>, says...
    >
    >
    > (Heinrich Mislik) wrote in message news:<415bfee0$0$12646$>...
    >> perl -e '$SIG{CHLD} = "IGNORE";system("date") and die'
    >> gives me
    >>
    >> Thu Sep 30 14:34:22 MSZ 2004
    >> Died at -e line 1.

    >
    >That's exactly what it's supposed to give you...
    >
    >> I can understand, why this happens.

    >
    >Why what happens? Perl run's "date", then returns when date exits,
    >after which it proceeds to die....perl is reporting that it die()ed.
    >I'm not sure what you expected to happen. You can check return values
    >etc of the system():


    I expected to get the sam result for

    perl -e '$SIG{CHLD} = "IGNORE";system("date") and die $?'

    which prints

    Fri Oct 1 12:08:04 MSZ 2004
    -1 at -e line 1.

    and

    perl -e '$SIG{CHLD} = "DEFAULT";system("date") and die $?'

    which prints

    Fri Oct 1 12:09:12 MSZ 2004

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Oct 1, 2004
    #4
  5. Heinrich Mislik

    Joe Smith Guest

    wrote:

    > (Heinrich Mislik) wrote in message news:<415bfee0$0$12646$>...
    >
    >>perl -e '$SIG{CHLD} = "IGNORE";system("date") and die'
    >>gives me
    >>
    >>Thu Sep 30 14:34:22 MSZ 2004
    >>Died at -e line 1.
    >>I can't understand, why this happens.

    >
    > Why what happens? Perl run's "date", then returns when date exits,
    > after which it proceeds to die....perl is reporting that it die()ed.
    > I'm not sure what you expected to happen.


    He was expecting the value returned by system() to be the same
    as what gets stored in $? (zero on success, nonzero on failue).
    But that is no longer the case; system() is returning -1 regardless
    of the command's exit code.

    solaris% perl -le '$SIG{CHLD} = "DEFAULT";print(system("/bin/true"))'
    0
    solaris% perl -le '$SIG{CHLD} = "DEFAULT";print(system("/bin/false"))'
    65280
    solaris% perl -le '$SIG{CHLD} = "IGNORE";print(system("/bin/true"))'
    -1
    solaris% perl -le '$SIG{CHLD} = "IGNORE";print(system("/bin/false"))'
    -1

    This behavior is not explictly documented; you have to do some digging
    to see why the return value if system() has become misleading or useless.

    From 'perldoc -f system':

    The return value is the exit status of the program as returned
    by the "wait" call. To get the actual exit value shift right
    by eight (see below). Return value of -1 indicates a failure
    to start the program (inspect $! for the reason).

    From 'perldoc perlvar':

    Using a value of 'IGNORE' usually has the effect of
    ignoring the signal, except for the "CHLD" signal.
    See perlipc for more about this special case.

    From 'perldoc perlipc':

    On most Unix platforms, the "CHLD" (sometimes also known as
    "CLD") signal has special behavior with respect to a value
    of 'IGNORE'. Setting $SIG{CHLD} to 'IGNORE' on such a
    platform has the effect of not creating zombie processes
    when the parent process fails to "wait()" on its child
    processes (i.e. child processes are automatically reaped).
    Calling "wait()" with $SIG{CHLD} set to 'IGNORE' usually
    returns "-1" on such platforms.

    That means that 'perldoc -f system' should have said:

    The return value is what is returned by the "wait" call,
    which the program's exit status. (Except when $SIG{CHLD}
    is set to 'IGNORE', in which case system() returns -1.)

    Conclusion: If it is possible that your program will be running with
    a non-default value of $SIG{CHLD}, you'll have to change your
    program logic to not look at the return value from system().
    Change
    system($command) and die;
    to
    system($command); $? and die;


    -Joe
    Joe Smith, Oct 1, 2004
    #5
  6. In article <eDa7d.148741$MQ5.96256@attbi_s52>, says...

    >Conclusion: If it is possible that your program will be running with
    >a non-default value of $SIG{CHLD}, you'll have to change your
    >program logic to not look at the return value from system().
    >Change
    > system($command) and die;
    >to
    > system($command); $? and die;


    Won't work either. $SIG{CHLD} = "IGNORE" really means "i'm not interessted
    in the return the value from my childs". Since no zombie will be created,
    there is no way, get the return value.

    OTH, if system temporary changes $SIG{CHLD} and any other child exits
    during the system function does waitpid, there would remain an unwanted
    zombie.

    perdoc -f system should make this clear.

    cheers

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Oct 1, 2004
    #6
    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. matchstick86

    sig : process vs. process(sig)

    matchstick86, Oct 12, 2009, in forum: VHDL
    Replies:
    1
    Views:
    551
    power_hf2005
    Oct 13, 2009
  2. Dan Janowski

    trap() block signal mask, esp. CHLD

    Dan Janowski, Feb 8, 2005, in forum: Ruby
    Replies:
    0
    Views:
    118
    Dan Janowski
    Feb 8, 2005
  3. Pavel Smerk
    Replies:
    2
    Views:
    163
    Yukihiro Matsumoto
    Jul 18, 2006
  4. Sébastien Cottalorda

    Pb $SIG{CHLD}=sub{wait()}; in Perl 5.8.0

    Sébastien Cottalorda, Aug 1, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    112
    Sébastien Cottalorda
    Aug 1, 2003
  5. Eric Pozharski

    Selective $SIG{CHLD}

    Eric Pozharski, Nov 23, 2010, in forum: Perl Misc
    Replies:
    0
    Views:
    89
    Eric Pozharski
    Nov 23, 2010
Loading...

Share This Page