Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Parent process does not wait for Child processes to finish and exits after the waitpid statement

by scriptstudent (Initiate)
on Feb 06, 2012 at 18:32 UTC ( [id://952134]=perlquestion: print w/replies, xml ) Need Help??

scriptstudent has asked for the wisdom of the Perl Monks concerning the following question:

Hi All... I have perl program that used to work fine...The parent process would wait for all the children (using waitpid()) before proceeding... However, when I add LWP request command in the code before forking the child process, then the Parent process does not wait for the children to finish execution and exit...AND it also does not execute the rest of the code after waitpid statement. I have tried different ways to figure out how to make the parent to wait, but somehow the HTTP request statement is making a difference and it is not waiting for children. Any help/suggestions would be appreciated Below is the code snippet... . . .

use File::Copy; use Net::FTP; use Win32::Process; use File::Path; use LWP::UserAgent; use HTTP::Headers; use HTTP::Cookies; use HTTP::Response; . . . . #IMP: Below are the few request lines mainly the $res = $ua->request($ +req); statement which makes parent not to wait for the child processe +s ######## Process the URL response ua = new LWP::UserAgent; $ua->agent("$0/0.1 " . $ua->agent); $req = new HTTP::Request 'GET=> 'http://www.cpan.org/RECENT'; $req->header('Accept' => 'text/html'); # send request $res = $ua->request($req); my $url_response = $res->content; print $url_response . "\n"; print $res->status_line . "\n"; print $res->code . "\n"; . . . my @pids; # Process every Unlink item foreach $extractItem (@extractItemLines) { . . . . my $childpid = fork(); if ($childpid) { push @pids, $childpid; print "I am parent: $childpid\n\n"; } # elsif (defined $childpid) { elsif ($childpid==0) { exec($command); die "Error: Cannot exec EXTRACT command:'$command': $!\n"; print "I am child: $childpid\n\n"; } else { die "Error: Cannot fork EXTRACT process: $!\n" } ######### Check for successful execution #if ($? != 0) #{ # print "Error: $C4C_TCENG_DOWNLOAD_ITK exited with fatal error +code.\n"; # die "Error: $C4C_TCENG_DOWNLOAD_ITK exited with fatal error co +de.\n"; #} print logFP "############## Processed $extractItem ############### +#\n"; } # foreach $extractItem... #### wait for all extract processes to finish before zipping the d +irectory waitpid($_,0) for @pids;
. . . . Rest of the parent code

Replies are listed 'Best First'.
Re: Parent process does not wait for Child processes to finish and exits after the waitpid statement
by bart (Canon) on Feb 06, 2012 at 20:04 UTC
    • You're using Win32::Process so you're probably running this on Windows. I have my doubts if using a Unix mechanism on Windows is the best way to do it, or even if it'll work well.
    • Actually, fork on Windows doesn't start a new program. It starts a new thread.
    • Next, you're calling exec in the child process thread. exec doesn't return.
    • What does running an LWP request have to do with anything? OK, I'm officially confused now.
    In short: if this is Windows, I think your program is killed by exec in the child.
      Thanks for the comments... I did not realize that fork() may have issues on the windows platform. I would try spawning a thread for each process and then have the parent wait for those threads to exit. Would also need to capture the exit status of the parent if exiting abruptly. Thanks again..
Re: Parent process does not wait for Child processes to finish and exits after the waitpid statement
by ikegami (Patriarch) on Feb 06, 2012 at 19:30 UTC
    So the parent exits and you don't know why. First thing you should do: check its exit status to find out why.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://952134]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found