Problem with fork

K

Krishna

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
 
N

Nicholas Dronen

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
 
G

Greg Bacon

: 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fork/exec question 6
Linux: using "clone3" and "waitid" 0
fork and hanging 1
problem with fork 8
Fork Problem 2
fork it 11
fork and blocking... 3
fork() & pipe() 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top