views:

394

answers:

3
  use LWP::Simple;
  use Parallel::ForkManager;


  @links=( 
    ["http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe","SweetHome3D-2.1-windows.exe"], 
    ["http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg","SweetHome3D-2.1-macosx.dmg"],
    ["http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip","SweetHome3DViewer-2.1.zip"],

  );

  # Max 30 processes for parallel download
  my $pm = new Parallel::ForkManager(30); 

  foreach my $linkarray (@links) {
    my $pid = $pm->start and next; # do the fork

    my ($link,$fn) = @$linkarray;
    warn "Cannot get $fn from $link"
      if getstore($link,$fn) != RC_OK;

    print "$pid = $link is done ";

    $pm->finish; # do the exit in the child process
  }
  $pm->wait_all_children;

after this execution i am going pid is zero

0 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip is done 

0 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg is done 

0 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe is done

But now I really thinking whether its really doing the fork. Why I am getting all times 0 as pid in Parallel::ForkManager?

EDIT 2

i have 2 changes

my $pid = $pm->start ; # do the fork

and $$ for process id

21892 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe is done 

21893 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe is done 

21892 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg is done 

23120 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg is done 

21892 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip is done 

23146 = http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip is done 

But now I am getting 2 twice. Why I am getting this?

+5  A: 

Not sure about the Parallel::ForkManager implementation, but if it is anything like standard fork() the 0 is being returned because you are in the child process. Fork will only return a non-zero pid to the parent process.

I forgot to add, given the 'and next' after the start call the code is behaving exactly as expected.

Note to EDIT 2:

Since you dropped the 'and next' from the start call the parent process is also running the download as well. I am guessing 21892 is the pid of the parent process which will run through the loop multiple times

Nic Strong
+4  A: 

See that line?

my $pid = $pm->start and next; # do the fork

The parent gets the pid, the child will see only 0. If you want to get your pid from within the child process, simply use the magic variable $$.

UPDATE:

The reference to that line was not meant as a invitation to change the line. But looking at it, it should become obvious that $pid will be 0 in the code following the line so it should hardly come as a surprise that a print $pid would print 0 and nothing else.

innaM
+1 for Very Good Point Manni
joe
@Manni - after this got 2 times running
joe
2 times running? Not sure what you are trying to say. sorry.
innaM
if you are seen the output of that process execution it shows its printing 2 times . but it should show one only times
joe
Why did you change the $pm->start line??
innaM
i got it what is issue
joe
+5  A: 

About your code below Edit2:

You changed

my $pid = $pm->start and next;

to

my $pid = $pm->start ; # do the fork

Now you are not doing the fork correctly. While previously only the child continued executing the loop body and the parent taking the next, now both continue executing the loop body. Therefore you see the pid of the parent (21892) executing the loop several times.

edgar.holleis

related questions