Problem with fork

Discussion in 'Perl Misc' started by Krishna, Jun 27, 2003.

  1. Krishna

    Krishna Guest

    Hi -

    I am using 'fork' to execute a child process (a perl program) in my
    CGI.

    When the user submits the page, I am displaying a status page (created
    by a CGI)
    and forking the my external process. However spawning of this external
    process is redisplaying the the status page below the original page
    (means I am getting
    a TILED page of my status page).

    $pid = fork;
    if ($pid)
    { return 1; }
    else {
    close STDOUT;
    exec "/usr/local/bin/perl /../../push.pl ";
    exit 1;
    }

    Why its happenning like that?

    Thanks
    Krishna, Jun 27, 2003
    #1
    1. Advertising

  2. Krishna <> wrote:
    K> Hi -

    K> I am using 'fork' to execute a child process (a perl program) in my
    K> CGI.

    K> When the user submits the page, I am displaying a status page (created
    K> by a CGI)
    K> and forking the my external process. However spawning of this external
    K> process is redisplaying the the status page below the original page
    K> (means I am getting a TILED page of my status page).

    K> $pid = fork;
    K> if ($pid)
    K> { return 1; }
    K> else {
    K> close STDOUT;

    close STDERR; # perhaps you need this as well?

    K> exec "/usr/local/bin/perl /../../push.pl ";

    Trailing whitespace aside, "/../../push.pl " is equivalent
    to "/push.pl". Is this how it appears in your code?

    Regards,

    Nicholas

    --
    "Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html
    "Meanings are another story." http://www.ifas.org/wa/glossolalia.html
    Nicholas Dronen, Jun 28, 2003
    #2
    1. Advertising

  3. Krishna

    Greg Bacon Guest

    In article <>,
    Krishna <> wrote:

    : I am using 'fork' to execute a child process (a perl program) in my
    : CGI.

    Have you read mjd's "Suffering from Buffering?"[*]?

    [*] http://perl.plover.com/FAQs/Buffering.html

    What version of perl are you using? According to the perlfunc manpage

    Beginning with v5.6.0, Perl will attempt to flush all
    files opened for output before forking the child
    process, but this may not be supported on some
    platforms (see the perlport manpage). To be safe,
    you may need to set "$|" ($AUTOFLUSH in English) or
    call the "autoflush()" method of "IO::Handle" on any
    open handles in order to avoid duplicate output.

    : When the user submits the page, I am displaying a status page (created
    : by a CGI) and forking the my external process. However spawning of
    : this external process is redisplaying the the status page below the
    : original page (means I am getting a TILED page of my status page).
    :
    : $pid = fork;
    : if ($pid)
    : { return 1; }
    : else {
    : close STDOUT;
    : exec "/usr/local/bin/perl /../../push.pl ";
    : exit 1;
    : }

    I can't say given only this little example. Is running push.pl
    generating another copy?

    Someone else in this thread asked if /../../push.pl is the path you
    really meant to use. Does your web server's error log have any relevant
    information?

    You aren't checking whether the fork failed. Look for an undefined
    return:

    my $pid = fork;
    unless (defined $pid) {
    warn "$0: fork failed: $!\n";
    }

    You might also consider hooking up your parent and child processes as
    in the following example:

    #! /usr/local/bin/perl

    use warnings;
    use strict;

    print "Content-type: text/plain\n\n";

    open STDERR, ">&STDOUT" or print "$0: dup STDOUT: $!\n";

    my $pid = open my $kid, "-|";
    die "$0: fork: $!\n" unless defined $pid;

    if ($pid) {
    # parent

    while (<$kid>) {
    print "kid: $_";
    }
    }
    else {
    exec 'lynx', '-dump', 'http://www.perl.org/'
    or die "$0: exec: $!\n";
    }

    # check child's exit status
    # try changing lynx to something bogus
    $kid = waitpid $pid, 0;
    if ($kid == -1) {
    print "pid $pid already reaped\n";
    }
    else {
    print "exit status for $pid: $?\n";
    }

    Hope this helps,
    Greg
    --
    One of the paradoxes of software engineering is that people with bad ideas
    and low productivity often think of themselves as supremely capable. They
    are the last people whom one can expect to fall in line with a good strategy
    developed by someone else. -- Phil Greenspun
    Greg Bacon, Jun 28, 2003
    #3
    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. Patrick
    Replies:
    1
    Views:
    509
  2. mishra

    fork problem

    mishra, Jul 19, 2005, in forum: C Programming
    Replies:
    16
    Views:
    596
    Barry Schwarz
    Jul 22, 2005
  3. Small Fork Problem

    , Mar 5, 2007, in forum: C Programming
    Replies:
    3
    Views:
    310
    Martin Ambuhl
    Mar 5, 2007
  4. CMorgan
    Replies:
    3
    Views:
    354
    suresh shenoy
    Jan 2, 2008
  5. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    560
    Eric Snow
    Jan 8, 2009
Loading...

Share This Page