<?xml version="1.0" encoding="windows-1252"?>
<node id="833224" title="Re: Parallel ForkManager" created="2010-04-07 04:30:30" updated="2010-04-07 04:30:30">
<type id="11">
note</type>
<author id="237051">
tirwhan</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;To further expand on the two (perfectly correct and valid) comments by [almut] and [moritz] above, here is a script that does exactly what you asked for:&lt;/p&gt;
&lt;c&gt;
#!/usr/bin/perl

use warnings;
use strict;
use Parallel::ForkManager;
use Sys::CPU;

my $number_of_cpus = Sys::CPU::cpu_count();
my $pm = new Parallel::ForkManager($number_of_cpus);

for (1..$number_of_cpus) {
    my $pid = $pm-&gt;start and next;
    my $string="abcdefg";
    my $ln=length($string);
    print "length of a string = $ln \n";
    $pm-&gt;finish();
}
&lt;/c&gt;
&lt;p&gt;This uses [mod://Sys::Cpu] to find the number of CPUs the system has, then forks off that number of processes and executes the code in them. Note you'll get the output ("length of a string...") multiple times (depending on the number of processors you have in the system), because exactly the same code is run in each process. So this approach is rather nonsensical, it uses all processors (normally, depends on your OS) but simply multiplies the workload. Something more sensible would be to have each child measure a different string, e.g.:&lt;/p&gt;
&lt;c&gt;
#!/usr/bin/perl

use warnings;
use strict;
use Parallel::ForkManager;
use Sys::CPU;

my $number_of_cpus = Sys::CPU::cpu_count();
my $pm = new Parallel::ForkManager($number_of_cpus);
my @strings = ("abcde","abcdefghij","ab");

for (1..scalar @strings) {
    my $pid = $pm-&gt;start and next;
    my $string=$strings[$_-1];
    my $ln=length($string);
    print "length of string \"$string\" = $ln \n";
    $pm-&gt;finish();
}
&lt;/c&gt;
&lt;p&gt;This will hand each child a different string and thus "parallelize" the work done. This is slightly less senseless than the previous example, but only slightly, because the work done by your machine to fork off a separate process is much more than the simple act of measuring the length of a string. So, as the previous commenters have already pointed out, you need to do some thinking on exactly what you want to parallelize and how to do it in a worthwhile fashion. Anyway, hope this helps clear up some confusion.&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-237051"&gt;
&lt;br&gt;&lt;i&gt;All dogma is stupid.&lt;/i&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
833215</field>
<field name="parent_node">
833215</field>
</data>
</node>
