Tech Support > Computers & Technology > Programming > Perl fork
Perl fork
Posted by CwK on December 22nd, 2003


How to use fork() system function to fork multi child process at the same
time ?

For example:

Run a program to fork 5 child process at the same time and the parent must
wait until all child exit.

The child do some thing like to read different file at the same time.

Thanks


Posted by Jeff Schwab on December 23rd, 2003


CwK wrote:
Not possible, AFAIK.

Call fork five times. See code below.

#!/usr/bin/perl

use warnings;
use strict;

my @pid;
my $this_is_the_parent;

if( fork ) {
if( fork ) {
if( fork ) {
if( fork ) {
if( fork ) {
$this_is_the_parent = 1;
wait;
}
}
}
}
}

if( $this_is_the_parent ) {
print "parent $$\n";
}
else {
# do your thing with one of the files.
print "child $$\n";
exit;
}


Posted by Martien Verbruggen on December 23rd, 2003


On Tue, 23 Dec 2003 00:38:52 +0800,
CwK <p12@bigfoot.com> wrote:
You also asked this question in comp.lang.perl.misc. If you must post
the question to multiple newsgroups, please use the cross-posting
facilities of your newsreader.

See the followups in comp.lang.perl.misc for an answer to your question.

Martien
--
|
Martien Verbruggen | Blessed are the Fundamentalists, for they
| shall inhibit the earth.
|

Posted by Martien Verbruggen on December 23rd, 2003


On Mon, 22 Dec 2003 22:09:31 -0500,
Jeff Schwab <jeffplus@comcast.net> wrote:
I don't really know why you're doing what you're doing that way. it
doesn't scale very well, in case you need more children, and it doesn't
deal with fork() failing. It's also almost impossible to follow the flow
of the code 9which bit is parent, and which bit is child).

You also need to wait() as many times as you fork(), or, probably
easier, until wait() returns -1.

Why not simply (untested):

for (1..5)
{
my $cpid = fork();
die unless defined $cpid;
if (! $cpid)
{
# Child code goes here
exit;
}
}

# This is parent code
while ((my $cpid = wait()) != -1)
{
# Code needed to deal with exit of child
}

Martien
--
|
Martien Verbruggen | Unix is user friendly. It's just selective
| about its friends.
|

Posted by Jeff Schwab on December 23rd, 2003


Martien Verbruggen wrote:
That was just the first approach that came to mind. I'm certainly not a
Perl guru.

No, it is not impossible. You are mistaken.

Yes, I forgot to call wait.

That certainly is better.

-Jeff


Posted by Martien Verbruggen on December 23rd, 2003


On Tue, 23 Dec 2003 02:12:17 -0500,
Jeff Schwab <jeffplus@comcast.net> wrote:
[snip]

Note underlining.

What I meant is that what you wrote is not an idiomatic use of fork(),
either in Perl or in other languages. That means that anyone needing to
read your code has to make a mental switch to figure out what you were
doing. Normally, when using fork, the child and parent code is isolated
as soon as possible after the fork() call, to make clear how the flow
through the program continues in parent and child.

Martien
--
|
Martien Verbruggen | prepBut nI vrbLike adjHungarian! qWhat's
| artThe adjBig nProblem? -- Alec Flett
|

Posted by Leo Chan on December 23rd, 2003


Thanks all guys reply me asap!!!

I could implement the below code in my script. Very nice!!!

A merry xmas to you all!!

Leo



"CwK" <p12@bigfoot.com> ¦b¶l¥ó news:bs76mu$4d44@imsp212.netvigator.com ¤¤¼¶
¼g...



Similar Posts