Home > Archive > PERL Miscellaneous > August 2007 > fork off several children processes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
fork off several children processes
|
|
|
| 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;
| |
| xhoster@gmail.com 2007-08-27, 7:52 pm |
| monk <rsarpi@gmail.com> 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
| |
|
| > > What I want is to have something like all parentX() subroutines
>
> 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?
| |
| Peter Makholm 2007-08-28, 8:24 am |
| monk <rsarpi@gmail.com> 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
| |
| xhoster@gmail.com 2007-08-28, 7:07 pm |
| monk <rsarpi@gmail.com> wrote:
>
> 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
| |
|
| Thanks all, for what I need I'll use Parallel::ForkManager.
That was right on the money.
;*)
| |
|
|
|
|
|