Google Answers Logo
View Question
 
Q: perl / fork question ( No Answer,   2 Comments )
Question  
Subject: perl / fork question
Category: Computers > Programming
Asked by: marcfest-ga
List Price: $25.00
Posted: 27 Aug 2005 08:23 PDT
Expires: 11 Sep 2005 05:19 PDT
Question ID: 561121
Hello - 

Please take a look at the code below (it can be invoked on my site via
http://www.marcfest.com/temp/fork.cgi).
 
When running the program in the linux shell it dumps its output right
away, without waiting for the child process ("sleep 10") to complete.
When invoking it through the browser, though, it does not send its
output to the browser until the child process is completed.
 
How do I fix that? I want it so that when invoked through the browser
the scripts prints the output and terminates the browser connection
immediately while then finishing the child process in the background.
 
As a solution to his question please post a revision of the script
that will work as described above.

Thank you for helping.
 
Marc.
 
####################################################
#!/usr/bin/perl -w

my $time = time;

$| = 1;

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

print "$time We are about to create the child!", "\n";

defined(my $childpid = fork) or die "Cannot fork: $!";

        if ($childpid) {

print <<End_of_Parent;
I am the parent speaking. I have successfully created a child process.
The Process Identification Number (PID) of the child process is: $childpid.
The child will be cleaning up all the files in the directory. It might 
take a while, but you do not have to wait!
End_of_Parent

exit;

} else {

#this is the child process

open STDIN, "</dev/null";
open STDOUT, ">/dev/null";

sleep 10;
        exit(0);
}


###########################################
Answer  
There is no answer at this time.

Comments  
Subject: Re: perl / fork question
From: bozo99-ga on 27 Aug 2005 15:43 PDT
 
Forget /dev/null.   close() STDIN, STDOUT and STDERR.
Subject: Re: perl / fork question
From: marcfest-ga on 27 Aug 2005 20:33 PDT
 
Wow. 

close (STDOUT);
close (STDIN);
close (STDERR);

did the trick. Go ahead and claim the price. Thank you.

Marc.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy