D
debraj
Hi
I am trying out a new thing to me ie. use fork(). Now my requirement
is like this :
I have a file which has quite a few numbers such as :
22,23,34 etc.
Now I have a sub-routine say sub create_page, which when passed the
above numbers does quite a few things and creates html pages in their
name, such as 22.html etc.
So instead of passing one at a time, I wanted to pass it to fork'ed
processes so that they happen all at a time. This is where I am
getting stuck. Can anyone help out please ? basically how to call the
same sub with different parameters and on different processes ? and
how to control them ?
My code :
open LIST , "file";
FORKER: while (<LIST>) {
my $newpid = fork();
if ( not defined $newpid )
{
# if return value of fork() is undef, something went wrong
die "fork didn't work: $!\n";
}
elsif ( $newpid == 0 )
{
# if return value is 0, this is the child process
$parent = $pid; # which has a parent called $pid
$pid = $$; # and which will have a process ID of its very
own
@kids = (); # the child doesn't want this baggage from the
parent
last FORKER; # and we don't want the child making babies
either
}
else
{
# the parent process is returned the PID of the newborn by
fork()
print "$$ spawned $newpid \n";
push @kids, $newpid;
$i= $#kids;
$i ++;
}
}
if ( $parent ) # if I have a parent, i.e. if I'm the child process
{
print "I am process number $pid\n";
&create_page("$kids[$i]");
print "Creating $kids[$i]\n";
exit( 0 );
}
else
{
# parent process needs to preside over the death of its kids
while ( my $kid = shift @kids )
{
print "Parent waiting for $kid to die\n";
my $reaped = waitpid( $kid, 0 );
unless ( $reaped == $kid )
{
print "Something's up: $?\n";
}
}
}
I am trying out a new thing to me ie. use fork(). Now my requirement
is like this :
I have a file which has quite a few numbers such as :
22,23,34 etc.
Now I have a sub-routine say sub create_page, which when passed the
above numbers does quite a few things and creates html pages in their
name, such as 22.html etc.
So instead of passing one at a time, I wanted to pass it to fork'ed
processes so that they happen all at a time. This is where I am
getting stuck. Can anyone help out please ? basically how to call the
same sub with different parameters and on different processes ? and
how to control them ?
My code :
open LIST , "file";
FORKER: while (<LIST>) {
my $newpid = fork();
if ( not defined $newpid )
{
# if return value of fork() is undef, something went wrong
die "fork didn't work: $!\n";
}
elsif ( $newpid == 0 )
{
# if return value is 0, this is the child process
$parent = $pid; # which has a parent called $pid
$pid = $$; # and which will have a process ID of its very
own
@kids = (); # the child doesn't want this baggage from the
parent
last FORKER; # and we don't want the child making babies
either
}
else
{
# the parent process is returned the PID of the newborn by
fork()
print "$$ spawned $newpid \n";
push @kids, $newpid;
$i= $#kids;
$i ++;
}
}
if ( $parent ) # if I have a parent, i.e. if I'm the child process
{
print "I am process number $pid\n";
&create_page("$kids[$i]");
print "Creating $kids[$i]\n";
exit( 0 );
}
else
{
# parent process needs to preside over the death of its kids
while ( my $kid = shift @kids )
{
print "Parent waiting for $kid to die\n";
my $reaped = waitpid( $kid, 0 );
unless ( $reaped == $kid )
{
print "Something's up: $?\n";
}
}
}