problem with system() and fork

Discussion in 'Perl Misc' started by gabriele.ravanelli@gmail.com, Nov 7, 2005.

  1. Guest

    Hi, I've got a problem whit fork() and system()
    I use the fork() function to fork my program and inside the code I use
    the funtion sytem() to call a program and to get the return status of
    this one.
    the problem is that the function system don't work if I use the fork()
    before. In this case the ret valute is always -1. Why system() works
    wrong?
    Thaks, Gabriele
     
    , Nov 7, 2005
    #1
    1. Advertising

  2. wrote in
    news::

    > Hi, I've got a problem whit fork() and system()
    > I use the fork() function to fork my program and inside the code I use
    > the funtion sytem() to call a program and to get the return status of
    > this one.
    > the problem is that the function system don't work if I use the fork()
    > before. In this case the ret valute is always -1. Why system() works
    > wrong?


    Please read the posting guidelines for this group. After that, post a
    short, but complete script we can run that still exhibits the problem you
    are experiencing.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 7, 2005
    #2
    1. Advertising

  3. Guest

    A. Sinan Unur wrote:
    > wrote in
    > news::
    >
    > > Hi, I've got a problem whit fork() and system()
    > > I use the fork() function to fork my program and inside the code I use
    > > the funtion sytem() to call a program and to get the return status of
    > > this one.
    > > the problem is that the function system don't work if I use the fork()
    > > before. In this case the ret valute is always -1. Why system() works
    > > wrong?

    >
    > Please read the posting guidelines for this group. After that, post a
    > short, but complete script we can run that still exhibits the problem you
    > are experiencing.
    >
    > Sinan
    >
    > --
    > A. Sinan Unur <>
    > (reverse each component and remove .invalid for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:
    > http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



    Sinan,

    May I suggest that when you refer people to the posting guidelines,
    that you include the link in your response, *before* you 'sign' the
    response. Having the link in what appears to be a signature line is an
    easy way to not have it noticed. I ignore signature lines because most
    of the time they contain gibberish that's supposed to be humorous, PGP
    keys, or some religious message which I have no interest in reading.

    In short, anything after your name at the end of the comment is likely
    to be ignored.
     
    , Nov 7, 2005
    #3
  4. Paul Lalli Guest

    wrote:
    > Hi, I've got a problem whit fork() and system()
    > I use the fork() function to fork my program and inside the code I use
    > the funtion sytem() to call a program and to get the return status of
    > this one.
    > the problem is that the function system don't work if I use the fork()
    > before. In this case the ret valute is always -1. Why system() works
    > wrong?


    #!/usr/bin/perl
    use strict;
    use warnings;

    print "About to fork...\n";
    my $pid = fork();

    if ($pid == 0){
    print "In the child\n";
    my $ret_val = system('ls') == 0 or die "Child cannot execute `ls`:
    $?\n";
    print "Child's retval: $ret_val\n";
    } else {
    print "In the parent\n";
    my $ret_val = system('ls') == 0 or die "Parent cannot execute `ls`:
    $?\n";
    print "Parent's retval: $ret_val\n";
    }
    __END__

    $ ./clpm.pl
    About to fork...
    In the parent
    In the child
    clpm.pl
    Child's retval: 1
    clpm.pl
    Parent's retval: 1
    $

    Seems to work fine for me. Perhaps you could post a short-but-complete
    program that exhibits the problem you're seeing?

    Paul Lalli
     
    Paul Lalli, Nov 7, 2005
    #4
  5. Paul Lalli Guest

    Paul Lalli wrote:
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    >
    > print "About to fork...\n";
    > my $pid = fork();
    >
    > if ($pid == 0){
    > print "In the child\n";
    > my $ret_val = system('ls') == 0 or die "Child cannot execute `ls`: $?\n";


    Arg. Why didn't I see that precedence error before I posted?
    (my $retval = system('ls')) == 0 or die ...

    > print "Child's retval: $ret_val\n";
    > } else {
    > print "In the parent\n";
    > my $ret_val = system('ls') == 0 or die "Parent cannot execute `ls`: $?\n";


    (my $retval = system('ls')) == 0 or die ...

    > print "Parent's retval: $ret_val\n";
    > }
    > __END__
    >
    > $ ./clpm.pl
    > About to fork...
    > In the parent
    > In the child
    > clpm.pl
    > Child's retval: 1

    Child's retval: 0
    > clpm.pl
    > Parent's retval: 1

    Parent's retval: 0
    > $


    Sorry about that.

    Paul Lalli
     
    Paul Lalli, Nov 7, 2005
    #5
  6. Jim Gibson <> wrote in
    news:071120051105334909%:

    > In article <>,
    > <> wrote:
    >
    >> A. Sinan Unur wrote:

    >
    > [original post snipped]
    >
    >> > A. Sinan Unur <>
    >> > (reverse each component and remove .invalid for email address)
    >> >
    >> > comp.lang.perl.misc guidelines on the WWW:
    >> > http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    >>
    >>
    >> Sinan,
    >>
    >> May I suggest that when you refer people to the posting guidelines,
    >> that you include the link in your response, *before* you 'sign' the
    >> response.

    ....

    > The reason people put this stuff in their sigs is so they don't
    > have to type it in or paste it each time.


    In addition, the guidelines are posted here very regularly, and people
    are expected to at least browse the archives before posting.

    When a poster receives a suggestion that s/he read the guidelines, and
    s/he cannot see a link, it does not take too much effort to type

    comp.lang.perl.misc guidelines

    in the Google search box.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 7, 2005
    #6
  7. Guest

    wrote:
    > Hi, I've got a problem whit fork() and system()
    > I use the fork() function to fork my program and inside the code I use
    > the funtion sytem() to call a program and to get the return status of
    > this one.
    > the problem is that the function system don't work if I use the fork()
    > before. In this case the ret valute is always -1. Why system() works
    > wrong?
    > Thaks, Gabriele


    system does an implicit fork and and, if its child has already been
    reaped (by the fork parent for instance, the system return is -1.

    Another scenario is setting the CHLD handler to ignore.
    Same thing: system will always return -1:

    $ perl -e '$SIG{CHLD}="IGNORE";system("sleep 1") == 0 or die $?'
    -1 at -e line 1.

    perldoc -f wait
    perldoc perlipc


    hth,
    --
    Charles DeRykus
     
    , Nov 8, 2005
    #7
    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. CMorgan
    Replies:
    3
    Views:
    383
    suresh shenoy
    Jan 2, 2008
  2. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    604
    Eric Snow
    Jan 8, 2009
  3. Michele Dondi

    A problem with fork() and managing processes

    Michele Dondi, Dec 1, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    151
    Michele Dondi
    Dec 3, 2004
  4. Henry Townsend

    Windows fork emulation (and buffering?) problem

    Henry Townsend, Apr 7, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    146
    A. Sinan Unur
    Apr 8, 2005
  5. Jacob JKW
    Replies:
    12
    Views:
    215
    Jacob JKW
    Jun 20, 2008
Loading...

Share This Page