#!/usr/bin/perl use strict; use warnings; my %pids; my @chunk_indixes = find_chunk_indixes( 100, 10 ); foreach my $thread_id ( 0 .. 9 ) { if ( my $pid = fork() ) { $pids{$pid}++; } else { my $start = $chunk_indixes[$thread_id]->[0]; my $end = $chunk_indixes[$thread_id]->[1]; print "[$start, $end]\n"; sleep(10); exit; } while (%pids) { my $pid = waitpid( -1, 0 ); delete $pids{$pid}; } } sub find_chunk_indixes { my ( $total_size, $number_of_chunks ) = @_; my @result; my $small_chunk = int( $total_size / $number_of_chunks ); my $oversized_count = $total_size % $number_of_chunks; my @chunk_sizes = ( ( 1 + $small_chunk ) x ($oversized_count), ($small_chunk) x ( $number_of_chunks - $oversized_count ) ); my $current_position = 0; foreach my $chunk_size (@chunk_sizes) { push @result, [ $current_position, $current_position + $chunk_size - 1 ]; $current_position += $chunk_size; } return @result; }