fork() and script execution afterwards



Ive got a question about script execution after a fork() which I havnt been
able to find an answer to on google yet, but Im still looking (Ive fork'd my
searching :-P)

I have this simple test script:

==========================
#!/usr/local/bin/perl

use strict;
my ($pid, $x, @child, $ischild);

print "BEGIN\n\n";

for ($x = 1; $x <= 5; $x++) {
if (not defined($pid = fork())) {
die "couldnt fork: \"$!\"\n";
} elsif ($pid) {
push(@child, $pid);
} else {
print "This is child $x starting at " . localtime() . "\n";
sleep($x);
print "Child $x terminating at " . localtime() . "\n";
exit(0);
}
}

foreach(@child) {
waitpid($_, 0);
}

1;

==========================

Fiarly straight forward, and it works beautifully.

My question is, for each of the child processes that are spawned, I dont see
"BEGIN" outputted to the screen.

Is this because the "program counter" in each of the childs continues from
where it was in the parent too?

The reason I ask is because Ive been trying to devise a way of executing a
child process without actually beginning the whole script over again. This
is because in my real script I want to perform a database query to determine
just how many childs I need to fork to complete my process.

But now Im beginning to wonder whether I actually need to borhter trying to
avoid executing the top half of the script each time the child is spawned,
if the PC continues from where it was in the parent, the child will never
return to the beginning of the script. That would be so nice! :-)

If this is true, where I print BEGIN I could place my database code, and
replace the for loop with a while loop that runs through each database
record and forks a new child if neccessary.

Thanks,
Tom


.



Relevant Pages

  • Re: Very big infrastructure - Please help
    ... if executing where principal is defined, ... > 1)I must add a user from the root domain to Cert Publishers group on every ... > child domain- to have the property permission "write user certificate" on ... > every child domain.How to do this with a script or anything else? ...
    (microsoft.public.windows.group_policy)
  • Re: Very big infrastructure - Please help
    ... executing relative to where resides the object, ... instead of an ldap moniker try use of a gc moniker, ... > child domain- to have the property permission "write user certificate" on ... > every child domain.How to do this with a script or anything else? ...
    (microsoft.public.windows.group_policy)
  • Re: trying to understand fork and wait
    ... old habits based on learning to script in REXX on the ... > the child reads it. ... situation for me (drop through to bottom/go back to top of loop). ... just to keep a hold of the exit code. ...
    (comp.lang.perl.misc)
  • Re: Questions about perl daemons with child processes and open files / signals
    ... running as daemon which launch a child process. ... I need to run a perl script as a daemon. ... tcpdump output, but I decided to use a pipe ... the parent process. ...
    (comp.lang.perl.misc)
  • Heirarchical path to the net
    ... traced the path of a net from child or leaf cell to the parent. ... script was run and it used .sch files(we use Calibre, ... Why the script traced up is because some signals have fan outs. ... hiCreateAppForm( ...
    (comp.cad.cadence)