in reply to Re^3: Waiting for an External Program to Finish Executing
in thread Waiting for an External Program to Finish Executing
(fork) returns ... 0 to the child process,
So this code is creating three processes:
The OP's problem is that he's waiting for the child but running the long-running process in the grandchild. The fix is to eliminate the second fork, and just do the work in the child; the purpose of a second fork is to do something in the background without having to wait for it, which is what the OP is trying to avoid.
Here's a small example:
outputs:warn "PID $$ (parent) forking\n"; unless ($pid = fork) { warn " PID $$ (child) forking again\n"; unless (fork) { warn " PID $$ (grandchild) exec'ing\n"; exec "sleep 5"; die "Couldn't run getSite.pl"; exit 0; } warn " PID $$ (child) exiting\n"; exit 0; } warn "PID $$ (parent) executing waitpid\n"; waitpid($pid,0); warn "PID $$ (parent) done.\n";
PID 6083 (parent) forking
PID 6084 (child) forking again
PID 6085 (grandchild) exec'ing
PID 6084 (child) exiting
PID 6083 (parent) executing waitpid
PID 6083 (parent) done.
Here's a version which does what I believe the OP wants.
warn "PID $$ (parent) forking\n"; unless ($pid = fork) { warn " PID $$ (child) exec'ing\n"; exec "sleep 5"; die "Couldn't run getSite.pl"; } warn "PID $$ (parent) executing waitpid\n"; waitpid($pid,0); warn "PID $$ (parent) done.\n";
But, this is completely equivalent to system, so why not just use that?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Waiting for an External Program to Finish Executing
by Moron (Curate) on Nov 15, 2005 at 10:00 UTC | |
by sgifford (Prior) on Nov 15, 2005 at 17:16 UTC |