hartmann@ds0050:~/learning/forkArena> cat parallelize_em_demo.pl #!/usr/bin/perl use strict; use warnings; use Carp qw(confess); my @commands = map { "touch $_; ls -l $_; sleep 2"; } qw(a b c d e); my ( $time_start, $time_elapsed_serial, $time_elapsed_parallel); $time_start=time(); #does first, waits two seconds, does the second, waits two seconds, etc. (should take about ten seconds) for my $bash_command ( @commands ) { run_bash_command($bash_command); } $time_elapsed_serial = time()-$time_start; print "\ntime elapsed serial: $time_elapsed_serial\n\n"; #does commands in parallel. (should take about two seconds) my $parallel_running_command = parallelize_bash_commands([@commands]); $time_start = time(); run_bash_command($parallel_running_command); $time_elapsed_parallel = time()-$time_start; print "\ntime elapsed parallel: $time_elapsed_parallel\n\n"; sub run_bash_command { my $command = shift or die "no command"; print "$command\n"; print `$command`; } sub parallelize_bash_commands { my $commands = shift or confess "no commands"; ref($commands) eq 'ARRAY' or confess "not an array"; my $parallel_running_command = ""; for my $command ( @$commands ) { $parallel_running_command .= "( ( $command ) & );"; } return $parallel_running_command; }