fork off several children processes

M

monk

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;
 
X

xhoster

monk said:
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
 
M

monk

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?
 
P

Peter Makholm

monk said:
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
 
X

xhoster

monk said:
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
 
M

monk

Thanks all, for what I need I'll use Parallel::ForkManager.
That was right on the money.

;*)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top