Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use forks; use forks::shared; use strict; use warnings; my $num = "10"; my @results = (); share(@results); my @children = (); for my $i (0..$num-1) { my $pid = fork(); if ($pid) { # Parent push @children, $pid; } elsif ($pid == 0) { # Child my $result = "abc"; # Later to be replaced with the # actual thing to be computed $results[$i] = $result; sleep 3; exit(0); } } foreach (@children) { waitpid($_, 0); } for (my $r=0 ; $r<@results; $r++) { print $r . "\t" . $results[$r] . "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Forks and IPC
by BrowserUk (Patriarch) on May 31, 2010 at 20:26 UTC | |
|
Re: Forks and IPC
by JavaFan (Canon) on May 31, 2010 at 20:09 UTC | |
|
Re: Forks and IPC
by almut (Canon) on May 31, 2010 at 20:12 UTC |