fork off several children processes
fork off several children processes
am 27.08.2007 23:36:48 von rsarpi
Hi all, How can I fork off several children processes? sorry for the
dumb newbie question.
With this solution below, parent1() executes in parallel with
child1(), then parent2() executes in parallel with child2(), and so
on.
What I want is to have something like all parentX() subroutines
executing along with all childrenX() subroutines, all in parallel, all
together like a good family.
Is that possible?
This is what I have:
if ($pid = fork) {
print "processing parent subroutines\n";
parent1();
parent2();
parent3();
}
elsif (defined $pid) {
print "processing children subroutines\n";
child1();
child2();
child3();
exit(0);
}
else {
print "couldn't fork: $!\n";
}
#wait for children
waitpid($pid, 0);
print "Done with parent and children! \n";
exit 0;
Re: fork off several children processes
am 28.08.2007 00:26:15 von xhoster
monk wrote:
> Hi all, How can I fork off several children processes? sorry for the
> dumb newbie question.
>
> With this solution below, parent1() executes in parallel with
> child1(), then parent2() executes in parallel with child2(), and so
> on.
From what you have shown, this should only be the case if parent1() and
child1() take the same time to execute, parent2 and child2 take the
same time to execute, etc. Once parent1() finishes, it goes on to
parent2() regardless of what the other process is doing.
> What I want is to have something like all parentX() subroutines
> executing along with all childrenX() subroutines, all in parallel, all
> together like a good family.
I don't understand what you want. It sounds like you want to make
all the processes siblings. If that is the case, why do you name
some of them parents and some of them children?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: fork off several children processes
am 28.08.2007 01:18:21 von rsarpi
> > What I want is to have something like all parentX() subroutines
> > executing along with all childrenX() subroutines, all in parallel, all
> > together like a good family.
>
> I don't understand what you want. It sounds like you want to make
> all the processes siblings. If that is the case, why do you name
> some of them parents and some of them children?
Thanks for your reply.
So should I have only *one* subroutine under the parent section,
and the rest of the subroutines under the child section?
Would that accomplish the goal of running several children in
parallel?
Re: fork off several children processes
am 28.08.2007 05:56:31 von Peter Makholm
monk writes:
> Thanks for your reply.
>
> So should I have only *one* subroutine under the parent section,
> and the rest of the subroutines under the child section?
>
> Would that accomplish the goal of running several children in
> parallel?
No, to run several things in parallel you'll have to have several
forks. I would use something like Parallel::ForkManager:
use Parallel::ForkManager;
my @tasks = ( \&parent1, \&parent2, \&parent3, \&child1, \&child2, \&child3 );
my $pm = new Parallel::ForkManager (scalar @tasks);
for my $task ( @tasks ) {
my $pid = $pm->start and next;
$task->();
$pm->finish;
}
$pm->wait_all_children;
but of course the parent child distinction is meaningless when
everything is run in parallel.
//Makholm
Re: fork off several children processes
am 28.08.2007 18:05:38 von xhoster
monk wrote:
> > > What I want is to have something like all parentX() subroutines
> > > executing along with all childrenX() subroutines, all in parallel,
> > > all together like a good family.
> >
> > I don't understand what you want. It sounds like you want to make
> > all the processes siblings. If that is the case, why do you name
> > some of them parents and some of them children?
>
> Thanks for your reply.
>
> So should I have only *one* subroutine under the parent section,
> and the rest of the subroutines under the child section?
>
> Would that accomplish the goal of running several children in
> parallel?
If you want to run N processes in parallel, you need to have N-1 forks.
(or N forks if the "parent" does nothing of its own other than fork
and wait, which is not a bad idea). See, for example,
Parallel::ForkManager.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: fork off several children processes
am 28.08.2007 21:57:59 von rsarpi
Thanks all, for what I need I'll use Parallel::ForkManager.
That was right on the money.
;*)